Tuesday, August 24, 2010

Syntax Highlighter for Blogger

I have decorated the code snippets in the blog with a syntax highlighter.

Thanks to syntax highlighter & this nice post:Carter Cole's blog on highlighter

The latest version seems to have scroll bar's by default. I don't have enough time to fix it, reverting to a older version.

Tuesday, May 11, 2010

Useful Eclipse Plugins

Copy/Cut the whole line
This was a surprise gift while Googling for something else.
This plug-in lets you copy the current line with regular ctrl+c, No need to select the whole line.
You can get it here:
http://code.google.com/p/copycutcurrentline/
Eclipse has a built-in shortcut Ctrl+D for deleting current line.
Explore in File System
This plug in adds "Explore in FileSystem" context menu to package explorer in Eclipse. Very useful when you want to view the files in native file explorer.
The alternative way would be to go to properties and copy the path and manually visit the location in file explorer, which is not worth doing.
Well, I do not see I need this everyday. But when I need it, this plug in does save valuable time.
You can get it here:
http://www.junginger.biz/eclipse

Tuesday, April 20, 2010

Reusable Logic inside a Non-Reusable Method

I recently encountered a method like this:


void calculateSomething(Map map,String factor,int mode){
       ....
       //calculation steps
       ....
       map.put("caculatedValue",output);
}
This method was a private method and It should have been called from only one place at the time of writing. When I wanted to calculate the same value somewhere else in the program, I need to prepare a map just to satisfy this method.

Still, I wont be sure if I should keep something inside the map so that I wont break the contract. That is, to use this method I need to go through it thoroughly. The contract on the map is not intuitive and clients can break the contract easily.

Things can get worse if the map is passed as an argument to another method.

The solution I came up with is to extract a method[Refactoring, Martin Fowler] and make the method sequentially cohesive [Code Complete,Steve McConnell].

The caller code after refactoring:

int calculatedValue=calculateSomething(String factor,int mode);

map.put("caculatedValue",output);


The reusable method that was not visible initially:
calculateSomething(String factor,int mode){
         ....
         calculation steps
         ....
         return output;
}

Conclusion:
There are methods that might do something reusable + something not required for all callers.
The idea is to make the reusable part clearly visible. Speculation on this idea might lead to extraction of many small methods. It is always better go with evidence.