How to develop a good Linux coding style

Source: Internet
Author: User

The Linux operating system is an Open-source operating system that you develop on a Linux system, including source code, which may be needed by other system administrators. To do this, you need to follow certain rules when writing code. This is not only for the convenience of others to read, but also for their own maintenance and upgrade considerations. Specifically, the author believes that the Linux system administrator to develop some of the following good coding style.

First, reasonable prevention of the opening of the function of the left curly braces.

Based on the coding style approved by most system administrators, the opening parenthesis at the beginning of the function is often placed at the far left of the code page. To avoid placing additional parentheses (including left curly braces, opening parentheses, or left brackets) to the left. This is mainly to facilitate reading. Because the body content of a function is often surrounded by a pair of curly braces. If you have only the curly braces representing the function at the far left of the code page, you can see the body of the function at a glance. This is a good way to improve code reading.

It should be noted that this may differ from the programming styles in other languages. As in the Java language or C language platform, the curly braces at the beginning of the function body are often placed behind the function. If you use {This left curly brace directly after the main function. However, this is not conducive to the reading of the program, the Linux system administrator to find the main code of the function. For this reason, if you have a system administrator with experience in other programming languages, it's best to change this writing habit. The author recommends that the system administrator still put the curly braces on the left and ensure that in the entire code, the left-most curly braces are the curly braces of the body of the code function.

Second, each function at the beginning of the best have a brief description of the function.

In the Linux functional code, its various functions are also a function or program composition. In other words, in a code file, there may be many functions. So what functions do these functions primarily implement? If you do not make any explanation, you will not be able to understand this information until you have read the full code of the function. This can be a big obstacle for others to read the source code. And, after a long time, it may not even be possible for the system administrator to realize what this function is for. This is obviously detrimental to its subsequent maintenance and upgrades. To this end, I suggest that the system administrator, whether for themselves or for others, it is best to write a short note at the beginning of each function or program. A good memory is better than a bad pen, which improves the readability of the code. It is also important to note that since the Linux system may not be very good for Chinese support, in writing this note, it is best to write in English. Because in some of the Chinese support is not very good system, this Chinese will be displayed as garbled, this time will not be able to play a role. If you do not understand English, then may only use pinyin. Of course it's just a joke. General Linux System engineers need a certain understanding of English. Because the help documents in the Linux operating system are written in English. So this English language is also the system administrator must solve the gateway.

Also in the description of the function, it is best to be famous This function requires the user to pass what parameters, will return what kind of results. And the parameters, the number of results, and so on. This is very helpful for compiling and maintaining code. and other members of the project team would not need to look at the specific code of the function if they were to refer to your function. Instead of just looking at some of the comments, you know what parameters need to be passed in. This is also a means of improving the efficiency of project cooperation.

Third, if statement use to specification.

When writing code in a Linux system, the IF statement is one of the most used structures. This if statement is used primarily to implement some logical judgments. Although the statement itself is relatively simple, it is best to follow some rules when using this statement. Although these rules are primarily considered in terms of readability, it is also helpful to write an accurate if structure statement.

As with the programming habits of Linux systems, it is best not to assign values in the condition of If. An if statement needs to determine what action to take based on a condition. In this condition, the variables can be assigned in this condition according to the syntax. But this does not conform to the programming style of the Linux system. The author suggests that it is best to assign a variable outside the if structure and then use the variable directly in the condition. This is easier to control if structures. In addition, if you use nesting in an if statement, you can use curly braces to enclose nested if else statements to facilitate the discovery of this nested statement. In other applications, the indentation is used to highlight the if nesting structure. But Linux system administrators prefer to use curly braces. Although there is no substantial difference between the two, but I recommend the use of curly braces. Because this is a code style universally accepted by the Linux operating system industry.

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.