📄 standards.texi
字号:
cross-operation is not a meaningful thing.Some programs have ways of configuring themselves automatically. Ifyour program is set up to do this, your @code{configure} script can simplyignore most of its arguments.@node Source Language@chapter Using Languages Other Than CUsing a language other than C is like using a non-standard feature: itwill cause trouble for users. Even if GCC supports the other language,users may find it inconvenient to have to install the compiler for thatother language in order to build your program. So please write in C.There are three exceptions for this rule:@itemize @bullet@itemIt is okay to use a special language if the same program contains aninterpreter for that language.Thus, it is not a problem that GNU Emacs contains code written in EmacsLisp, because it comes with a Lisp interpreter.@itemIt is okay to use another language in a tool specifically intended foruse with that language.This is okay because the only people who want to build the tool will bethose who have installed the other language anyway.@itemIf an application is not of extremely widespread interest, then perhapsit's not important if the application is inconvenient to install.@end itemize@node Formatting@chapter Formatting Your Source CodeIt is important to put the open-brace that starts the body of a Cfunction in column zero, and avoid putting any other open-brace oropen-parenthesis or open-bracket in column zero. Several tools lookfor open-braces in column zero to find the beginnings of C functions.These tools will not work on code not formatted that way.It is also important for function definitions to start the name of thefunction in column zero. This helps people to search for functiondefinitions, and may also help certain tools recognize them. Thus,the proper format is this:@examplestatic char *concat (s1, s2) /* Name starts in column zero here */ char *s1, *s2;@{ /* Open brace in column zero here */ @dots{}@}@end example@noindentor, if you want to use @sc{ANSI} C, format the definition like this:@examplestatic char *concat (char *s1, char *s2)@{ @dots{}@}@end exampleIn @sc{ANSI} C, if the arguments don't fit nicely on one line,split it like this:@exampleintlots_of_args (int an_integer, long a_long, short a_short, double a_double, float a_float)@dots{}@end exampleFor the body of the function, we prefer code formatted like this:@exampleif (x < foo (y, z)) haha = bar[4] + 5;else @{ while (z) @{ haha += foo (z, z); z--; @} return ++x + bar (); @}@end exampleWe find it easier to read a program when it has spaces before theopen-parentheses and after the commas. Especially after the commas.When you split an expression into multiple lines, split itbefore an operator, not after one. Here is the right way:@exampleif (foo_this_is_long && bar > win (x, y, z) && remaining_condition)@end exampleTry to avoid having two operators of different precedence at the samelevel of indentation. For example, don't write this:@examplemode = (inmode[j] == VOIDmode || GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j]) ? outmode[j] : inmode[j]);@end exampleInstead, use extra parentheses so that the indentation shows the nesting:@examplemode = ((inmode[j] == VOIDmode || (GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j]))) ? outmode[j] : inmode[j]);@end exampleInsert extra parentheses so that Emacs will indent the code properly.For example, the following indentation looks nice if you do it by hand,but Emacs would mess it up:@examplev = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000;@end exampleBut adding a set of parentheses solves the problem:@examplev = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000);@end exampleFormat do-while statements like this:@exampledo @{ a = foo (a); @}while (a > 0);@end examplePlease use formfeed characters (control-L) to divide the program intopages at logical places (but not within a function). It does not matterjust how long the pages are, since they do not have to fit on a printedpage. The formfeeds should appear alone on lines by themselves.@node Comments@chapter Commenting Your WorkEvery program should start with a comment saying briefly what it is for.Example: @samp{fmt - filter for simple filling of text}.Please put a comment on each function saying what the function does,what sorts of arguments it gets, and what the possible values ofarguments mean and are used for. It is not necessary to duplicate inwords the meaning of the C argument declarations, if a C type is beingused in its customary fashion. If there is anything nonstandard aboutits use (such as an argument of type @code{char *} which is really theaddress of the second character of a string, not the first), or anypossible values that would not work the way one would expect (such as,that strings containing newlines are not guaranteed to work), be sureto say so.Also explain the significance of the return value, if there is one.Please put two spaces after the end of a sentence in your comments, sothat the Emacs sentence commands will work. Also, please writecomplete sentences and capitalize the first word. If a lower-caseidentifer comes at the beginning of a sentence, don't capitalize it!Changing the spelling makes it a different identifier. If you don'tlike starting a sentence with a lower case letter, write the sentencedifferently (e.g. ``The identifier lower-case is @dots{}'').The comment on a function is much clearer if you use the argumentnames to speak about the argument values. The variable name itselfshould be lower case, but write it in upper case when you are speakingabout the value rather than the variable itself. Thus, ``the inodenumber @var{node_num}'' rather than ``an inode''.There is usually no purpose in restating the name of the function inthe comment before it, because the reader can see that for himself.There might be an exception when the comment is so long that the functionitself would be off the bottom of the screen.There should be a comment on each static variable as well, like this:@example/* Nonzero means truncate lines in the display; zero means continue them. */int truncate_lines;@end exampleEvery @samp{#endif} should have a comment, except in the case of shortconditionals (just a few lines) that are not nested. The comment shouldstate the condition of the conditional that is ending, @emph{includingits sense}. @samp{#else} should have a comment describing the condition@emph{and sense} of the code that follows. For example:@example#ifdef foo @dots{}#else /* not foo */ @dots{}#endif /* not foo */@end example@noindentbut, by contrast, write the comments this way for a @samp{#ifndef}:@example#ifndef foo @dots{}#else /* foo */ @dots{}#endif /* foo */@end example@node Syntactic Conventions@chapter Clean Use of C ConstructsPlease explicitly declare all arguments to functions.Don't omit them just because they are ints.Declarations of external functions and functions to appear laterin the source file should all go in one place near the beginning ofthe file (somewhere before the first function definition in the file),or else should go in a header file. Don't put extern declarationsinside functions.Don't declare multiple variables in one declaration that spans lines.Start a new declaration on each line, instead. For example, insteadof this:@exampleint foo, bar;@end example@noindentwrite either this:@exampleint foo, bar;@end example@noindentor this:@exampleint foo;int bar;@end example@noindent(If they are global variables, each should have a comment preceding itanyway.)When you have an if-else statement nested in another if statement,always put braces around the if-else. Thus, never write like this:@exampleif (foo) if (bar) win (); else lose ();@end example@noindentalways like this:@exampleif (foo) @{ if (bar) win (); else lose (); @}@end exampleIf you have an if statement nested inside of an else statement,either write @code{else if} on one line, like this,@exampleif (foo) @dots{}else if (bar) @dots{}@end example@noindentwith its then-part indented like the preceding then-part, or write thenested if within braces like this:@exampleif (foo) @dots{}else @{ if (bar) @dots{} @}@end exampleDon't declare both a structure tag and variables or typedefs in thesame declaration. Instead, declare the structure tag separatelyand then use it to declare the variables or typedefs.Try to avoid assignments inside if-conditions. For example, don'twrite this:@exampleif ((foo = (char *) malloc (sizeof *foo)) == 0) fatal ("virtual memory exhausted");@end example@noindentinstead, write this:@examplefoo = (char *) malloc (sizeof *foo);if (foo == 0) fatal ("virtual memory exhausted");@end exampleDon't make the program ugly to placate lint. Please don't insert anycasts to void. Zero without a cast is perfectly fine as a nullpointer constant.@node Names@chapter Naming Variables and FunctionsPlease use underscores to separate words in a name, so that the Emacsword commands can be useful within them. Stick to lower case; reserveupper case for macros and enum constants, and for name-prefixes thatfollow a uniform convention.For example, you should use names like @code{ignore_space_change_flag};don't use names like @code{iCantReadThis}.Variables that indicate whether command-line options have beenspecified should be named after the meaning of the option, not afterthe option-letter. A comment should state both the exact meaning ofthe option and its letter. For example,@example/* Ignore changes in horizontal whitespace (-b). */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -