Sunday, April 4, 2010

[Java] How To Make Your Java Code Understandable

 

You may be working on the development of a system at your company. You have been working in coding some module. And, it has been around 6-months since you last worked on that module.

Today, your manager has asked you to make some modifications to that module.

You said to yourself: "Well, that is no problem at all, I have coded most of that module, and really very aware of how it operates".

You fired up your favorite editor. Your fingers began tingling. And, you are ready to the coding.

You headed directly to the required class to start your operation.

"Yes, this is my code. Look who's the author (@author)", you said confidently.

So, let the operation begin.

What is the problem?! Why are you staring like that?!

And it stopped there.

Have you ever gone through such scenario?

So, as you can see, this may happen to us with our own code. But, why is that?

Yes, you may be documenting the code, or having comments for specific parts of the code, but, at the end of the day, you may be wondering why you even did that, what was the reason, and many many other questions that may come to your mind.

If you fall in such circumstance, we can say in brief that the code is NOT understandable.

What good in a code if you or others cannot understand how it works and what it is about.

The case is really sensitive here. Yes, we have to make our code understandable even without commenting.

One way to do that is to make the code clear and obvious to see what is happening.

I think it is now time for an example to make what we talked about so far more clear.

Assume that you have the following line of code:

coffeeShop.placeOrder(1);

Is this code understandable? Well, we can understand that we are placing an order in a coffee shop. But, the question here is: What are we actually ordering? 1?!

So, it is not clear from the code what we are trying to place.

A way to solve this issue is by adding a comment as follows:

coffeeShop.placeOrder(1); // 1 is small cup size

But, we don't want comments to compensate for this poor code. We want the code to be understandable in its own.

A workaround for this would be in defining an enum as follows:

enum CoffeeSize {SMALL, MEDIUM, LARGE }

In this case, the placing order code could look as follows:

coffeeShop.placeOrder(CoffeeSize.SMALL);

Look at the code once again. Isn't it more descriptive? And, thus, understandable?

So, what we learned here is what's so called the PIE (Program Intently and Expressively) principle, and it states the following:

Code you write must clearly communicate your intent and must be expressive. By doing so, your code will be readable and understandable. Since your code is not confusing, you will also avoid some potential errors. Program Intently and Expressively.

Abder-Rahman Ali

References

Practices of an Agile Developer

SCJP, Sun Certified Programmer for Java 6 Study Guide

__._,_.___
Recent Activity:
Visit http://aiaiai.com or http://jgame.org for more groups to join.
Java Official Group is created for the following topics: Java 2 Enterprise Edition - J2EE, Java 2 Standard Edition - J2SE, Java 2 Micro Edition - J2ME, XML, XSL, XSD, XPATH, Web Services, Jini, JXTA for all type of Java Geeks.
Whoever posts spam / ads / job related message will be BANNED IMMEDIATELY
MARKETPLACE

Stay on top of your group activity without leaving the page you're on - Get the Yahoo! Toolbar now.


Welcome to Mom Connection! Share stories, news and more with moms like you.


Hobbies & Activities Zone: Find others who share your passions! Explore new interests.

.

__,_._,___

No comments:

Post a Comment