Programmer's path-code style

Source: Internet
Author: User

The excellent code style is like a decent dress, which can give a good impression. A beginner must first establish good programming habits, including the Code style. This article discusses several key issues in the Code style and provides an excellent code as a style template. You don't have to spend too much time researching the code style. Instead, you can easily write "professional code" by constantly imitating the template code ".
1. 80 characters, code line limit
No matter how time and space change, how the world changes, a line of 80 characters should always be remembered. The old Unix terminal displays text in 80 columns. To make the source code and manual readable, Unix always sticks to the tradition of 80 columns. There are not many 80 columns. It is enough to write a meaningful line of code, and it is also enough to be displayed on the terminal screen and printed on A4 paper. Even today, our screen resolution is already high enough, and the content displayed on one line is far more than 80 characters, however, our excellent tradition has been formed-almost all Unix/Linux kernel source code and online user manuals strictly abide by the 80-Column Limit. If you are using Dev C ++ on Windows, have you noticed the thin gray vertical line in the code editing box? Yes, that is the limit of the code line. In addition to lengthy and complex Markup languages such as HTML and XML, almost all languages must strictly abide by the limit of code lines, this includes C, C ++, Java, C #, Python, PHP, and so on. But sometimes, for example, when PHP is dealing with HTML, this restriction can be temporarily relaxed. Too long lines of code are always bad, and good code should always be slim.
2. tab or space: Different indentation methods
The Code cannot be separated from indentation. There are two main arguments about indentation. One is whether space should be used or tabs, and the other is whether it should be indented with 4 cells or 8 cells or even not.
Let's talk about space and tab. Programmers who insist on using space will tell you that if you never use tab, your code will be the same in all places. Yes, this is the advantage of using space indentation. Unfortunately, this is its only advantage. The more code hierarchies, the more indentation at the beginning of the inner code, which means you need to enter spaces many times. Even if you can barely press the Space key until it breaks down, you will be troubled by IDE's clever insertion of some tab characters. We recommend that you always use tab indentation because almost all code (not just C code) uses tab indentation.
Is the tab 4 or 8? This is the root cause of tab indentation criticism. The tab size used when you write a program is different from the tab size used when others read the program, and the beautiful layout will become messy. The standard tab is 8 cells, but unfortunately, almost all Ides on Windows platforms, including Visual Studio, Dev C ++, and even cross-platform eclipse, by default, four tabs are used. All kernel source code of the FreeBSD system I use adopts 8-lattice indentation, so I have always insisted on using 8-lattice indentation. Maybe you are not used to a large margin. If it is not on a UNIX platform or a C language, use a 4-grid tab. If you write C code in UNIX, it is better to use standard tabs of 8 cells.
3. Discount principle, easy to be ignored
Since the limit of code lines exists, we have to break a complete line of code in many cases, which leads to the question: how should we indent the lines? A good practice is to increase the tab space by 1/2 on the basis of the original indent after the first line is folded, and all the subsequent lines are aligned with the second line. This text description may be too obscure. For example, let's take the 8-lattice indentation as an example ):
If (value> A & value> B & value> C & value <D & value <E & value <F
Value <H & value Value = value + 1;
Value = value * value
* Value + value * value
* Value;/* Note the indentation after the line is folded again */
}
Obviously, this code segment is useless, but it is fabricated to illustrate line indentation.
4. ubiquitous space and ubiquitous blank lines
Spaces are required:
1) The keywords such as if, while, and switch are in the left brackets (.
2) left curly braces {before.
3) Two Sides of the binary operator, for example, P = NULL.
4) comma (,) and semicolon (;), for example, for (I = 0; I <10; I ++ ).
Do not place spaces in the following locations:
1) function name and left parentheses (including macro with parameters and left parentheses (such as Max (A, B ).
2) Semicolon; and Colon: before.
3) left brackets (right), for example, if (P = NULL ).
The following locations are required for empty rows:
1) before and after the Function Definition
2) A group of closely related code segments before and after
These rules are not complete. When you encounter situations not listed above, please refer to the template Code provided in this Article.
5. controversy over left curly braces-line feed? Nothing else?
This is another question of benevolent wisdom. From the perspective of making the code clearer, the left curly braces that are marked at the beginning of the code segment {should start with another line:
If (P = NULL)
{
Printf ("error! \ N ");
Exit (0 );
}
However, this seems to be not compact enough, so most of the C code (at least on UNIX) adopts this method:
If (P = NULL ){
Printf ("error! \ N ");
Exit (0 );
}
I suggest using the latter, which will make your code more compact and professional. We need to specify a special case. When defining a function, we always need to wrap the left curly braces {line feed:
Static int
Maxint (int A, int B)
{
Return A> B? A: B;
}
6. Beautiful and flexible treatment
The Code style is far more than the five points mentioned above. In fact, many companies have prescribed Code styles, including naming rules and indentation rules. If you are in a development team, you should always focus on the standards of the team, and if you are only learning the C language and trying to form your own code style, I will give you the most professional source code here for your reference. If you have any questions, you can find the answer in this Code: Code style (note that the tab is set to 8 when viewing ). This Code comes from the excellent FreeBSD operating system kernel source code (version 6.2), and you will not doubt its authority. For more information, see the FreeBSD kernel code writing specifications. You may need some patience to read this English document.

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.