The Psychology of Quality and More |
CHAPTER 6 : Layout
6.16 Function declarationA function declaration, particularly when it is also a definition, needs to clearly show its purpose, and the meaning of its parameters. 6.16.1 Original or ANSIThe function declaration is another major area where the ANSI standard changes the original layout (although ANSI still allows this).
/* original C */ ------------------------------------------------------------------- This shows the function as it is called, and allows each parameter to be described afterwards. However, it has the redundancy of naming the parameters twice, and requires looking back and fore to correlate their position and type. This can be addressed by using ANSI function prototypes:
/* ANSI C */
This is more concise, but is more likely to overshoot the line and makes commenting of the parameters more difficult. A 'comb' layout can be used to address this problem:
int
6.16.2 Function reference declarationsWhere the function is not being defined, such as in extern declarations, it is being explicit to name the identifiers, even though they are not required:
extern int GetKeyPress( char *pBuffer, int CharsToGet );
This gives extra information to the reader about what GetKeyPress does, albeit at an increased maintenance cost. 6.16.3 Function typeThe function type tends to obscure the function name, which can be inconvenient when searching for a particular function:
struct WINDOW *FindWindow( int WinHandle );
The type and the function name can be separated by putting the type on the previous line. The return type is now clear and the function name is easily found:
struct WINDOW *
A danger with this style is that the type may be missed (for example if it is off-screen) and the declaration interpreted as returning an int.
|
Site Menu |
Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive | |
And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact | |
Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate | |
You can buy books here |
And the big |