
Here are a few more tips / tricks with which you can save time when you use the eclipse IDE.
Assign Key bindings:

Navigate to Window -> Preferences -> General -> Keys
You will find that there are many key bindings that are not assigned to actions that you might do on a daily basis. For example, when I start up on a new project, it is highly likely that I will introduce a lot of Value Objects (or Transfer / Data object, whatever you like to call them). A slick feature is to generate the ‘Getters and Setters’ for them using the ‘Source -> Generate getter and setter’ command from the Main menu.
You can either do that or you can assign a shortcut key to this action and get it done quickly. I use Alt+Shift+Q, +G (Yeah not the easiest combination. I know
). To restart eclipse I use Ctrl + F12. You might want to look at other bindings like
Ever visit sites like topcoder.com and solve a problem or two ? No ? You should and do so regularly. It will help find tune your programming / design skills. This is because solving the problems requires going though the following thought process
- Understanding the problem.
- Thinking up a solution that covers all use cases.
- Executing your thought process through code.
- Testing.
Now as simple as that sounds, it is not so easy to get right. Especially when you are competing with thousands of fellow coders for time. And when time is a constrain you usually select the first solution that comes to your head, which might not necessarily be the best. So there is that risk of your code timing out even if it is following the right logic. I would love to delve into a problem right about now and throw in some examples and illustration, but you get the point.

I ran into another sweet little bug that I thought I would share with you. This one cropped up after it passed a few tests. See if you can spot the problem. Without further delay here it is
Problem:
A record needs to be inserted with a new identity number. This number is obtained as the next maximum ID value from a set of elements in a XML file. The following code is supposed to obtain the next ID from the XML. Lets skip the XML related code since that just adds clutter.
Code:
int max = 0;
for( DataType data : someListFromTheXml )
{
if(max < data.getId())
{
max = data.getId()+1;
}
}
s_logger.info(" Max id: " + max);
return max;
See anything wrong with it ? Its got a pretty big flaw. The logic works only for odd number of elements. When the number of elements is even, it fails. Here’s an illustration
Test cases:
public class Test
{
public static void main(String... args)
{
int [] input1 = new int[]{1};
int [] input2 = new int[]{1,2};
int [] input3 = new int[]{1,2,3};
int [] input4 = new int[]{1,2,3,4};
Test test = new Test();
test.go(input1);
test.go(input2);
test.go(input3);
test.go(input4);
test.go(input5);
}
public void go(int [] input)
{
int max=0;
for(int id:input)
{
if(max < id)
{
max = id+1;
}
}
System.out.println(" Max id: " + max);
}
}
Here’s the output:
I often see environments where web applications use log4j for logging into files using various appenders. That is all well and good until I see that the logs are getting written into the application server’s logs. In JBOSS for example this is server.log. So why is this a bad idea ?
Why not to write into server.log:
- An application server’s log is supposed to be used by the app server and not by your application.
- This log is supposed to contain app server level information like loading war files / exceptions that were handed over to the container etc.
- Weeding through the logs of about 10 applications to find a particular debug / error line is going to be crazy.

I came across another silly little bug today. Take a look at the code below (assume ‘days’ is a parameter and that this snippet is part of a larger function)
Date now = new Date();
long nowMillis = now.getTime();
Timestamp nowTimestamp = new Timestamp(nowMillis);
long future = 3600 * 24 * days * 1000;
Timestamp expiryTimestamp = new Timestamp(nowMillis + future);
System.out.println(nowTimestamp);
System.out.println(expiryTimestamp);
Can you tell what is wrong with it ? What the code intends to do is to set an expiry timestamp to an element – X days from today. A caller called this code snippet with the value 5. Well there is nothing wrong so far. Here is the output
2010-04-27 10:37:10.497
2010-05-02 10:37:10.497
So then came along an element that needed an expiry of around 40 days. The caller calls with the value 40. Guess what the output is
2010-04-27 10:38:43.372
2010-04-17 17:35:56.076