Develop a good Linux coding style

Source: Internet
Author: User
Article Title: Develop a good Linux coding style. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

The Linux operating system is an open-source operating system. For this reason, you have developed a tool software on the Linux system, including the source code, which may be required by other system administrators. Therefore, when writing code, you must follow certain rules. This is not only for the convenience of others' reading, but also for future maintenance and upgrade considerations. Specifically, I think the Linux system administrator should develop some of the following good coding styles.

I. properly control the left curly braces at the beginning of the function.

According to the encoding style recognized by most system administrators, the left brackets at the beginning of the function are usually placed at the leftmost of the code page. Avoid placing other parentheses (including left braces, left braces, or left square brackets) to the leftmost. This is mainly for ease of reading. Because the body content of a function is usually enclosed in curly brackets. If there are only curly braces representing the function on the far left of the code page, you can see the main body of the function at a glance. This is a good way to improve code readability.

Note that this may be different from the programming style of other languages. For example, on the Java or C Language Platform, braces starting with the function subject are usually placed behind the function. For example, the left curly braces {will be used directly after the main function. However, this is not conducive to the reading of the program, and is not conducive to the Linux system administrator to find the main code of the function. Therefore, if you have experience in other programming languages as a system administrator, it is best to change this writing habit. The author suggests that the system administrator should put the curly braces on the leftmost side and ensure that the curly braces on the leftmost side of the Code are the curly braces of the main body of the Code function.

2. It is best to have a brief description of each function at the beginning.

In the functional code of Linux, each function is composed of functions or programs. That is to say, in a code file, there may be many functions. So what functions are these functions mainly used to implement? If you do not describe it, you will be able to understand this information only after reading all the code of the function. This will cause a great obstacle for others to read the source code. In addition, after a long time, the system administrator may not even know what functions the function is used to implement. This is obviously unfavorable for subsequent maintenance and upgrade. For this reason, the author suggests that system administrators, whether for themselves or for others, should write a short comment at the beginning of each function or program. It is easier to improve the code. In addition, it should be noted that the Linux system may not support Chinese well. Therefore, it is best to write this comment in English. Because in some systems that do not support Chinese well, this Chinese character will be garbled and thus cannot be used properly. If you do not understand English, you may only use Pinyin. Of course, this is just a joke. Generally, Linux System Engineers must understand English. Because the help documentation in Linux is written in English. Therefore, this English language is also a must for system administrators.

In addition, we recommend that you specify the parameters required for the famous function and the results returned. And the number of parameters and results. This is very helpful for code compilation and maintenance. If other members of the project team want to reference your function, they do not need to view the specific code of the function. You only need to view some of the comments to know which parameters need to be passed in. This is also a way to improve the efficiency of project cooperation.

 Iii. Use of If statements must be standardized.

When writing code in Linux, the IF statement is one of the most commonly used structures. This if statement is mainly used to implement logic judgment. Although this statement is relatively simple, it is best to follow some rules when using this statement. Although these rules are mainly taken into account from the perspective of ease of operation, they are also helpful for compiling an accurate IF structure statement.

For example, according to the programming habits of Linux, it is best not to assign values in the IF condition. The IF statement needs to determine the action to be taken based on a certain condition. In this condition, variables can be assigned a value based on the syntax. However, this does not conform to the programming style in Linux. I suggest that you assign values to variables outside the IF structure and then use the variable directly in the condition. This makes it easier to control the IF structure. In addition, IF Nesting is used in the IF statement, you can use curly brackets to enclose the nested if else statement to facilitate the discovery of this nested statement. In other application programming processes, the IF nested structure is highlighted by indentation. However, Linux administrators prefer curly brackets. Although there is no substantial difference between the two, I suggest using curly brackets. This is a common encoding style in the Linux operating system industry.

  4. case-sensitive writing should be standardized.

Although the function name or variable name can contain uppercase or lowercase characters. However, when defining these names, it is better to use the upper and lower cases to comply with some common rules. For example, it is best to Use lowercase English characters for the function name. If there are multiple words, it is best to use underscores to separate them. Instead of using the first English word in uppercase. Case-insensitive mixing may be a good solution in some environments, but not in Linux. For a variable, it is necessary to determine whether to use lower-case characters or upper-case characters based on the variable type. Generally, if the variable is defined by the system administrator, it is better to use lower-case characters. If you use system variables, you can use uppercase letters. In this case, you can determine whether a user-defined variable or a system variable is used when you see whether the variables are in uppercase or lowercase state. Some constants are also required in Linux. According to your programming habits, constants are often defined using uppercase characters. For example, when the system administrator learns the code written by other experts, he can find that in enumeration or macros, uppercase characters are used to represent constants.

Before determining whether to use uppercase or lowercase characters, it must be clear that in Linux, uppercase and lowercase letters are sensitive. That is to say, Name and name represent two different variables. Therefore, it is particularly important to standardize the case format in the system. Because of the Case sensitivity, mixing case sensitivity in the same name can complicate the application of variables or functions. Therefore, if a name consists of several words, the system administrator must use underscores to separate words, rather than using the first word in uppercase.

[1] [2] Next page

Related Article

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.