Apr 29 2011

Can You Spot the Infinite Loop?

Can you spot the infinite loop in the following snippet of code?

int i = 0;
}while(i < 5);

The above code caused a critical bug in a application I was working on. At first sight, the code looks okay, especially since it compiles. It’s a do-while loop with a condition that seems that it would break when the variable i is equals or greater than 5. The variable i is set and incremented correctly but unfortunately this causes an infinite do nothing loop. Did you spot the problem? An important keyword is missing from the do-while loop, the do. Let me add comments to explain each statement as the compiler sees it…

// Initialize the variable i
int i = 0;

// Create a block and increment i by one

// Infinite loop with an empty code block
while(i < 5);

The problem is that the while loop can have zero or one code block of statements (where a single statement can be a code block). If the state of what you are testing in the condition does not change because of the test, or because of the statement you are looping over, then you have an infinite loop. In the above code, because of a badly written do-while loop, this loop does not have statements that update the variables in the condition and so this while loop never breaks out.

The correct code would be the following…

int i = 0;
do {
}while(i < 5);

Apr 25 2011

Java String Conversion Puzzlers

In Java, object coercion from one type to another can led to interesting results, and possible bugs when done implicitly. For example, a common type conversion bug is when you have a method that accepts a primitive boolean but pass it an an object of type Boolean. If the object is null, the conversion from a Boolean object to a boolean primitive will cause a NullPointerException.

But mixing between Strings, characters, and integers can result in an interesting mix of results. For example, I think that any Java developer would have some doubt in describing the output from the following Java code.

System.out.println("Hello, "+(char)('A'+1));
System.out.println("Hello, "+('A'+1));
System.out.println("Hello, "+'A'+1);

The one rule to remember about coercion is that if you are performing a calculation with two different types, the least accurate type will be coerced or converted to the more accurate. In this case, adding a character and an integer will result in converting the character to the corresponding integer value before performing the addition operation. Because the integer equivalent of the char value ‘A’ is 65, then ‘A’+1 = 66. When adding between a string, character, and integer the character and integer are converted to strings because concatenating the values into a new string.

"Hello, "+(char)('A'+1); // Hello, B
"Hello, "+('A'+1); // Hello, 66
"Hello, "+'A'+1; // Hello, A1

Mar 8 2010

Top Worst Java Errors

I’ve had my fair share of Java bugs. There is a stage in a programmer’s career that he or she either knows that they rather manage a convenience store or that they can debug common Java errors just by the way the code smells. Here are my list of worst common Java bugs I have been frustrated to solve. These issues have come up more than once, most often in code that I inherited, and had to fix under a time crunch or tight deadline.

I’ve had to fix a few bugs related to the mkdir() method in the File class. The mkdir() method creates or makes a new directory. The mkdir() method only creates one directory. But in many situations, especially if the end user is supplying the path of the directory to be created, you will need to crate a whole new directory hierarchy. Often times you will create a directory and subdirectories at one time. In these situations, you want to use the mkdirs() method instead.

Index Of
Most software bugs are caused by assumptions made by the programmer. One common assumption I see developers making is that file names have only one period to separate the file name from the file extension. It is common to see code that finds the first period and everything after that period is assumed to be the extension. Other assumptions regarding file names include that extensions are three characters long or that the file name does not have special characters. The following code snippet is a dramatization of code I have seen in the field, which incorrectly tries parse the file extension.

String filename = "a.file.path.ext";
int index = filename.indexOf(".");
String extension = filename.substring(index);

A different approach, which would have the desired result is to use the lastIndexOf() instead.

Null Equals
I’ve seen a whole slew of null pointer exceptions due to this sort of Java bug where a possibly null object reference will be used to check the equality of a hard coded constant. Here is the code snippet of what I mean.

boolean equals = maybeNullObjectReference.equals("CONSTANT");

The object reference may be null, so you need to check for null values. I found that if you switch your thinking, you can write less code and still achieve a better solution that having to constantly check if the object is null. A safe approach is to use a object that you know is not null, the constant value.

