Archive

Posts Tagged ‘java’

The 5 minute fix

August 14th, 2010 CertPal No comments

It was a normal day. The sun rose, birds were singing and everything seemed to be going fine until Kaboom ! A bug appeared in the live environment. Things turned downhill from there that day. But we are more interested in how things got fixed so lets focus on that.

An investigation of the code revealed that the bug had crept in because of a minor mistake. A variable was referring to the wrong column index on a table. ‘Hmmm… simple fix’ I thought. I told my manager it would not take more than 5 minutes to do. ‘Its a one line fix’. Mistake.

There were loads of other things to do after fixing this bug. This is what I ended up doing over the next 4 hours.

  • Analyze if any other code flow would explode.
  • Code the fix – This only took 5 minutes as expected.
Categories: java Tags: , , ,

Java Neuroph tutorial – The code classifier

July 26th, 2010 CertPal 3 comments

An article was written a while back about how neural networks can be used to classify source code. Yes the source code that you write to feed to compilers / interpreters.

The article explains at a high level what method could be used to perform this activity. In the end the author claims some level of success and wonders how other neural-network implementations / techniques would solve the same problem. This got me curious enough and I spent a weekend trying to crack this with Neuroph, the neural network library for java. I present to you my analysis and results below. For the impatient here is the code-classifier DEMO.

Categories: java Tags: , ,

Eclipse tips and tricks – Part 3

June 15th, 2010 CertPal 3 comments

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

Categories: java Tags: , , ,

How to improve your programming skills

June 7th, 2010 CertPal 1 comment

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

  1. Understanding the problem.
  2. Thinking up a solution that covers all use cases.
  3. Executing your thought process through code.
  4. 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.

Categories: java Tags: , ,

More bugs

June 3rd, 2010 CertPal 7 comments

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:

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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:

Categories: java Tags: ,