It was never in my mind to write a blog on a subject like this. I was unable to think of an apt title for this blog. This does not explain any internal details of java or the if statement in particular. Its just that I wanted to share my experience in coding one such statement.
I always thought and also want to put into practice the coding style that I have learnt academically. But is it always true, that we use "good" coding style in our daily work. In this current world, where a new programing language comes up each day, there are very few languages that actually stand up. Though 'C' is the base for most of the languages, I hardly write 'C'. Whenever I have a choice, I go for Java. Its probably the IDEs that "gives me impression" that I can write well in Java.
The experience that I had today, has proved that I do not think while coding. I have something similar to this in my code:
String text;
......
......
if (text.equals("") || text == null){
.........
}
In the snippent above, I have taken great care to make sure that my program does not crash under any circumstance. Though I know, that the string variable will be assigned some value, I still handled the situation that when it is not initialized. I am always proud of myself until, such a program threw an exception and behaving in a weird manner. My program was throwing an exception at the "if" statment. And the exception was a "Null pointer exception". Even the dumbbest in Java would say that the variable is not initialized and thats the reason for this exception. Yes, the variable has not been initialized but I handled the situation to say something. Why does it still crash? I struggled over this for 3-4 hours why this happened. Finally, I could....
I changed the statement as follows;
if (text==null || text.equals("")){
.....
}
And it worked perfectly. I was very happy when it worked. But did not last for a second, as I figured out the reason for it. For the if statment, not always the two expressions (operands) are not evaluated when it is a OR logical operator. The second expression is evaluated based on the result of the first. If the first is true, the second expression is not evaluated since it has to execute the if statement. Unlike the OR operator, for the logical opertor AND, the second expression (operand) is evaluated only when the first expression is true. In this case, the first expression throws an expception and hence it did not check the second.
I was under the silly thought that since the second expression is true, the result is going to be true and the error is handled. And it took me such a long time to figure out!! I answered hundreds of such questions while preparing for GATE and Subject GRE examinations. I remember such a question was also asked severals time in several classes related to object oriented programming. How stupid of me to still commit such a silly mistake even after (theoritically) answering such questions? Had I tought a moment while writing this step, I would have saved 4 hours of precious time...My reluctance to think a second has costed me 4 hours. I am not such a busy person to grieve about the time I spent in fixing it, but my crib was over why I could not apply what I learnt?
Subscribe to:
Post Comments (Atom)
2 comments:
next time try
if("".equals(text)){
--
--
}
Post a Comment