Thursday, May 7, 2009

Final Refractoring Thoughts

Refractoring is nice tool to use, I probably won't use it constantly like I would unit tests but it really does help to clean up code.  Like the other day I ended up writing about 30 if statements in a row for one particular attribute that I was working on.  It ended up being about 200 lines for all of it and would only return a single value back.  I then had to do this two more times for 2 different attributes.  It made the code look terrible and I had no idea where one thing started and the other stopped.  So I used extract method on all three of those things and shoved it into a new file to hold it.  Then again I did extract method on them and then put all of the if statements related to one attribute in its own method.   Stuff like this there really isn't a big deal.  It's more of a stylistic thing and it does help make hard to follow code look way better.  Doing stuff like this doesn't really have an performance gains or hurts the performance of the application so it's not a big deal.

On the other hand, performance is a big deal.  I do believe that if you use extract method several times with small amounts of code in it over a large block of code it hurts performance by a small amount.  But when this code is called constantly and the application depends on it running super fast everything slows down significantly.  There are times when OOP is critical to be used but having ugly code that runs in 0.0000003 seconds compared to 0.01 seconds and it is being called over a billion times, I'd much rather prefer the horrifying looking code than something that is amazing looking but takes forever.  I'd much rather have Windows move files in less than a minute compared to an hour.

Job security is one of the other things that comes to mind.  If you write code that no one else can understand, you are less likely to be fired.  If you have something that looks like it is summing up some data but in reality is just sorting that data and the comments make zero sense, then that is job security.  It doesn't mean that if you write horrible looking code constantly, that could easily be written very nicely, is a good idea.  It just means that if there is some function that ties the whole system together and you are the only one who knows how it works and where to add/remove things that is pretty good.

The book on refractoring does have some really useful ideas for one to use after they have written the code or having to make changes to existing code later.  I know for sure I won't be writing code and then spontaneously decide to "Replace Type Code with State/Strategy".  I'd much rather have the code working perfectly first then clean it up.

This is the last CS 373 blog entry I'm going to write.  So to any hardcore followers, you should know where to find me on the internet.

Wednesday, May 6, 2009

Pro/Cons of Python

This is somewhat of a recap of what I have learned about Python this semester and what I like about it and what I hate about it.

Likes:
Generators:  Are super awesome.  They remove some of the trouble of having to keep track of an array or something that is iterable and then keep track of which spot you are at.  Instead with a generator, all you need to do is call next() and you get the next thing.  This is easily one of my most favorite things in python.

List Operations:  with stuff like [0: 5] it makes getting a sub array out of an array easy to do.  With it being able to start at the end and then work backwards is great as well.  Just to get the last element, using arr.length()-1 as the index instead makes it somewhat more readable and easier to do.

Print:  Just saying print "something"  instead of System.out.println("something");  is so much better just because it requires less typing.  And really the whole "System.out" part I hate typing because of its redundancy.

Dynamic Typing:  s="A string" I know s is a string, why should I have to type String s = "A string";  And then if I am done with s being used I could do s=67/3.  

Hates:
Dynamically Typed:  I absolutely hate it how if in a method one of its parameters is an object "A" and what is actually needed is an object "B" and then when it is used DURING RUNTIME we get the error.  This actually makes things more difficult to figure out what is going on  during runtime to pinpoint when "A" is actually being passed in.  And it could be at the end of the program making me retrace every line of code.

Indention:  I hate this.  While it seems nice to do after a while  I lose track of how the code flows and when if statements end or functions end.  I really like the enforced indentation but without nice "{" "}" I can't tell the difference between some lines of code and when to debug things.

Arrays:  I don't know why I can't create an array with a set size.  I hate having to create a while loop and then iterate over it appending useless data to the array just to get the array into a desirable size.  Which makes me hate it when I'm going through the array after finally using it and I hit one of the "dummy data" indexes and then python either: Uses that data and screws things up. Or: Treats it as an object and then throws me an error and then screw things up.

For Loops:  Where are they?  Sometimes I need to be able to iterate over something with a size that changes and using a while loop is just not good enough.  I hate having to create a lcv and setting it to 0 before the execution of the while loop and then in the end I always forget to increment/decrement it.

So those are some nice rants about how much I have enjoyed Python and how much I despise Python.  I really would not suggest using Python in a large scale project.  It makes a really well developed script but sometimes debugging things in it seems too hard to do in a large project where the complexity in increased 10000x over.

Thursday, April 30, 2009

Explaining MoisUnn

MoisUnn is a groundbreaking programming language that has just recently been launched. So not many people know about it. It is very similar to Java/C++ in some of its syntax and borrows some from Python.

The main features of MoisUnn are the ability to do concurrent assignments, powerfull list operations and multiple returns.

Concurrent Assignments:
Originally to get some thing like:
int a=5; int b=0;
int a_old=a;
a= a+5; b=a_old;
//a=10, b=5

In this better way:
int a=5; int b=0;
a,b=a+5,a;
//a=10, b=5

