Tại sao Mã thụt đầu dòng trong quan trọng,en?

Code indentation

Code indentation

Tổng quan: 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.








Giới thiệu:
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

Mô tả:

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

[đang]

public Indentation()

{

// While loop

trong khi (n > 0)

{

System.out.println();

n ;

}

// nếu- else if – else conditional statements

nếu (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;

}

}

[/đang]

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

[đang]

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

nếu (cond1 == true) System.out.println(“Its true”);

// This is also not a good practice – NO

nếu (cond1 == true)

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

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

nếu (cond1 == true){

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

}

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

nếu (cond1 == true)

{

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

}

[/đang]

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.

[đang]

// NOT Recommended

thử nghiệm (tôi, j);

// Recommended

thử nghiệm(tôi, j);

[/đang]

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

[đang]

// NOT Recommended

arr [0];

// Recommended

arr[0];

[/đang]

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

[đang]

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

[/đang]

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

[đang]

// NOT Recommended

count ++;

// Recommended

count ;

// NOT Recommended

tôi –;

// Recommended

tôi–;

// NOT Recommended

++ tôi;

// Recommended

++tôi;

[/đang]

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

[đang]

// NOT Recommended

cho (int j = 0;j < 10;j )

// Recommended

cho (int j = 0; j < 10; j )

// NOT Recommended

getDetails(id,tuổi tác);

// Recommended

getDetails(id, tuổi tác);

[/đang]

All casts must be written without any space

[đang]

// NOT Recommended

(ClassA) m.get(3);

( ClassA )m.get(3);

// Recommended

(ClassA)m.get(3);

[/đang]

All keywords like if, trong khi, cho, switch, and catch should be followed by a single space.

[đang]

// NOT Recommended

nếu(đúng)

// Recommended

nếu (đúng)

// NOT Recommended

trong khi(conut < 7)

// Recommended

trong khi (count < 7)

// NOT Recommended

cho(int j = 0; j < 10; j )

// Recommended

cho (int j = 0; j < 10; j )

// NOT Recommended

bắt(Exception e)

// Recommended

bắt (Exception e)

[/đang]

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

[đang]

class OrderExample

{

// The first entry will be for fields (thuộc tính)

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

}

}

[/đang]

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

[đang]

// NOT recommended

nếu ( (a == Good) && ( (b == better) || (b == best) ) )

// Recommended

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

nếu ( (a == Good) && isbetterbest )

[/đang]








Kết luận: 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.

Tagged on: ,
============================================= ============================================== Mua sách Techalpine tốt nhất trên Amazon,en,Thợ điện CT Hạt dẻ,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Thưởng thức blog này,,en,làm ơn mở rộng vốn từ,,en,techalpine.com/apache-mahout-and-machine-learning,,en? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share