how to write an easy to debug code
Tags: Programming, Debugging, Coding

It is said that programming is 55% coding and 90% debugging. Your source code has to be easy to read. And also easy to debug. Else if you return to the code years later, you might not recognize what does what.

Let’s look at some practical ways we can write easy to debug code. A code that is easy to debug is also easy to read.

The code examples presented here are in java. Of course these tips also apply to all other programming languages.

describe the class first

/**

* this is a class that contains all

* the properties of a circle. These

* properties are the length, breadth

* and width.

* It also returns the area and

* parameter of the circle

*/

public class Circle

{

private double length;

private double breadth;

private double width;

Before you start writing the methods/functions and variables of the class, first state what the class is about. As you can see from above, some information about class ‘Circle’ has been given above the class.

This information helps you to know that is expected of the class. If there is a bug in your source code. Looking at the class description will tell you whether the error is likely coming from there.

Anytime another developer looks at your code, they will know what to expect from the your class.

all imported classes, APIs and packages at the top



import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;



public class MainActivity

{

Your code needs to be organized. This requires that all the classes, packages and APIs that you need for your code to work remain at the top.

This makes them easy to find. Don’t mess imported code with the main code in your class.

all getter methods at one place

/**

* all getter methods

*/

public double getLength()

{

return length;

}



public double getBreadth()

{

return breadth;

}



public double getWidth()

{

return width;

}

getter methods enable you to get an object’s private data. They are the methods that control what data goes out from the object to outsiders.

They are the methods that allow outside code to interact with the data in this class. Getter methods must be organized so that you can find them easily during debugging.

all setter methods at one place

/**

* all setter methods

*/



public void setLength(double length)

{

this.length = length;

}



public void setBreadth(double breadth)

{

this.breadth = breadth;

}



public void setWidth(double width)

{

this.width = width;

}

we need to talk about setter methods after talking about getter methods. setter methods, as their name implies, allow us to set the value of the class’ member variables.

they allow us to modify the values of these variables also. (variables do not contain values actually. But this statement is made just to simplify things a bit. What variables really do is mentioned later in this article.)

If the setter methods are in a single place, they are easy to find and debug

a curly brace on a single line

/**

* bad code design

*/

public double calculateArea(){

return getLength()*getLength();

} /**

*good code design

*/ public double calculateArea()

{

return getLength() * getLength();

}

Curly braces allow you to know where a block of code begins and ends. Most code-editor software systems expect each curly brace to be on its own line.

This allows them to connect the two related curly braces. The editors draw a vertical line from the beginning curly brace of a class or method/function to the ending curly brace of that class or method/function.

The vertical line helps the programmer to monitor the location of all the code in the class or method easily.

Make the beginning curly brace occupy one line and the ending curly brace occupy another line. Thus making it easy to identify the block of code within the curly braces

comment every method/function

//a method to calculate the area of a square

double calculateArea()

{

return getLength() * getLength();

} //a method to calculate the parameter of a square

double calculateParameter()

{

return (getLength() * 4);

}

The word method is usually used in java. The word function is usually used in javascript and C++ programming languages. Both method and function mean the same thing.

A method or function is a block of code that does only one thing for the class. For example, a function might be declared to perform the task of ‘displaying results to the user’.

Write a comment at the top of the method. This comment tells us what the method does. Also what parameters this method has, if any. This helps you to identify which part of the code is not performing its task well.

a method/function should do only one thing

/**

* bad code design

*/

public double[] calculateAreaAndParameter()

{

double[] results = new double[2];

double area = getLength()*getLength();

double parameter = (getLength() * 2) + (getBreadth() * 2);

results[0] = area;

results[1] = parameter;

return results;

}



/**

* good code design

*/

public double calculateArea()

{

return getLength()*getLength();

}



public double calculateParameter()

{

return (getLength() * 2) + (getBreadth() * 2);

}

a method should do only one thing at a time. If a method performs more than one task, break it into 2 methods.

For example, if a method called ‘calculateAreaAndParameter()’ performs these two functions 1) calculating the area of a square 2)calculating the parameter of a square.

It is better to break this method ‘calculateAreaAndParameter()’ into two. Then each method will do only one thing. For example ‘calculateArea()’ method will only calculate the area of the square. ‘calculateParameter()’ will calculate the paremeter of the square.

This implies that if the value of the area is wrong, then the error is coming from the ‘calculateArea()’ method only

comment every variable declared

private double length; //the length of the square

private double breadth; // the circle's breadth

private double width; // width of the circle

Variables help us to know what information is stored in a computer’s memory.

We give each location in the computer’s memory a name. The name we give this location is called a variable. A variable holds only one location at a time. One variable cannot hold more than one location at a time.

Anytime we call a variable, the variable does this: ‘It tells the computer the location in the computer’s memory that it(this variable) is connected to. The computer looks for that location, and returns the information in that location to us.’

Now for a complex program, we declare a lot of variables. It will be difficult to keep track of the role of all these variables.

That is why we need to comment them. When we declare a variable, we need to remind ourselves why we declared that variable. This way, we know what it does anytime we see it in the future.

Also, we know what variable is not behaving well if the code begins to crash. A short description of the variable is enough to remember the use of the variable.

give your methods a useful name

/**

* bad method name

*/

public String thisIsANewMethod(String parameter)

{

return parameter.toUpperCase();

}



/**

* good method name

*/

public String covertToUpperCase(String userName)

{

return userName.toUpperCase();

}

The name of a method/function should tell us what that method/function does. If the method is to convert every word you give it into UPPERCASE letters,

then the name of the method should be something like ‘convertToUpperCase()’ and not something like ‘helloThisIsAnotherMethod()’

document your code