Program Writing Style

Source: Internet
Author: User
I have heard of such a sentence. Through a programmer's programming style, I can see whether this programmer has much experience in reading and writing programs. Although this cannot be generalized, the importance of the programming style can be seen. The programming style I mentioned here does not include specific programming ideas. It only refers to the writing style, that is, the visual style.

Some people may not understand or ignore it, but write the program when writing the program. The written program can run correctly and effectively, regardless of what it looks like. In fact, many software companies have rigid rules on the style of code written by employees, for example, it specifies where to use the contraction, the length of the hop key, the variable naming method, and the number of empty lines between different function codes. The advantage is that the Code Compiled by different programmers can be standardized to facilitate communication and cross-modification.

So what is a good programming style? It is difficult for me to define an accurate definition, but there are several points for recognition: the compiled code looks elegant, hierarchical, consistent, fresh and clean, the module is clear, easy to read, and so on. Next I will give a detailed introduction based on my preferred style.

1. Contraction:
It is also called indent or skip. Contraction is one of the important factors that affect the visual effect of the Code. I usually press the "tab" button on the keyboard to complete this job. Some people prefer to replace it with space. "Tab" and space are superior or inferior? I said each has its own advantages: the "tab" Key is fast and convenient, but in the editor with different hop length settings, the effect of the contraction is different, and even the contraction is messy; the format is fixed and can be written into the finalized code style. However, a large number of repeated buttons are required to reduce the encoding speed. My choice is the "tab" key, with a hop length of 4 cells. In this way, the visual effect of the program is good and the typing speed is fast. As for the performance of different editors, it can only be maintained by setting the uniform hop length. However, some editors provide the function of replacing tabs and spaces, which also helps maintain the code style.

When should I use the scale-in? When you need a sense of attention, for example:

Void alert (int I)
{
While (I> 0)
{
Beep (1000,100 0 );
I --;
}
}

When variables are defined in a set, for example:

Int I;
DWORD dwdata;
Lptstr lpszname;

When there are too many function parameters, for example:

Createmediawex (
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.

2. Space:
The rational use of spaces can make the program look more fresh, rather than messy, generally in these places:

Separate parameters, for example:

Textout (HDC, 10, 10, "testing...", 10 );

Assignment Statement and expression, for example:

I = 10*(123 + 100/5 );
If (A> B) & (c <D) & (E = f ))

And so on.

3. Empty rows:
A proper empty line can make the module structure more obvious. There are several situations: 1. code that completes a relatively independent operation can be grouped together and separated by blank lines; 2. code with similar functions can be grouped together and separated by blank lines; 3. similar code can be aggregated and separated by blank lines. 4. an empty line is required between a function and a function.

4. Name:
Naming rules are even more difficult to unify. Here, we can only remind you to pay attention to several principles. This is very important. For example, if you write two functions, one is getxpos () and the other is getyposition (), which gives you the illusion: these two functions do not belong to the same class. Although they correspond to each other in terms of functionality, they are as simple as possible. Which of the following statements can be easily understood if CX or cursorx is used to describe the cursor position? In addition, we recommend that you follow the naming conventions of the MFC programming environment like VC. Finally, if you are a local programmer, even if you are developing a local application, you 'd better not use Pinyin to name it. Instead, you can use English to look up the dictionary.

Well, other more in-depth essentials will be summed up in constant practice. I hope you can make more 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.