MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Importance of code indentation

In this article we will see the importance of code indentation in software development.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

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.

Code Indentation

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

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 leter 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

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;
    }
}

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

//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");
    }  

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.

Left parenthesis should start immediately after the method name.

Listing 2: Following code sample shows the spacing best practices

    // NOT Recommended
    test (i, j); 
    
    // Recommended
    test(i, j);  
    

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

    // NOT Recommended
    arr [0];  
    
    // Recommended
    arr[0];   
    

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

    // 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); 
   

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

    // NOT Recommended
    count ++; 
    
    // Recommended
    count++;  

    // NOT Recommended
    i --;    
    
    // Recommended
    i--;     
    
    // NOT Recommended
    ++ i;
    
    // Recommended
    ++i;
   

All commas and semicolons must be followed by single whitespace.

     // 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); 
    

All casts must be written without any space

     // NOT Recommended
    (ClassA) m.get(3);  
    ( ClassA )m.get(3); 
    
    // Recommended
    (ClassA)m.get(3);   
    

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

     // 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)
     

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 3: Sample showing class member ordering

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
        }
    }

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 4: Following sample shows the self documenting code

    // NOT recommended
    if ( (a == Good) && ( (b == better) || (b == best) ) )

   // Recommended
    boolean isbetterbest = ( (b == better) || (b == best) );
    if ( (a == Good) && isbetterbest )

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.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login