Saturday, October 23, 2010

c language programming style



Chapter I: indentation

Tab is 8 characters, thus indentations are also 8 characters. There are many strange style, they indentation is defined as 4 characters (set to 2 characters!) Depth, which is like trying to PI defined as 3 as unacceptable.

The reasons are: the size of indentation is to clearly define the beginning and end of a block. Especially when you have spent in front of the computer more than 20 hours later, you will find a large indentation makes your program understanding easier.

Now, there are some people that use 8-character indentations makes the code from the right side close to the 80-character terminal screen width is difficult to be watching proceedings. The answer is, but your program has more than 3 indented time, you should modify your program.

In short, 8-character indentations makes the program easy to read, there is an additional advantage is that it can become nested layers you will program the time to give you a warning too. This time, you should modify your program .

Chapter II: The position of symbols

Another issue of C programming style is to brace treatment. Indentation with different sizes, there is little reason to choose one instead of another style, but there is a recommended style, it is the Kernighan and Ritchie's The classic book brings, it will start

Braces on the line last, and will end brace on the first line, as follows:

if (x is true) (we do y)
However, there is a special case: name function: brackets is the beginning of the next line on the first, as follows:

int function (int x) (body of function)
People of all unorthodox criticism this inconsistency, but all right-thinking people understand that: (1st) K & R is the ___ of the ___, and (Second) K & R, if not, see first. (:-))...... In addition, the function is special, need not be the same.

Note that the end of the bracket in its share of the line is empty, __ __ it apart with a statement as a continuation of the same symbol. Such as "while" in the do-while loop, or "else" In the if statement. as follows:

do (body of do-loop) while (condition);
And

if (x == y) (..) else if (x> y) (...) else (....)
Reason: K & R.

Also, note that this brace placement method reduces the number of blank lines, but no less readable. Thus, the screen size is limited, you can have more blank lines to write some comments the.

Chapter III: Naming System

C is a concise language, then the name should be simple. ASCAL with MODULE-2 and the language is different, C programmers do not use such ThisVariableIsATemporaryCounter the like are named. A C language programmer will be the named "tmp", which is easy to write, and not so difficult to understand.

However, when the mixed type of the name had to appear when the descriptive name of the global variable is necessary for the. Call a name "foo" is a function of overall annoying. Global variables (only you have to use it only when used), as a global function, requires a descriptive naming. If you have a function used to calculate the number of active users, you should be so named - "count_active_users ()"-- or other similar forms, you should not be named "cntusr ()".

There is a method known as the Hungarian name, it will function, the type of code to write the variable name in this way is a manifestation of defective brain --- the compiler knows the type but also to check it, but this will only confuse programmer. - Why know why Micro $ oft will produce so many "bugs" program has to!!.

Local variable names should be short and pithy. If you have a random integer loop counter, it is possible to have "i", if it can be made without any possible misunderstanding, then, to write "loop_counter" is inefficient. Again a, "" tmp "can be any temporary value function variables.

If you are afraid of confusing the name of your local variables, there is another issue, which is called

function-growth-hormone-imbalancesyndrome.
Chapter IV: Function

Function should be short and charming, but only for one thing. It should only cover one or two screens (80 * 24 1 screen), and only for one thing, and will do it well. (Is not that UNIX- style me, translators note).

A function of the maximum length and complexity of function, and inversely proportional to indentation. So, if you have written a simple but a function of the length of long, and you have already done a lot of different situations very small things, write a function is a little longer does not matter.

However, if you write a very complex function, and you have estimated that if most people reading this function, he may not know what to say to this function, this time using the descriptive name of a help function.

Another consideration is the number of local variables. They should not exceed 5-10, or you may be wrong. To re-consider this function, they split into smaller functions. The human brain can often be very easy to remember live in 7 different things, more than this number would cause confusion. you know you are very smart, but you may still want to know two weeks before to do.

Chapter 5: Notes

Notes is a good thing, but too many notes and dangerous, do not try to explain your area code is a comment on how how good: you should write better code, rather than spending a lot of time to explain those bad code.

Usually, the comments you explain your code is doing what, not how to do. Moreover, Yao tried to avoid Jiang comment inserted in a function of Ti in: If this function Que Shihen Fuza, you Xuyao in which a part of Note, you should look back to Chapter IV. You can write a short note to indicate or warn those you think are particularly clever (or very ugly) part, but you have to avoid too much. Instead, the written comments before the function, telling people what to do in it, and possibly why.

Chapter VI: You have caught them up.

Do not worry. You may have been accused of "GUN emacs" will automatically help you deal with C source code format, and you have seen it does so, however, the case of default, its role is somewhat different of man (in fact, they are easily knocked out of something than even ugly - ainfinite number of mnkeys typing into GNU emacs would never make a good program)

So, you can either do not use GUN emacs, or let it use sanervalules. To use the latter, you need to enter the following statement to your. Emacs file.

(Defun linux-c-mode () "C mode with adjusted defaults for use with the Linux kernel." (Interactive) (c-mode) (c-set-style "K & R") (setq c-basic -offset8))
This will define a Mx Linux-c-mode command. When you when hacking a module, how you will -*- linux-c -*- input in the beginning of two lines, this mode will automatically work. Moreover, You may want to join the following

(Setq auto-mode-alist (cons''("/ usr / src / linux .*/.* \. 銆栥? Ch $ ". Linux-c-mode) auto-mode-alist))
To you. Emacs file, so that when you / usr / src / linux and edit the file, it automatically switches to linux-c-mode.

However, if you do not let emaces to automatically handle file formats, not nervous, you still have the same thing: "indent."

GNU indent is also very rigid format, which is why you need to add a few lines of command options. However, this is also not too bad, because the creator of GNU indent formats also remember the authority of K & R, (GNU is not crime, they simply wrong in this matter guided by the people), you have to do is just enter the options "-kr-i8" (that "K & R, indented 8 characters).

"Indent" has a lot of features, especially when it recommended that you re-format your code, you should look at the help. But remember: "indent" is not a bad program style Souls Dan.







相关链接:



Specialist Animation Tools



VOB to ZUNE



VOB to WMV



Some Terms related to the domain name explained



Community 2.0 My Community I Call The Shots



Word file recovery simple



VOB to MP4



Icahn comeback MOTO dilemma



Cybersquatting: Business Down? Injury or DOWN?



Photoshop rendering realistic beautiful necklace



Review Games Simulation



09 'happy to steal food' in 2010 what we steal?



The New Second Generation Of New ID Card Readers



System Maintenance EVALUATION



States United States to transfer?



Trust by the Bank through the East, TongLINK / Q acclaimed



Premier SERVER Applications



No comments:

Post a Comment