About Code indent _function

Source: Internet
Author: User

In the question of code style, the most obvious thing to say is the indentation of the code. The so-called indentation, is to the left side of each line of the code to empty part of the length, so that more clearly from the appearance of the logical structure of the program. First, give an example of the correct indentation:

int Kmp_match (char t[], char p[], int flink[], int n, int m)
{
int i = 0, j = 0;

while (I < n)
{
while (J!=-1 && p[j]!= t[i])
j = Flink[j];
if (j = = m–1)
Return (i–m + 1);
i++;
j + +;
}
Return (-1);
}

I'm here to show that the lines of code with nested relationships are indented. As you can see, if you are able to pay careful attention to indentation while writing your code, then errors related to nesting, including braces pairing, are unlikely to occur. Suppose the code is written in this way:

int Kmp_match (char t[], char p[], int flink[], int n, int m)
{int i = 0, j = 0;
while (I < n)
{while (J!=-1 && p[j]!= t[i])
j = Flink[j];
if (j = = m–1)
Return (i–m + 1);
i++;
j + +;
}
Return (-1);
}

You can still see it. What is the specific content of each while loop? Therefore, proper indentation can greatly improve the readability of your code, and here are some basic indentation rules.

structure that needs to be indented

When you encounter a definition of a complex program structure, such as a class, struct, function, or procedure, and enumerations, we usually need to indent its contents one level. In the C/c++/c#/java language, curly braces are a very obvious sign, and when you encounter curly braces, you should associate them directly with indents. In the Basic language, Class/end class, Sub/end Sub, Function/end Function, Structure/end Structure, Enum/end Enum, and so on, with "end" The content in the finished structure should be indented. For example:

public class Myclass:mybase
{
The contents of a class
protected int mhandle;
...
}

Public Class MyClass Inherits MyBase
' Content of the class
Protected Mhandle as Integer
...
End Class

The branching structure (including the if...else structure, the switch/select...case structure, and so on) and the loop structure (including for structure, while/do...while structure, and so on) are nested, so it should also be indented from a clear point of view.

if (a > B)
{
Structure body content of IF clause
max = A;
min = b;
}
Else
{
The structure body contents of the ELSE clause
max = b;
Min = A;
}

If a > B Then
The structural body content of the ' If clause '
max = a
Min = b
Else
The structural body contents of the ' Else clause
max = b
Min = a
End If

Switch (n)
{
Case 0:
When n is 0 o'clock the processing content
...
Break
Case 1:
When n is 1 o'clock the processing content
...
Break
Default
...
}

Select Case N
Case 0
' When n is 0 o'clock the processing content
...
Case 1
' When n is 1 o'clock the processing content
...
Case Else
...
End Select

for (i = 1; I <= i++)
{
For the Loop body
s + + data[i];
T *= Data[i];
}

For i = 1 to 100
' For The Loop body
s = s + data (i)
t = T * data (i)
Next

i = 0;
while (Data[i]!= 0)
{
The loop body of while
s + + data[i];
T *= Data[i];
i++;
}

i = 0
While data (i) <> 0
' While the loop body
s = s + data (i)
t = T * data (i)
i = i + 1
Wend

Indented format

In the compiler, indents are usually controlled by the TAB key, which usually corresponds to 2 or 4 spaces. Indents at the same level must be strictly aligned.

In the curly braces syntax, there are several different ways in which curly braces are written, which, in the opinion of the individual, is the clearest way to use a single line of left and right brackets, since the pairing of parentheses can best be reflected. Some of the other formulations are:

Put the opening brace at the end of the structure line
int Sum (int x, int y) {
function content
int s;
s = x + y;
return s;
}

Write the opening brace with the first statement in the structure
int Sum (int x, int y)
{int S;
s = x + y;
return s;
}

Special Branch Loop structure (single-line content)

In many languages, when there is only one statement in the structure of a branch structure (such as if) and a loop structure (for example, a for), the writing is slightly different from the inclusion of multiple statements. In the c/c++/java/c# language, curly braces can be omitted, at which point we should still indent according to its logical relationship:

if (a > B)
max = A;
Else
max = b;
Other statements
printf ("%d", max);

for (i = 1; I <= i++)
s + + data[i];
Other statements
printf ("%d", s);

i = 0;
while (Data[i]!= 0)
s + + data[i++];
Other statements
printf ("%d", s);

In Basic, if or Else there is only one statement, it can be written in the same line as If/else without the end if structure. However, that is the old-fashioned way of writing, and in the modern sense, it is recommended to write a branch and use end If.

Long Statement Wrapping Line

Sometimes, a statement can be very long, but beyond the boundaries of the screen. In this case, although you can not change the line, but it looks very inconvenient. Often, we wrap long statements according to the width of the screen. After the line break, to indent a grid relative to the original statement:

This is a very long statement:
MyVar = myvar1 + myvar2 + myvar3-myvar4-myvar5 * MYVAR6 * MYVAR7/
MYVAR8/MYVAR9 + myvar10 + myvar11-myvar12–myvar13 * myvar14 *
Myvar15/myvar16;
The following statement restores the normal indent position

When changing lines, we usually wrap the line before a variable or constant, leaving the delimiter such as commas and operators at the end of the previous line.

When a function is called, if the number of arguments, or the expression to be passed is long, it also involves the long statement wrapping problem, the most basic principle is: as far as possible between parameters and parameters of the line, and leave the comma at the end of a line:

Wrap between parameters and arguments
printf ("This function called has many parameters.") %d%d%d%d/n ", MYVAR1,
Myvar2, MYVAR3, MYVAR4);

In some cases, in order to clearly list each parameter of a function, we can use a similar structure declaration:

Each parameter branch
printf
"This function is called has many parameters. %d%d%d%d/n ",
MYVAR1,
MYVAR2,
MYVAR3,
Myvar4
);

This is especially clear when the incoming expression is more complex. Similarly, when declaring multiple variables of the same type at the same time, you can write as well:

Double Myvar1,
Myval2,
MYVAR3,
MYVAR4;

Its primary purpose is to make it easier to annotate a single variable:

Char USERNAME[MAXN],//user name
PASSWORD[MAXN],//password
DESC[MAXN]; Describe

Here are some of the more common things that are summed up, they are not the golden rule, but a suggestion, but a strong one. When you encounter a situation not mentioned in this article, you can adjust the indentation according to the actual situation and needs. The bottom line is to reflect the logical structure of the code more clearly and correctly.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.