boolean equals = "CONSTANT".equals(maybeNullObjectReference);

Yet another Java bug that can be easy to miss is the use of the Java equals operator instead of the equals() method. The equals operator returns true if the two object references you are testing point to the same object instance. The equals operator compares the equality of the object references, not the object values but since it reads the same it is easy to overlook.

Map Key
The worst Java bug that I had to ever deal with involved the use of mutable objects as keys in hash maps. The implementation of the Java HashMap is really simple, the hash code of the key is used to locate the bucket in which to store the map entry, which consists of the key/value pair. Once you put a key/value pair in a hash map you should not change the value of the key, ever, in any way that changes the hash code. If the key is changed where it generates a new hash code, you will not be able to locate the correct bucket in the HashMap that contains the key/value pair. I had a scenario where key/value pairs were stored in a HashMap, then the keys where updated generating a new hash code, and there after the value was not able to located.

Sep 10 2009

World’s Most Famous Developer Excuse

I have to say that the World’s most commonly used excuse is “it works on my machine“. Amongst programmers this excuse must rank up there with other famous excuses like “It’s not you, it’s me” and “sorry, I guess I didn’t get reception.” When ever I hear a developer explain away broken builds, compilation errors, or bugs with “it works on my machine” I usually answer with one of the following replies, depending on my mood.

  • Oh, are going to ship your machine to the client.
  • Do you want all of us to work on your machine.
  • Yeah, so fix it on my machine!
You Break It, You Buy It

You Break It, You Buy It

Sep 8 2009

Retweet August 2009

From time to time I just blast tweets about software development, project planning, team dynamics, or whatever else comes to mind. Here is a synopsis of recent tweets and rants. If you want to follow the conversation follow me at techknow and/or juixe and I’ll be sure to follow back.

Software Development

  • sudo gem install mixico; wishing sudo gem install vacation.
  • Version control tools need a better way to diff source history than just diffing two versions of a file…
  • If all bug defects are set to high priority, how are you to know, which one are really really really important ones?
  • I have 20 bugs marked a high priority, but some are higher than others. In addition to the having a priority and severity on defects we need color code too, ‘high pink’ is lower than ‘high crimson.’
  • Just remember, a two line fix can cost you a two hundred thousand dollar deal! Any code change to a production system must be tested.
  • LOL, getting a NullPointerException in a method called initNullValues.
  • Give me open source or give me piracy!
  • Let your users help drive the development of your product, not the accountants.

Team Leadership

  • Sometimes it seems that team meetings are anti-team building!
  • You can still compare apples to oranges, but it is harder to compare apples to some ill-perceived subjective metric.
  • Sometimes people tell you whatever you want to hear, because you are not listening to whatever they are trying to say.
  • People are crazy, and you getting upset about other people’s craziness, will make you crazy.
  • Not losing is not winning.
  • Take your competitors and make them your competitive advantage.

Product Placement

  • I don’t think Apple sponsored #iPhoneDevCamp 3, which is interesting because the iPhone dev camps generate a lot of business for Apple.
  • One feature implemented in many MS products is to restrict other features unless you upgrade from the home to the super business edition.
  • Why does Edible Arrangements require an answer to the following question ‘How did you hear about us?’ before I can place an order?
  • Finally figured out how to cancel @UsWeekly, just typical of a old media company their web site/design/usability makes absolutely no sense!
  • Facebook adds so many obstacles before you can follow someone… It’s like they don’t really want you to have friends.
  • Updating and adding new plugins on my WordPress bloggie! Plugins are like bling for blog!
  • You might have heard of ttyl, maybe of ttyn, but what about ttyat&t? ttyat&t: Talk To You if I get cell reception from AT&T.

Business Planning

  • When you have lemons don’t just make lemonade, make lemon cake too!
  • Having your mommy pay for your business plan is not a business plan.
  • Everyone has a million dollar idea, I have a billion dollar idea, care to invest?
  • Cash in your million dollar idea!
  • If content is king, then context is like emperor.