So the ability to write less code that is more powerful becomes evident.

List Operations:
There are some math operations you should be familiar with: +,-,/,*,%. MoisUnn introduces ^ as the exponent operator.
You can apply a single arithmetic operation on a list, or use a list to do math on another list.
EX:
to add 1 to every element in a list:
int list=[1,2,3,4,5];
for(int i=0; i less than list.size(); i++)
list[i]=list[i] +1;
//list=[2,3,4,5,6]
But in MoisUnn, it is:
array of int x: [1,2,3,4,5];
x=x+1;
//x=[2,3,4,5,6]
Also to use a list on a list:
array of int x: [1,2,3] +[4,5,6];
//x=[5,7,9]

This can be applied to doubles/floats.

Boolean Operators can be applied as well (<,<=, ==,!=,=>,>)
array of boolean x: [1,2,3,4,5] less than 3;
//x=[true,true,false, false,false]

And logical operators can be applied as well (!, &&, )
array of boolean x: [true,false,true] && [false,true,true]
//x = [false, false, true].

Now the fun stuff with lists begins:
The operator ".:" will append an element to the beginning of a list. The operator "`" will pop off the first element of a list. The operator ":." will append an element to the end of a list. The operator "++" will combine two lists. The operator $ returns the size of a list.
EX:
array of int x: [1,2,3,4]; int y;
y=`x;
//y=1, x=[2,3,4]
x=x :. y;
//y=1, x=[2,3,4,1]
x= 5 .: x;
//x=[5,2,3,4,1]
x= x ++ [34,4];
//x= [5,2,3,4,1,34,4]
int b= $x;
//b=7

You can also access individual elements of a list and get values from it or put values in it.
It also allows list operations like in python.
array of int x: [1,2,3,4,5]
array of int y: x[0:2]
//y=[1,2]

There are no lists of Chars available in MoisUnn because then that would be a string. And a list of chars is just stupid in general.

Also the operator "in" can be applied to lists or strings like in python to see if something is in a list or a string.

Multiple Returns:

Normally do find the min and max of two elements you would have to do:
int max(int x, int y)
{ if (x greater than y) return x;
return y;
}

int min(int x, int y)
{ if (x less than y) return x;
return y;
}

int mi=min(5,2);
int ma=max(5,2);
//mi=2, ma=5

In MoisUnn:
int, int MM(int x , int y)
{ if (x greater than y)
return x,y;
return y,x;
}
int max, min;
max,min=MM(5,2);
//max=5,min = 2

Of course, multiple returns can make code far more powerfull and far more complex.
int ba()
int, int, fool(int x, int)
int,bool foo()
float, bool, int, int foo2(int, bool, int, int)

And then :
foo2(foo(),fool(ba(),5))

Is completely runnable.

So far that's all that has been accomplished with MoisUnn in the past few weeks. It is set to rapidly expand with it's usability and flexibility and revolutionize(hopefully) programming languages.



MoisUnn is copyrighted by Stephen Moist and Varun Unnikrishnan.

Thursday, April 23, 2009

DON'T DO THIS

SERIOUSLY DONT.

When you write unit tests don't ever use loops to check things.  EVER.
As an example:

for (int i=0; i is less than 512; i++)
{
ASSERT(something);
}

Why?  Because when one of the asserts fail, then what happens is that you get the line number of the assert failing.  Which is in the example above, 3.  Which is completely useless.  It won't tell you on which iteration it failed.  And if you are using an ASSERT_FAIL, then it is even more useless.   This is horrible.  This should never be used unless your unit tests test the ability of a generator to test whether or not it can consistently generate good values.  See the example of how to do this that isn't retarded.
A better version to do is:
int numberofFailures=0;

for (int i=0; i is less than 512; i++)
{
if( the same logic of the assert)
else:
numberofFailures++;
}
AssertEquals(numberofFailures, 512);

Another thing:
Don't have output that you have to check manually.  This really is bad.  And I don't mean printing out some number and then checking by hand if that number is the right output.  What I mean is printing out :
-----------------------------------
Test 21: Passed: Good values of tacos.
-----------------------------------
***************************
Test 22: Failed: Too much tacos.
***************************

This is not helpful since you are not using asserts which can then print standardly out to a file which then can be easily parsed and integrated in a report for all the unit tests ran. 

I am not against printing things out, but USE ASSERTS not prints and if's instead.

Also:
If you use a counter to keep track of the current test case being ran, this is stupid.  Use fixed numbers.  It makes things very hard to figure out which test is being ran when everything looks the same!

Thursday, April 9, 2009

Making my Own Unit Test Suite

Yea I'm going to do that.

I hope to do something more flexible than Python's unit testing framework where if something fails it stops everything.  I won't be using asserts to do this because I don't wanting it to stop stuff, I want it to continue on!  It will will probably be an object.  I hope for it to somehow be compatible with the google app engine and also independent of it.  So I hope to let the user specify if they want it to be part of the google app or just write the output directly to the terminal.

Yea, it's gonna have to be an object for that to work.  So far I've come up with the methods that you can use:

Unit.assertTrue(boolean value)
Unit.assertFalse(boolean)
Unit.assertTrueFAIL(boolean)
Unit.assertFalseFAIL(boolean)
Unit.assertEqual(thing 1, thing 2)
Unit.assertNotEqual(thing 1, thing 2)
Unit.assertEqualFAIL(thing 1, thing2)
Unit.assertNotEqualFAIL(thing 1, thing 2)

The functions with "FAIL" in them will instead of using a nice if statement they will use an assert, so if the assert fails, it will stop everything. This allows things to where the code after it is absolutely DEPENDENT on the code above working correctly.  This is great for when if something should stop at that point could perhaps, say, delete your whole database or enter a profoundly awesome loop that will not end.

If I have enough time, I will add to it:
Unit.assertTrueGIVE(boolean) 
where the GIVE will return the value of what happened.  So if you make a function call in it, it will return that function call's return value.
So it will be possible to do:
x=Unit.assertEqualGIVE(5,65) and will set x = to 5.

So theoretically, there will be no statements other than unit test calls in the unit test file.  Nifty eh?

Any comments or feedback would be nice.

Wednesday, April 1, 2009

Selenium Part 2

So hopefully by now you have gotten adjusted to Selenium or completely abandoned it.  A quick note on how Selenium works.  It works by looking for a field and when it finds that field, it will insert text into it or selected it.  So if you name your phone number field in TA "phone" and then change it, Selenium won't find it.  But if you move it around on the page, or change the validators and code that gets run from it, Selenium will run it fine.  So keep that in mind if you start changing the names of fields.

So open up the IDE.  Load in a test and then click the tab called "Source" on the window bar (or where ever) under "Options" go to "Format" and then "Python".  It will take your nice GUI test and convert it all to python code.  More specifically, a unit test built around the Selenium framework.  And the best part is that it is easily runnable, you just copy and paste it into a new file.py and then run it from python.  You can easily link many together.   So if you have multiple tests, just make a "UIUnitTest.py" file, copy all of one into it and then just grab the "def test_new(self):   " part and paste it in.   Python will run it all as one big unit test automatically.  Isn't it wonderfull!

However......   For this to run, you must install the Selenium RC package (it is on the same download page).   And you must use Unix, Linux or the terminal in OS X.   So download it,  unzip it and shove it into a directory.  The next part you need to visit here  on how to set environment variables and point Selenium to the right spot.   So go into the folder selenium-remote-control-1.0-beta-2/ and then into selenium-server-1.0-beta-2   .   Now run the jar there by java -jar selenium-server.jar  .   This must be running for you to remote execute things.  Make a new terminal and put your UIUnitTest.py file under the one python branch in the selenium-remote-control-1.0-beta-2 folder and then run it.   It will automatically launch firefox (remember to have X windows on...) and then run it.  And please make sure that it runs correctly before doing this!

Now the great part about running it in the RC version is that you can inject Python code into the unit test file.  So if you need to create tables for a database, you can easily do that.  Run some script?  Go ahead!  What ever Python can do, you can put it in and then automate it.

So if you think Selenium will help you in whatever, use it.  If not, don't.   Oh and remember, the documentation for Selenium is horrible.  So if you have a question, ask here I may be able to help.

Tuesday, March 31, 2009

Selenium (not the Element)

Today's topic is selenium.   Selenium is this pretty nifty tool that helps for GUI "unit testing".  What it does is records all of your actions and then will play it back for you.  It's perfect when you make changes to the UI under the hood and want to make sure stuff works right.  So basically, it is a unit test of your UI.

I've already confirmed that it does work with the group projects (at least with my group's project).  So you can totally use it to help automate filling in forms over and over by a simple click of the mouse.

Selenium is basically another class to use in python.  (It also runs with Java, PHP, Perl, Ruby, JavaScript and many more natively).  So you can just make a new instance of a window and tell it to start clicking on things and filling in forms.  

But there is the best way, go get the Selenium IDE from seleniumhq.org/download.  The IDE is  firefox plugin.  Go ahead and install it.  Then go under tools and select it.  There will be a path name on the front, paste the google app engine link into it.   Click the red button off to the right and start filling in forms and navigating through pages and clicking on stuff.  When you are done, hit that red button again and then hit the play button.   It will start to play back exactly (or nearly exactly, Selenium is sometimes wonky when recording things but you can easily insert some commands to fix it) what you did.  Play with it for a while and see what happens.  You might like it or you might hate it.  Either way, I don't really care.

Any way there is a convert script to python tab menu thing and it will do that and generate a python script where you can add system calls and other functions you write (such as creating a table) and then run it as the Selenium script starts running.  Next time, I will go more in depth about this to give you time to play with Selenium because the next part is a pain to get to work and requires you to know how to do Selenium well.