Why code indentation in important?

Code indentation

Code indentation

Преглед: 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.








Увод:
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 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

[код]

public Indentation()

{

// While loop

while (н > 0)

{

Систем.оут.принтлн();

n ;

}

// ако- else if – else conditional statements

ако (Cond1 == val1)

{

Систем.оут.принтлн(“Cond1 is val1”);

}

else if (Cond1 == val2)

{

Систем.оут.принтлн(“Cond2 is val2”);

}

else

{

Систем.оут.принтлн(“No condition is satisfied”);

}

// Switch construct

switch (Val1)

{

case 1:

Систем.оут.принтлн(“This is Java”);

break;

case 2:

Систем.оут.принтлн(“This is C ”);

break;

case 3:

Систем.оут.принтлн(“This is Oracle”);

break;

default:

Систем.оут.принтлн(“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

ако (cond1 == true) Систем.оут.принтлн(“Its true”);

// This is also not a good practice – NO

ако (cond1 == true)

Систем.оут.принтлн(“Its true”);

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

ако (cond1 == true){

Систем.оут.принтлн(“Its true”);

}

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

ако (cond1 == true)

{

Систем.оут.принтлн(“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.

Listing 3: Following code sample shows the spacing best practices

Left parenthesis should start immediately after the method name.

[код]

// NOT Recommended

тест (ја, j);

// Recommended

тест(ја, 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 – ц;

// Recommended

a = b – ц;

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

ја –;

// Recommended

ја–;

// NOT Recommended

++ ја;

// Recommended

++ја;

[/код]

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

[код]

// NOT Recommended

за (int j = 0;j < 10;j )

// Recommended

за (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, за, switch, and catch should be followed by a single space.

[код]

// NOT Recommended

ако(прави)

// Recommended

ако (прави)

// NOT Recommended

while(conut < 7)

// Recommended

while (count < 7)

// NOT Recommended

за(int j = 0; j < 10; j )

// Recommended

за (int j = 0; j < 10; j )

// NOT Recommended

улов(Exception e)

// Recommended

улов (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 4: 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 5: Following sample shows the self documenting code

[код]

// NOT recommended

ако ( (a == Good) && ( (b == better) || (b == best) ) )

// Recommended

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

ако ( (a == Good) && isbetterbest )

[/код]








Закључак: 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.

Таггед на: ,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share