Changes

Jump to navigation Jump to search
misc
Line 1: Line 1: −
==  Formatting: ==
+
==  Formatting source code ==
* Formatting your source code.
      
Keep the length of source lines to 79 characters or less, for maximum readability in the widest range of environments.
 
Keep the length of source lines to 79 characters or less, for maximum readability in the widest range of environments.
Line 13: Line 12:     
== Comments ==
 
== Comments ==
* Commenting your work.
+
 
 
Every program should start with a comment saying briefly what it is for. Example: ‘fmt - filter for simple filling of text’. This comment should be at the top of the source file containing the ‘main’ function of the program.
 
Every program should start with a comment saying briefly what it is for. Example: ‘fmt - filter for simple filling of text’. This comment should be at the top of the source file containing the ‘main’ function of the program.
   Line 30: Line 29:  
There is usually no purpose in restating the name of the function in the comment before it, because readers can see that for themselves. There might be an exception when the comment is so long that the function itself would be off the bottom of the screen.  
 
There is usually no purpose in restating the name of the function in the comment before it, because readers can see that for themselves. There might be an exception when the comment is so long that the function itself would be off the bottom of the screen.  
   −
https://www.gnu.org/prep/standards/html_node/Comments.html#Comments
+
See: https://www.gnu.org/prep/standards/html_node/Comments.html#Comments
 +
 
 +
 
 +
== Syntactic Conventions ==
 +
* Clean use of C constructs.
 +
 
 +
Don’t make the program ugly just to placate static analysis tools such as lint, clang, and GCC with extra warnings options such as -Wconversion and -Wundef. These tools can help find bugs and unclear code, but they can also generate so many false alarms that it hurts readability to silence them with unnecessary casts, wrappers, and other complications. For example, please don’t insert casts to void or calls to do-nothing functions merely to pacify a lint checker.
 +
 
 +
Declarations of external functions and functions to appear later in the source file should all go in one place near the beginning of the file (somewhere before the first function definition in the file), or else should go in a header file. Don’t put extern declarations inside functions.
 +
 
 +
It used to be common practice to use the same local variables (with names like tem) over and over for different values within one function. Instead of doing this, it is better to declare a separate local variable for each distinct purpose, and give it a name which is meaningful. This not only makes programs easier to understand, it also facilitates optimization by good compilers. You can also move the declaration of each local variable into the smallest scope that includes all its uses. This makes the program even cleaner.
 +
 
 +
Don’t use local variables or parameters that shadow global identifiers. GCC’s ‘-Wshadow’ option can detect this problem.
 +
 
 +
See: https://www.gnu.org/prep/standards/html_node/Syntactic-Conventions.html#Syntactic-Conventions
 +
 
 +
 
 +
 
 +
== Naming variables, functions, and files ==
 +
 
 +
The names of global variables and functions in a program serve as comments of a sort. So don’t choose terse names—instead, look for names that give useful information about the meaning of the variable or function. In a GNU program, names should be English, like other comments.
 +
 
 +
Local variable names can be shorter, because they are used only within one context, where (presumably) comments explain their purpose.
 +
 
 +
Try to limit your use of abbreviations in symbol names. It is ok to make a few abbreviations, explain what they mean, and then use them frequently, but don’t use lots of obscure abbreviations.
 +
 
 +
Please use underscores to separate words in a name, so that the Emacs word commands can be useful within them. Stick to lower case; reserve upper case for macros and enum constants, and for name-prefixes that follow a uniform convention.
 +
 
 +
For example, you should use names like ignore_space_change_flag; don’t use names like iCantReadThis.
 +
 
 +
Variables that indicate whether command-line options have been specified should be named after the meaning of the option, not after the option-letter. A comment should state both the exact meaning of the option and its letter.
 +
 
 +
https://www.gnu.org/prep/standards/html_node/Names.html#Names
 +
 
 +
==Portability between System Types==
 +
 
 +
In the Unix world, “portability” refers to porting to different Unix versions. For a GNU program, this kind of portability is desirable, but not paramount.
 +
 
 +
The primary purpose of GNU software is to run on top of the GNU kernel, compiled with the GNU C compiler, on various types of CPU. So the kinds of portability that are absolutely necessary are quite limited. But it is important to support Linux-based GNU systems, since they are the form of GNU that is popular.
 +
 
 +
Beyond that, it is good to support the other free operating systems (*BSD), and it is nice to support other Unix-like systems if you want to. Supporting a variety of Unix-like systems is desirable, although not paramount. It is usually not too hard, so you may as well do it. But you don’t have to consider it an obligation, if it does turn out to be hard.
 +
 
 +
The easiest way to achieve portability to most Unix-like systems is to use Autoconf. It’s unlikely that your program needs to know more information about the host platform than Autoconf can provide, simply because most of the programs that need such knowledge have already been written.
 +
 
 +
Avoid using the format of semi-internal data bases (e.g., directories) when there is a higher-level alternative (readdir).
 +
 
 +
As for systems that are not like Unix, such as MSDOS, Windows, VMS, MVS, and older Macintosh systems, supporting them is often a lot of work. When that is the case, it is better to spend your time adding features that will be useful on GNU and GNU/Linux, rather than on supporting other incompatible systems.
 +
 
 +
If you do support Windows, please do not abbreviate it as “win”. In hacker terminology, calling something a “win” is a form of praise. You’re free to praise Microsoft Windows on your own if you want, but please don’t do this in GNU packages. Instead of abbreviating “Windows” to “win”, you can write it in full or abbreviate it to “woe” or “w”. In GNU Emacs, for instance, we use ‘w32’ in file names of Windows-specific files, but the macro for Windows conditionals is called WINDOWSNT.
 +
 
 +
It is a good idea to define the “feature test macro” _GNU_SOURCE when compiling your C files. When you compile on GNU or GNU/Linux, this will enable the declarations of GNU library extension functions, and that will usually give you a compiler error message if you define the same function names in some other way in your program. (You don’t have to actually use these functions, if you prefer to make the program more portable to other systems.)
 +
 
 +
But whether or not you use these GNU extensions, you should avoid using their names for any other meanings. Doing so would make it hard to move your code into other GNU programs.
 +
 
 +
https://www.gnu.org/prep/standards/html_node/System-Portability.html#System-Portability
      −
* Syntactic Conventions:   Clean use of C constructs.
  −
* Names:   Naming variables, functions, and files.
  −
* System Portability:   Portability among different operating systems.
   
* CPU Portability:   Supporting the range of CPU types.
 
* CPU Portability:   Supporting the range of CPU types.
 
* System Functions:   Portability and “standard” library functions.
 
* System Functions:   Portability and “standard” library functions.
10

edits

Navigation menu