Wednesday, December 23, 2009

the worst job ever

Chris Hardin shares his bad experiences on his last job as a Senior Software Architect. Take a look here and see if there are similar problems you experience.

Thursday, December 17, 2009

mockito tutorial

Mockito makes life easier for anyone who's writing unit tests. First I planned writing a brief tutorial for Mockito but then I saw Brett L. Schuchert's mockito tutorial. It is as detailed and clear as it can be. I recommend it.

Sunday, October 25, 2009

no operator overloading in java? tell it to + operator

Lets begin with the wikipedia definition of operator overloading.

In computer programming, operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments.



It's generally thought that Java does not support operator overloading which is not true.
Take a look at + operator.



int two = 2;
int six = 6;

int sum = two + six;

System.out.println(sum); // output is 8



Quite expected right? I give two operands (two and six) and + operator adds them together. What if I use strings as operands?


String a = "avenged";
String b = "sevenfold";

String result = a + b;

System.out.println(result); // output: avengedsevenfold


+ operator concatenates two strings. As you can easily see + operator has different implementations depending on the type of operands.
Now, lets take a look at some gotchas of the + operator.
What if I mix different types of operands?


int two = 2;
int six = 6;

String str = "depechemode";

System.out.println(two + six + str);

System.out.println(two + str + six);

System.out.println(str + two + six);


The outputs are quite interesting. Let me explain you how it's processed. From left to right it's inspected if there are any strings as operands. As long as I don't see a string I treat + as addition but when I see one I begin to treat every subsequent operand as strings and I concatenate them.

So I have two + six which are two integers. I add them together which makes 8. Then I see a string and treat 8 as a string and concatenate it with str which in result gives "8depechemode".

Later I have two + str. str is a String so two will be treated like one. As I saw a string I treat all of the rest as strings and concat them. The result is "2depechemode6".

For the last example, we see a string as the first operand. We will begin treating every operand as strings and concat them so I'll have "depechemode26" as result.

I hope that everything is clear for + operator and its overloading capabilities.

Saturday, October 10, 2009

refactoring my code (with a little help from my friend Eclipse)

In this post, I'll explain few refactoring tricks and we will use the help of Eclipse as much as possible. Here we go!



Map<String, List<String>> personalItemsMap = new HashMapMap<String, List<String>>();

// for darth vader

List<string> vaderList = new ArrayList<string>();
vaderList.add("lightsaber");
vaderList.add("helmet");
vaderList.add("armor");
personalItemsMap.put("darth vader", vaderList);

// for han solo
List<string> soloList = new ArrayList<string>();
soloList.add("blaster");
soloList.add("vest");
personalItemsMap.put("han solo", soloList);

// for boba fett
List<string> bobaList = new ArrayList<string>();
bobaList.add("blaster");
bobaList.add("mandalorian armor");
personalItemsMap.put("boba fett", bobaList);

System.out.println(personalItemsMap);







Above we create a list of items for each character and add them to an owner-item map. This piece of code is begging for refactoring.



First, I choose the piece of code I'd like to extract as a method.



Then I choose refactor>extract method.


I enter "generateVaderList" as method name and here's our refactored code.


public static void main(String[] args) {
Map<String, List<String>> personalItemsMap = new HashMap()<String, List<String>>;

// for darth vader
List<string> vaderList = createVaderList();
personalItemsMap.put("darth vader", vaderList);

// for han solo
List<string> soloList = new ArrayList<string>();
soloList.add("blaster");
soloList.add("vest");
personalItemsMap.put("han solo", soloList);

// for boba fett
List<string> bobaList = new ArrayList<string>();
bobaList.add("blaster");
bobaList.add("mandalorian armor");
personalItemsMap.put("boba fett", bobaList);

System.out.println(personalItemsMap);
}

private static List createVaderList() {
List vaderList = new ArrayList();
vaderList.add("lightsaber");
vaderList.add("helmet");
vaderList.add("armor");
return vaderList;
}


No need for a temporary reference assignment so I can edit the below part;


List<string> vaderList = createVaderList();
personalItemsMap.put("darth vader", vaderList);


to


personalItemsMap.put("darth vader", createVaderList());


If I do this refactoring for every character my code will be clearer. But think about it. If I do that I'll have three different methods with similar functionality. These methods create a list, add necessary elements to the list and return the list. What if I take these methods and write a more generic one instead of three methods with similar functionality? Later, if I'd like to add a new character I won't write a createNewCharacterList method. I'll use the generic one instead.

Lets see how we can make the method more generic. It creates a list and adds hard-coded strings to it. This method will be general if I hand it the item strings to add. First I choose the name of "createVaderList" and then right click. Then refactor>rename. I rename it as "createItemList". I'm writing a more general method, remember? I can change the method signature from the menu but this is not so handy so I'll skip that and edit my method by hand.


private static List createItemList() {
List<string> vaderList = new ArrayList<string>();
vaderList.add("lightsaber");
vaderList.add("helmet");
vaderList.add("armor");
return vaderList;
}


will become


private static List<string> createItemList(String... items) {
List<string> itemList = new ArrayList<string>();

for(String item : items)
itemList.add(item);

return itemList;
}


In this new method, we -again- create a list. Then we take an arbitrary number of strings. I assure this behavior by using var-args. Then in our beautiful for-loop we add the items to the item list and return the list.

After that we will do the necessary changes in the code. Eclipse can't help us in this case. After we do the necessary changes in our code, our main method will be such as below;


public static void main(String[] args) {
Map<String, List<String>> personalItemsMap = new HashMapMap<String, List<String>>();

// for darth vader
personalItemsMap.put("darth vader", createItemList("lightsaber", "helmet", "armor"));

// for han solo
personalItemsMap.put("han solo", createItemList("blaster", "vest"));

// for boba fett
personalItemsMap.put("boba fett", createItemList("blaster", "mandalorian armor"));

System.out.println(personalItemsMap);
}


Much clearer and more usable right?

Sunday, October 4, 2009

new metal gear peace walker trailer

Check the new metal gear solid: peace walker trailer from the Tokyo Game Show 2009.
The plot is confusing even for the fans of the series, but we eagerly wait for the magnificient work that'll be delivered by Hideo Kojima.