Missing image

Smelly Code – Confessions of a Java Noob

I find the “code smell” topic to be pretty fascinating, which is rather geeky of me to say… but I guess I’m one of those people that tend to learn from mistakes. I’ve learned it’s much better to learn from OTHERS’ mistakes though! Here are some interesting points of “code smells” I’ve come across that I could probably use some work on with my Java / OOP PHP,

From: http://www.codinghorror.com/blog/2006/05/code-smells.html

Lazy Class: Classes should pull their weight. Every additional class increases the complexity of a project. If you have a class that isn’t doing enough to pay for itself, can it be collapsed or combined into another class?

(I tend to make a class with the anticipation of needing if for a number of reasons in the future during my planning, but then end up only using it as a sort of “stepping stone” and have other classes do my heavy lifting. I could get rid of some of the getters/setters I create for example, but feel reluctant to, because I think to myself, “Well… I’ll just leave it in there. I might need it for some reason eventually…”, when I usually don’t.)

Feature Envy: Methods that make extensive use of another class may belong in another class. Consider moving this method to the class it is so envious of.

(During my initial designing of the application, I tend to compartmentalize what tasks need to happen by creating multiple methods to do each thing individually. By the time I’m over halfway finished, I realize that some of my methods are doing very much the same thing. If I were to just add an if statement to one method, I could eliminate another completely.)

Indecent Exposure: Beware of classes that unnecessarily expose their internals. Aggressively refactor classes to minimize their public surface. You should have a compelling reason for every item you make public. If you don’t, hide it.

(OOP is still kind of new to me, so the idea of “data hiding” seems unnatural. In fact, I’ve gone out of my way to create function libraries into a single “include file” in PHP, and added the include statement to each of my web page “header” templates so I can access code on the all the pages of the site, “just in case”. I’m starting to see the benefits of the “private” methods/classes as a means of controlling the “flow” of my Java / OOP applications so that I can quickly say “oh, that’s a private method, so my other method can’t be changing the value there… where else could it be happening?” It’s providing me with a new way of thinking when I develop applications entirely. It’s a bit more cumbersome in the design process, but the end result is quite a bit more “fluid” and elegant.)

From: http://en.wikipedia.org/wiki/Code_smell

In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem. Code smells are usually not bugs—they are not technically incorrect and don’t currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future.

(I had a great example of PHP code smells, as I was often tasked having to go back and rework 3 other developers’ code for a maintenance and upgrade project for an employer. It wa a Web Portal that POUNDED on a SQL database by taking field level values of a resultset of a SQL query, and creates new queries from that resultset, and sends NEW queries on different JOINed tables/views to produce reporting. The database servers capacity and internet pipe were PATHETIC for what the company was trying to accomplish, so optimization was rather critical in this case. The “too many cooks in one kitchen” code was thrown together to make an impossible deadline, and with little to no business requirements. It was a disaster to unravel… BUT… it DID work! It was slow during peak usage times, but it was fully functional for the most part. Did it have a bunch of bugs? No… not really. It just lacked design, structure and consistency. It was in serious need of some code deodorant though… I was been able to write new SQL queries that consolidate all the bits and piece that were added and “ad-hoc”ed to stream line single database connections and resultsets instead of pounding away with 3000-5000 database connections opening and closing in foreach loops to cut down on the smells though.)

And lastly, From: http://c2.com/xp/CodeSmell.html

Under the heading: Not enough code, better put the half-baked code back in the oven a while:

I found this to be familiar:

Explicitly setting variables to null. Can indicate that either there are references to things that this code has no business referencing, or the structure is so complex that the programmer doesn’t really understand it and feels the need to do this to be safe.

I’ve always believe that an ounce of prevention is worth a pound of cure. Sometimes I will expect to need something, but either later on I forget what I created it for, or I feel the need to assign it a value so I know that I’m not trying to do something like calculate or concatenate with a NULL value with the legendary PHP check:

// PHP syntax
if (is_null($some_junk) == false) {
echo "Do you want to go play pool or ".$some_junk."?";
} else {
echo "I don't have a thing to wear tonight.";

The more I find a useful implementation to practice my Java with, the better I will get at it. Hopefully, much like I have with Perl/PHP, I will get better at the planning and design process and prevent such smelly code I’ve laid thus far as a Java noob.



Leave a Reply