About program writing style

Source: Internet
Author: User

I've heard people say that in a programmer's programming style, you can tell if the programmer has a lot of experience reading and writing programs. Although not so generalized, but how many can see the importance of programming style. The programming style I'm talking about here doesn't include specific programming ideas, and so on, only refers to the writing style, which is the visual style.

Maybe someone will not understand or Sho Yiku, write the program to write the program, write the program can be correctly and effectively run not on it, what it looks like. In fact, many software companies to write the code of the staff has a mandatory style, such as the provision of where to use the indentation, the length of the bar, variable naming, different functions of the number of empty lines, and so on. The advantage of this is that you can standardize the code for different programmers, facilitate communication and cross modification, and so on.

What is the style of a good programming style? It's hard for me to get the next exact definition, but there are certain points to be sure: The code looks elegant, structured, consistent, refreshing, clean, modular, easy to read, and so on. Below I take my liking the style as the starting point, the detailed introduction.

1. Indented line:

Also known as indentation or a jump lattice. Indentation is one of the important factors that affect the visual effect of code. I often use the keys on the Keyboard "tab" to complete this work, but also some people like to replace it with empty bar once. What are the advantages of "tab" and "Space"? I said each has its own advantages: "tab" key fast and convenient, however, in the editor with different size of the bar, the indentation effect is not the same, and even the indentation is confusing; using spaces to indent, the format is fixed, you can write stereotypes of the code style, but the need for a large number of repeated keystrokes, reducing the encoding speed. My choice is "tab", with a 4-grid jump. This program's visual effect is very good and the typing speed is also fast, as for the performance in different editors, can only rely on the unified jump-length settings to maintain, but some editors provide "tab" and space to replace each other function, but also help maintain the style of the code.

When do I use indented lines? When you need a sense of hierarchy, for example:

void Alert(int i)
{
   while(i > 0)
   {
     Beep(1000, 1000);
     i --;
   }
}
   当集中定义变量的时候,例如:
int   i;
DWORD  dwData;
LPTSTR lpszName;

When there are too many function arguments, for example:

CreateWindowEx(
   0,
   "MyClass",
   "My Window",
   WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
   CW_USEDEFAULT,
   CW_USEDEFAULT,
   CW_USEDEFAULT,
   CW_USEDEFAULT,
   (HWND)NULL,
   (HMENU)NULL,
   hInstance,
   NULL
);

And so on to help improve the readability of the place.

2. Space:

Reasonable use of space can make the program look more refreshing, rather than mess, generally used in these places:

Separating parameters, for example:

TextOut (HDC, ten, "Testing ...", 10);

Assignment statements and expressions, such as:

i = 10 * (123 + 100/5);

if ((a > B) && (C < D) && (E = f))

And so on, where it needs to be clear.

3. Blank line:

The appropriate blank line can make the module structure more obvious. In general there are several situations: 1. Code that completes a relatively independent operation can be gathered together, separated by a blank line before and after; 2. Code with similar functions can be clustered together, separated by a blank line before and after; 3. Similar code can be gathered together, separated by a blank line before and after, 4. Functions and functions are spaced blank lines, and so on.

4. Name:

The case for naming rules is more difficult to unify, and here we can only draw attention to a few issues of principle. It's very important to be consistent, for example, you wrote two functions, one named: Getxpos (), and the other named: Getyposition (), which gives the illusion that these two functions do not belong to the same class, although functionally they are relative And try to do words too literally, if you use CX or Cursorx to describe the position of the cursor, which is easier to understand? In addition, for VC-like MFC programming that has been standardized naming habits of the environment, preferably in accordance with the norms. Finally, if the local programmers, even the development of native applications, it is best not to use Pinyin to name, or in English, do not understand English can look up the dictionary.

Well, other deeper essentials will be summed up in the continuous practice, I hope we can make greater progress.

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.