program diet sehat how to lose 40 pounds in 2 months: Java
Tampilkan postingan dengan label Java. Tampilkan semua postingan
Tampilkan postingan dengan label Java. Tampilkan semua postingan

Senin, 22 Juli 2013

Java Mysteries

If you aren't a computer nerd -- you might want to skip this one.

I have started to use the FindBugs plugin for Eclipse, and it is very nice.  It finds many obscure failures that Eclipse or MyEclipse alone does not.  One particular problem it found in the code that I am attempting to brutalize into usefulness was use of == and != operators for doing String compares.  If you program in Java, you immedately recognize that:

    if (strObject == "hello")

is wrong.  String compares are done with

   if (strObject.equals("hello")

But we had almost 200 string compares using == or !=.  So why do we not have more problems?  The always useful StackOverflow explains why this often works, in spite of being wrong.  Java is smart enough to create a single copy of a String if it is a constant and there is another constant of the same content.  As a result, == and != will work if you are comparing two String objects that are both constants.  Thus, the many compares of the form strObj == "" work because the passed in parameter strObj was initialized somewhere to "".  If for any reason strObj is created in some other manner, such as new String(""), or as the result of some concatenation operation, the use of == or != is not going to work correctly.

As a result, in many cases, these really invalid uses of == and != work -- but it is a hazardous practice!


Kamis, 20 Juni 2013

McCabe Cyclomatic Code Complexity Measure

I recently installed the Metrics plugin in MyEclipse at work.  It provides a great big stack of code complexity statistics -- of which the most easy to understand for non-computer geeks is the McCabe Cyclomatic Code Complexity metric.  This is effectively a measure of how many different paths there are through a particular piece of code. 

Imagine if you lived in a big city, and had to find your way to another spot in the same city.  Every place where you could make a decision of where to turn or go ahead represents complexity, in the same way that the decision points in a programming language represent complexity.  Pretty obviously, the more decision points there are, the more opportunities there are to make mistakes.  A cyclomatic code complexity greater than ten is supposed to be a sign that you need to refactor the code.  So what happens when you see lots of code with complexity measures above 30?  Oh dear.

And when I actually look at some of the functions with high code complexity measures, what do I find?  The equivalent of dropping a mouse at Santa Monica Blvd. and Ocean Avenue in Santa Monica, and telling it to find its way to Boyle Heights in East Los Angeles.

There is a lifetime (perhaps several lifetimes) of work to clean up this pile.