Wednesday, August 4, 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, your comment is explain what your code do, not how to do it. Moreover, to attempt to avoid comment inserted in a function body: if this function is very complex, you need one part of the 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.








相关链接:



Ps3 format



Comment Download Managers



MKV



Fireworks 8 dream trip (5): the new changes in the menu



News about File And Disk Management



Popped into the home appliance industry with high black horse



Mp4 to mp3 converter



Paladin 4 Solved The Copy Protection



mov to avi converter Free



CES Show for the first time to advertise in Facebook and YouTube



Menu



Charles Zhang: Sohu is LOOKING for the next opponent



How to make VB do not echo in the text box



Huawei Harbour end of the "son" are killing a man and shake hands with Li Ren Zhengfei



Liang Wang: Whitman's China troubles



Hot Games Board