Debugging and Maintenance

Debugging and Maintenance

Firstly, lets answer the question; What is code is for? Code is for humans to read, debug, maintain, and understand. This is the key measure of good code - is it easy to understand what is going on and to maintain.

This is one of the most important measurements of great code verses crap code, and generally under this heading, crap code comes from old C++ developers.

Let me give you two examples of code that does the same thing, and see if you an work out which one is easier to debug:

LatLog location = LocationUtil.getLatLong(db.getCustomer("ABC")
                           .getAddress()
                           .getGetPostcode());

LatLog mapCenter = LocationUtil.getLatLong(postcode);
int mapZoomLevel = 0;

int zoom = 0;
if(location == null)
{
   location = UK_LAT_LONG;
   zoom = 15;
}


 

final Customer customer = db.getCustomer("ABC");
final Address homeAddress = customer.getAddress();
final Postcode postcode = homeAddress.getPostcode();

LatLog mapCenter = LocationUtil.getLatLong(postcode);
int mapZoomLevel = 0;

if(location == null)
{
    mapCenter = UK_LAT_LONG;
    mapZoomLevel = 15;
}

So egnoring all the obvious bug errors lets look at the major signes of bad coding practice.

Inline Code

The most obvious and most anoying thing about the first code snippet is that the entire getting of the customer's location is done in a single line. If there are any faults with this line (such as a NullPointerException), the debugger or stack trace will only identify the line and not the partcular method. However on the second snippet, because the stacktrace will tell you the line number you will know at which point the problem occured.

Untidy Code

Secondly the second snippit is easier to read simply because it's layed out more neat and tidy. The '{' line up which makes it easy to see where blocks start and end, and the indentation makes it easy to see what sections of code belong together.

Nameing of Variables

Code such as:

   Address address = ...

Does not give the reader any indication of what the address vaiable is or used for. A better way is:

    Address homeAddress = ....

 An even better way is:

    final Address homeAddress = ...

Which further indicates that the homeAddress value will not be re-assigned latter on in the code.

Probably the worst example of variable declaration is:

    Address a = ...

Which, once the reader has read past this line, does not even give any idea of the type of values that can be assigned to 'a'

Here is the worst way of writing out the code section above:

String pc = LocationUtil.getLatLong(db.getCustomer("ABC")
                       .getAddress()
                       .getGetPostcode());

LatLog l = LocationUtil.getLatLong(pc);
int z = 0;

if(l == null){
    l = DEFAULT;
    z = 15;
}

99% of code written like this comes from people who first learned to write code in C. Those people really do find it hard to write good clear and understandable code.

Add comment