Why code indentation in important?

Code indentation

Code indentation

Overview: Indentation is one of the most important aspects in any programming domain. But this is often the most neglected part during the programming and the developers are mostly reluctant to follow it. In my experience some of the most complex projects are become uncontrollable due to ONLY bad code indentation and wrong programming practice. We need to understand that programming is not only to solve the problem but it is also an art of coding. And the most important part is that, as a developer you must be passionate about coding and then only you will be able to include the art of coding in your development work.








Introduction:
Programming style and indentation can be defined as the way you follow to organize and document your source code. Code indentation is a part of style and it is more of aesthetic interest. If we follow proper style guide and indentation then a program can be just like a POEM. And the reader will be comfortable enough to SAIL through it and understand the meaning. As we know a proper code indentation will make it

  • Easier to read
  • Easier to understand
  • Easier to modify
  • Easier to maintain
  • Easier to enhance

Description:

The purpose of code indentation and style is to make the program more readable and understandable. It saves lots of time while we revisit the code and use it. A style guide provides a road map which the developer should follow and implement in coding. So in a group of developers, all the code generated will be of consistent in nature and reusable by any coder/developer. The worst part of coding is not to follow the style and indentation guide when the project is in the mid way of development. Some time it happens due to time pressure and many other factors. But we need to keep in mind that good code is always better even if we need to give some more time because in the long run it will save a lot of time. But unfortunately, it is often forgotten and the trouble comes at a later stage. And in that stage it is very difficult to rectify and recover.

As we know, a program is written only once during coding but it is read many times at a later stage. Following are some future points when we refer the code again.

  • During reading an understanding the program
  • During code debugging
  • During adding new code to the existing program
  • During updating the existing program

Now in the next section we will discuss different sections of formatting. Formatting can be broadly categorized in the following sections

Indentation:

The indentation comes first in formatting section. The first important thing is indentation is done with SPACES not TABS. As we know all programs work good with SPACES and most important part is SPACE is of fixed length in all types of system. But TAB is not of fixed length. So if your TAB is set to 6 then it might be different in other’s system. So if some one is having TAB setting as 8 then your code will look different and messy. So during programming we should not mix TAB with SPACES, rather we should only use spaces.








The following code example shows up a code with proper indentation made by spaces. The matching braces should be in the same column. It means the start and close brace should have the same column location.

Listing 1: The following sample shows indentation in different constructs

[code]

public Indentation()

{

// While loop

while (n > 0)

{

System.out.println();

n++;

}

// if- else if – else conditional statements

if (Cond1 == val1)

{

System.out.println(“Cond1 is val1”);

}

else if (Cond1 == val2)

{

System.out.println(“Cond2 is val2”);

}

else

{

System.out.println(“No condition is satisfied”);

}

// Switch construct

switch (Val1)

{

case 1:

System.out.println(“This is Java”);

break;

case 2:

System.out.println(“This is C++”);

break;

case 3:

System.out.println(“This is Oracle”);

break;

default:

System.out.println(“This can be anything”);

break;

}

}

[/code]

As you can see in the above example all the constructs are closed with proper braces, even if they just have a single line of statement. So it is a best practice to follow consistency to make the code more readable and maintainable. In addition, for future addition or enhancement this coding practice is very helpful. The code is properly marked and hence easy to add new code in the existing constructs. Following are some examples which show the proper use of braces in different types of constructs.

Listing 2: Following code sample shows best practices

[code]

//This should not be used even if single line is there – NO

if (cond1 == true) System.out.println(“Its true”);

// This is also not a good practice – NO

if (cond1 == true)

System.out.println(“Its true”);

// The opening brace shoud not be in the same line – NO

if (cond1 == true){

System.out.println(“Its true”);

}

// This is the perfect way to use braces – YES

if (cond1 == true)

{

System.out.println(“Its true”);

}

[/code]

Spacing: Spacing forms the second important part in code indentation and formatting. It also makes the code more readable if properly maintained. So we should follow proper spacing through out our coding and it should be consistent.

Following are some examples showing the usage of spacing in code indentation.

Listing 3: Following code sample shows the spacing best practices

Left parenthesis should start immediately after the method name.

[code]

// NOT Recommended

test (i, j);

// Recommended

test(i, j);

[/code]

All array names should be immediately followed by a left square bracket.

[code]

// NOT Recommended

arr [0];

// Recommended

arr[0];

[/code]

All binary operators should maintain a space on either side of the operator.

[code]

// NOT Recommended

a=b-c;

a = b-c;

a=b – c;

// Recommended

a = b – c;

// NOT Recommended

z = 6*x + 9*y;

// Recommended

z = 6 * x + 9 * y;

z = (7 * x) + (9 * y);

[/code]

All unary operators should be written immediately before or after their operand.

[code]

// NOT Recommended

count ++;

// Recommended

count++;

// NOT Recommended

i –;

// Recommended

i–;

// NOT Recommended

++ i;

// Recommended

++i;

[/code]

All commas and semicolons must be followed by single white space.

[code]

// NOT Recommended

for (int j = 0;j < 10;j++)

// Recommended

for (int j = 0; j < 10; j++)

// NOT Recommended

getDetails(id,age);

// Recommended

getDetails(id, age);

[/code]

All casts must be written without any space

[code]

// NOT Recommended

(ClassA) m.get(3);

( ClassA )m.get(3);

// Recommended

(ClassA)m.get(3);

[/code]

All keywords like if, while, for, switch, and catch should be followed by a single space.

[code]

// NOT Recommended

if(true)

// Recommended

if (true)

// NOT Recommended

while(conut < 7)

// Recommended

while (count < 7)

// NOT Recommended

for(int j = 0; j < 10; j++)

// Recommended

for (int j = 0; j < 10; j++)

// NOT Recommended

catch(Exception e)

// Recommended

catch (Exception e)

[/code]

Class Member Ordering: Now we will discuss the ordering of class members as it also forms a part of code indentation and formatting. The members inside a class should be followed a proper order as shown below.

Listing 4: Sample showing class member ordering

[code]

class OrderExample

{

// The first entry will be for fields (attributes)

int a;

int b;

int c;

// The second entry should be class constructors

OrderExample()

{

// write initialization entries

}

// The third entery should contain methods

getOrder()

{

// Write method details

}

}

[/code]

Maximum Line Length: 

Maximum line length should not exceed 120 characters. If it needs to be increased then put it in a different line. The reason behind it is due to the capability of editors and printing facilities. In general, normal editors and printers can handle 120 characters comfortably. So if it exceeds the limit then it’s become a problem to handle.

Self Documenting Code:

Many a times we have heard that each and every piece of code should be documented properly so that it is easily understandable. And we start writing details of the method on top of it. But this is not the best practice. Because the code might be changed in future and the programmer forgot to change the documentation. So the best practice is to write the code in a way so that it can explain itself without any comment.

Listing 5: Following sample shows the self documenting code

[code]

// NOT recommended

if ( (a == Good) && ( (b == better) || (b == best) ) )

// Recommended

boolean isbetterbest = ( (b == better) || (b == best) );

if ( (a == Good) && isbetterbest )

[/code]








Conclusion: So far we have discussed different aspects of code formatting and indentation. We have also understood the importance of writing good code with best indentation and formatting practices. We need to remember that a proper indentation rule must be followed while coding so that the program is easily readable and maintainable in future. So we can conclude that indentation and formatting is an important part of programming practice and developers should follow It from the starting of their programming career.

Please follow and like us:
0
Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *


9 × = 54

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+
http://techalpine.com/why-code-indentation-in-important">
LinkedIn