Code Inspection

I recently got the chance to participate in another code inspection, in which among other things, the following line of code was heavily criticized for using String concatenation on constants and not using a StringBuffer object for the string operation in general.

FindFile.FindFileInClasspath( kPropFilePrefix + "*"+ kPropFileSuffix + kKeyProperties, vPropFiles );

This is from a J2EE project and the code is used in a background thread and executes every couple of seconds.

Since I also do some work in the field of embedded software development, which usually involves memory, processing speed, and power constraints, I can really appreciate code level optimizations. However, this is not a MIDlet project nor does the code run all that often. Moreover, the concatenation really does add some clarity to what the code is doing and what the parameter’s value is when the static method is called.

Now, to satisfy the optimization junkies, I have created the following small sample:

Compiling followed by disassembling shows the following code:

C:\temp\test>javap -c Test

If you look at the disassembled code you will find out that the compiler combines the String constants into one and also uses a StringBuffer for the concatenation that needs to be done at runtime.


Share this post:

Leave a Reply