1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>JunkBuster Coding Standards</title>
8 <h1><a name="top"></a>JunkBuster Coding Standards</h1>
10 <h1><a name="contents"></a>Contents</h1>
12 <h3><a href="#s1">Introduction</a></h3>
14 <h3><a href="#s2">Using Comments</a></h3>
17 <li><a href="#s3">Comment, Comment, Comment</a></li>
19 <li><a href="#s4">Use blocks for comments</a></li>
21 <li><a href="#s5">Keep Comments on their own line</a></li>
23 <li><a href="#s6">Comment each logical step</a></li>
25 <li><a href="#s7">Comment All Functions Thoroughly</a></li>
27 <li><a href="#s8">Comment at the end of braces if the content
28 is more than one screen length</a></li>
31 <h3><a href="#s9">Naming Conventions</a></h3>
34 <li><a href="#s10">Variable Names</a></li>
36 <li><a href="#s11">Function Names</a></li>
38 <li><a href="#s12">Header file prototypes</a></li>
40 <li><a href="#s13">Enumerations, and #defines</a></li>
42 <li><a href="#s14">Constants</a></li>
45 <h3><a href="#s15">Using Space</a></h3>
48 <li><a href="#s16">Put braces on a line by
51 <li><a href="#s17">ALL control statements should have a
54 <li><a href="#s18">Do not belabor/blow-up boolean
57 <li><a href="#s19">Use white space freely because it is
60 <li><a href="#s20">Don't use white space around structure
63 <li><a href="#s21">Make the last brace of a function stand
66 <li><a href="#s22">Use 3 character indentions</a></li>
69 <h3><a href="#s23">Initializing</a></h3>
72 <li><a href="#s24">Initialize all variables</a></li>
75 <h3><a href="#s25">Functions</a></h3>
78 <li><a href="#s26">Name functions that return a boolean as a
81 <li><a href="#s27">Always specify a return type for a
84 <li><a href="#s28">Minimize function calls when iterating by
85 using variables</a></li>
87 <li><a href="#s29">Pass and Return by Const
90 <li><a href="#s30">Pass and Return by Value</a></li>
92 <li><a href="#s31">Use #include <fileName> and #include
93 "fileName" for locals</a></li>
95 <li><a href="#s32">Provide multiple inclusion
98 <li><a href="#s33">Use `extern "C"` when appropriate</a></li>
100 <li><a href="#s34">Where Possible, Use Forward Struct
101 Declaration Instead of Includes</a></li>
104 <h3><a href="#s35">General Coding Practices</a></h3>
107 <li><a href="#s36">Turn on warnings</a></li>
109 <li><a href="#s37">Provide a default case for all switch
112 <li><a href="#s38">Try to avoid falling through cases in a
113 switch statement.</a></li>
115 <li><a href="#s39">Use 'long' or 'short' Instead of
118 <li><a href="#s40">Don't mix size_t and other types</a></li>
120 <li><a href="#s41">Declare each variable and struct on its
123 <li><a href="#s42">Use malloc/zalloc sparingly</a></li>
125 <li><a href="#s43">The Programmer Who Uses 'malloc' is
126 Responsible for Ensuring 'free'</a></li>
128 <li><a href="#s44">Add loaders to the `file_list' structure
129 and in order</a></li>
131 <li><a href="#s45">"Uncertain" new code and/or changes to
132 exitinst code, use FIXME</a></li>
135 <h3><a href="#s46">Addendum: Template for files and function
136 comment blocks:</a></h3>
142 <h1><a name="s1"></a>Introduction</h1>
144 <p>This set of standards is designed to make our lives easier.
145 It is developed with the simple goal of helping us keep the
146 "new and improved Junkbusters" consistent and reliable. Thus
147 making maintenance easier and increasing chances of success of
150 <p>And that of course comes back to us as individuals. If we
151 can increase our development and product efficiencies then we
152 can solve more of the request for changes/improvements and in
153 general feel good about ourselves. ;-></p>
159 <h1><a name="s2"></a>Using Comments</h1>
163 <h2><a name="s3"></a>Comment, Comment, Comment</h2>
165 <p><b>Explanation:</b></p>
167 <p>Comment as much as possible without commenting the obvious.
168 For example do not comment "aVariable is equal to bVariable".
169 Instead explain why aVariable should be equal to the bVariable.
170 Just because a person can read code does not mean they will
171 understand why or what is being done. A reader may spend a lot
172 more time figuring out what is going on when a simple comment
173 or explanation would have prevented the extra research. Please
174 help your brother IJB'ers out!</p>
176 <p>The comments will also help justify the intent of the code.
177 If the comment describes something different than what the code
178 is doing then maybe a programming error is occurring.</p>
180 <p><b>Example:</b></p>
182 /* if page size greater than 1k ... */
183 if ( PageLength() > 1024 )
185 ... "block" the page up ...
188 /* if page size is small, send it in blocks */
189 if ( PageLength() > 1024 )
191 ... "block" the page up ...
194 This demonstrates 2 cases of "what not to do". The first is a
195 "syntax comment". The second is a comment that does not fit what
196 is actually being done.
205 <h2><a name="s4"></a>Use blocks for comments</h2>
207 <p><b>Explanation:</b></p>
209 <p>Comments can help or they can clutter. They help when they
210 are differentiated from the code they describe. One line
211 comments do not offer effective separation between the comment
212 and the code. Block identifiers do, by surrounding the code
213 with a clear, definable pattern.</p>
215 <p><b>Example:</b></p>
217 /*********************************************************************
218 * This will stand out clearly in your code!
219 *********************************************************************/
220 if ( thisVariable == thatVariable )
222 DoSomethingVeryImportant();
226 /* unfortunately, this may not */
227 if ( thisVariable == thatVariable )
229 DoSomethingVeryImportant();
233 if ( thisVariable == thatVariable ) /* this may not either */
235 DoSomethingVeryImportant();
240 <p><b>Exception:</b></p>
242 <p>If you are trying to add a small logic comment and do not
243 wish to "disrubt" the flow of the code, feel free to use a 1
244 line comment which is NOT on the same line as the code.</p>
250 <h2><a name="s5"></a>Keep Comments on their own line</h2>
252 <p><b>Explanation:</b></p>
254 <p>It goes back to the question of readability. If the comment
255 is on the same line as the code it will be harder to read than
256 the comment that is on its own line.</p>
258 <p>There are three exceptions to this rule, which should be
259 violated freely and often: during the definition of variables,
260 at the end of closing braces, when used to comment
263 <p><b>Example:</b></p>
265 /*********************************************************************
266 * This will stand out clearly in your code,
267 * But the second example won't.
268 *********************************************************************/
269 if ( thisVariable == thatVariable )
271 DoSomethingVeryImportant();
274 if ( thisVariable == thatVariable ) /*can you see me?*/
276 DoSomethingVeryImportant(); /*not easily*/
280 /*********************************************************************
281 * But, the encouraged exceptions:
282 *********************************************************************/
283 int urls_read = 0; /* # of urls read + rejected */
284 int urls_rejected = 0; /* # of urls rejected */
288 DoSomethingVeryImportant();
292 short DoSomethingVeryImportant(
293 short firstParam, /* represents something */
294 short nextParam /* represents something else */ )
298 } /* -END- DoSomethingVeryImportant */
307 <h2><a name="s6"></a>Comment each logical step</h2>
309 <p><b>Explanation:</b></p>
311 <p>Logical steps should be commented to help others follow the
312 intent of the written code and comments will make the code more
315 <p>If you have 25 lines of code without a comment, you should
316 probably go back into it to see where you forgot to put
319 <p>Most "for", "while", "do", etc... loops _probably_ need a
320 comment. After all, these are usually major logic
327 <h2><a name="s7"></a>Comment All Functions Thoroughly</h2>
329 <p><b>Explanation:</b></p>
331 <p>A reader of the code should be able to look at the comments
332 just prior to the beginning of a function and discern the
333 reason for its existence and the consequences of using it. The
334 reader should not have to read through the code to determine if
335 a given function is safe for a desired use. The proper
336 information thoroughly presented at the introduction of a
337 function not only saves time for subsequent maintenance or
338 debugging, it more importantly aids in code reuse by allowing a
339 user to determine the safety and applicability of any function
340 for the problem at hand. As a result of such benefits, all
341 functions should contain the information presented in the
342 addendum section of this document.</p>
348 <h2><a name="s8"></a>Comment at the end of braces if the
349 content is more than one screen length</h2>
351 <p><b>Explanation:</b></p>
353 <p>Each closing brace should be followed on the same line by a
354 comment that describes the origination of the brace if the
355 original brace is off of the screen, or otherwise far away from
356 the closing brace. This will simplify the debugging,
357 maintenance, and readability of the code.</p>
359 <p>As a suggestion , use the following flags to make the
360 comment and its brace more readable:</p>
362 <p>use following a closing brace: } /* -END- if() or while ()
365 <p><b>Example:</b></p>
369 DoSomethingVeryImportant();
370 ...some long list of commands...
371 } /* -END- if x is 1 */
377 DoSomethingVeryImportant();
378 ...some long list of commands...
379 } /* -END- if ( 1 == X ) */
388 <h1><a name="s9"></a>Naming Conventions</h1>
392 <h2><a name="s10"></a>Variable Names</h2>
394 <p><b>Explanation:</b></p>
396 <p>Use all lowercase, and seperate words via an underscore
397 ('_'). Do not start an identifier with an underscore. (ANSI C
398 reserves these for use by the compiler and system headers.) Do
399 not use identifiers which are reserved in ANSI C++. (E.g.
400 template, class, true, false, ...). This is in case we ever
401 decide to port JunkBuster to C++.</p>
403 <p><b>Example:</b></p>
405 int ms_iis5_hack = 0;
409 <p><b>Instead of:</b></p>
411 <p>int msiis5hack = 0; int msIis5Hack = 0;</p>
417 <h2><a name="s11"></a>Function Names</h2>
419 <p><b>Explanation:</b></p>
421 <p>Use all lowercase, and seperate words via an underscore
422 ('_'). Do not start an identifier with an underscore. (ANSI C
423 reserves these for use by the compiler and system headers.) Do
424 not use identifiers which are reserved in ANSI C++. (E.g.
425 template, class, true, false, ...). This is in case we ever
426 decide to port JunkBuster to C++.</p>
428 <p><b>Example:</b></p>
430 int load_some_file( struct client_state *csp )
434 <p><b>Instead of:</b></p>
436 <p>int loadsomefile( struct client_state *csp ) int
437 loadSomeFile( struct client_state *csp )</p>
443 <h2><a name="s12"></a>Header file prototypes</h2>
445 <p><b>Explanation:</b></p>
447 <p>Use a descriptive parameter name in the function prototype
448 in header files. Use the same parameter name in the header file
449 that you use in the c file.</p>
451 <p><b>Example:</b></p>
453 (.h) extern int load_aclfile( struct client_state *csp );
454 (.c) int load_aclfile( struct client_state *csp )
458 <p><b>Instead of:</b> (.h) extern int load_aclfile( struct
459 client_state * ); or (.h) extern int load_aclfile(); (.c) int
460 load_aclfile( struct client_state *csp )</p>
466 <h2><a name="s13"></a>Enumerations, and #defines</h2>
468 <p><b>Explanation:</b></p>
470 <p>Use all capital letters, with underscores between words. Do
471 not start an identifier with an underscore. (ANSI C reserves
472 these for use by the compiler and system headers.)</p>
474 <p><b>Example:</b></p>
476 (enumeration) : enum Boolean { FALSE, TRUE };
477 (#define) : #define DEFAULT_SIZE 100;
481 <p><b>Note:</b> We have a standard naming scheme for #defines
482 that toggle a feature in the preprocessor: FEATURE_xxx, where
483 xxx is a short (preferably 1 or 2 word) description.</p>
485 <p><b>Example:</b></p>
487 #define FEATURE_FORCE 1
490 #define FORCE_PREFIX blah
491 #endif /* def FEATURE_FORCE */
500 <h2><a name="s14"></a>Constants</h2>
502 <p><b>Explanation:</b></p>
504 <p>Spell common words out entirely (do not remove vowels).</p>
506 <p>Use only widely-known domain acronyms and abbreviations.
507 Capitalize all letters of an acronym.</p>
509 <p>Use underscore (_) to separate adjacent acronyms and
510 abbreviations. Never terminate a name with an underscore.</p>
512 <p><b>Example:</b></p>
514 #define USE_IMAGE_LIST 1
518 <p><b>Instead of:</b></p>
520 <p>#define USE_IMG_LST 1 or #define _USE_IMAGE_LIST 1 or
521 #define USE_IMAGE_LIST_ 1 or #define use_image_list 1 or
522 #define UseImageList 1</p>
528 <h1><a name="s15"></a>Using Space</h1>
532 <h2><a name="s16"></a>Put braces on a line by themselves.</h2>
534 <p><b>Explanation:</b></p>
536 <p>The brace needs to be on a line all by itself, not at the
537 end of the statement. Curly braces should line up with the
538 construct that they're associated with. This practice makes it
539 easier to identify the opening and closing braces for a
542 <p><b>Example:</b></p>
551 <p><b>Instead of:</b></p>
553 <p>if ( this == that ) { ... }</p>
557 <p>if ( this == that ) { ... }</p>
559 <p><b>Note:</b> In the special case that the if-statement is
560 inside a loop, and it is trivial, i.e. it tests for a
561 condidtion that is obvious from the purpose of the block,
562 one-liners as above may optically preserve the loop structure
563 and make it easier to read.</p>
565 <p><b>Status:</b> developer-discrection.</p>
567 <p><b>Example exception:</b></p>
569 while ( more lines are read )
571 /* Please document what is/is not a comment line here */
572 if ( it's a comment ) continue;
574 do_something( line );
584 <h2><a name="s17"></a>ALL control statements should have a
587 <p><b>Explanation:</b></p>
589 <p>Using braces to make a block will make your code more
590 readable and less prone to error. All control statements should
591 have a block defined.</p>
593 <p><b>Example:</b></p>
603 <p><b>Instead of:</b></p>
605 <p>if ( this == that ) DoSomething(); DoSomethingElse();</p>
609 <p>if ( this == that ) DoSomething();</p>
611 <p><b>Note:</b> The first example in "Instead of" will execute
612 in a manner other than that which the developer desired (per
613 indentation). Using code braces would have prevented this
614 "feature". The "explanation" and "exception" from the point
615 above also applies.</p>
621 <h2><a name="s18"></a>Do not belabor/blow-up boolean
624 <p><b>Example:</b></p>
626 structure->flag = ( condition );
630 <p><b>Instead of:</b></p>
632 <p>if ( condition ) { structure->flag = 1; } else {
633 structure->flag = 0; }</p>
635 <p><b>Note:</b> The former is readable and consice. The later
636 is wordy and inefficient. Please assume that any developer new
637 to the project has at least a "good" knowledge of C/C++. (Hope
638 I do not offend by that last comment ... 8-)</p>
644 <h2><a name="s19"></a>Use white space freely because it is
647 <p><b>Explanation:</b></p>
649 <p>Make it readable. The notable exception to using white space
650 freely is listed in the next guideline.</p>
652 <p><b>Example:</b></p>
656 int anotherValue = 0;
657 int thisVariable = 0;
659 if ( thisVariable == thatVariable )
661 firstValue = oldValue + ( ( someValue - anotherValue ) - whatever )
670 <h2><a name="s20"></a>Don't use white space around structure
673 <p><b>Explanation:</b></p>
675 <p>- structure pointer operator ( "->" ) - member operator (
676 "." ) - functions and parentheses</p>
678 <p>It is a general coding practice to put pointers, references,
679 and function parentheses next to names. With spaces, the
680 connection between the object and variable/function name is not
683 <p><b>Example:</b></p>
691 <p><b>Instead of:</b> aStruct -> aMember; aStruct . aMember;
698 <h2><a name="s21"></a>Make the last brace of a function stand
701 <p><b>Example:</b></p>
708 } /* -END- function1 */
713 } /* -END- function2 */
718 <p><b>Instead of:</b></p>
720 <p>int function1( ... ) { ...code... return( retCode ); } int
721 function2( ... ) { }</p>
723 <p><b>Note:</b> Use 1 blank line before the closing brace and 2
724 lines afterwards. This makes the end of function standout to
725 the most casual viewer. Although function comments help
726 seperate functions, this is still a good coding practice. In
727 fact, I follow these rules when using blocks in "for", "while",
728 "do" loops, and long if {} statements too. After all whitespace
731 <p><b>Status:</b> developer-discrection on the number of blank
732 lines. Enforced is the end of function comments.</p>
738 <h2><a name="s22"></a>Use 3 character indentions</h2>
740 <p><b>Explanation:</b></p>
742 <p>If some use 8 character TABs and some use 3 character TABs,
743 the code can look *very* ragged. So use 3 character indentions
744 only. If you like to use TABs, pass your code through a filter
745 such as "expand -t3" before checking in your code.</p>
747 <p><b>Example:</b></p>
749 static const char * const url_code_map[256] =
759 return( ALWAYS_TRUE );
763 return( HOW_DID_YOU_GET_HERE );
766 return( NEVER_GETS_HERE );
777 <h1><a name="s23"></a>Initializing</h1>
781 <h2><a name="s24"></a>Initialize all variables</h2>
783 <p><b>Explanation:</b></p>
785 <p>Do not assume that the variables declared will not be used
786 until after they have been assigned a value somewhere else in
787 the code. Remove the chance of accidentally using an unassigned
790 <p><b>Example:</b></p>
798 <p><b>Note:</b> It is much easier to debug a SIGSEGV if the
799 message says you are trying to access memory address 00000000
800 and not 129FA012; or arrayPtr[20] causes a SIGSEV vs.
803 <p><b>Status:</b> developer-discrection if and only if the
804 variable is assigned a value "shortly after" declaration.</p>
810 <h1><a name="s25"></a>Functions</h1>
814 <h2><a name="s26"></a>Name functions that return a boolean as a
817 <p><b>Explanation:</b></p>
819 <p>Value should be phrased as a question that would logically
820 be answered as a true or false statement</p>
822 <p><b>Example:</b></p>
835 <h2><a name="s27"></a>Always specify a return type for a
838 <p><b>Explanation:</b></p>
840 <p>The default return for a function is an int. To avoid
841 ambiguity, create a return for a function when the return has a
842 purpose, and create a void return type if the function does not
843 need to return anything.</p>
849 <h2><a name="s28"></a>Minimize function calls when iterating by
852 <p><b>Explanation:</b></p>
854 <p>It is easy to write the following code, and a clear argument
855 can be made that the code is easy to understand:</p>
857 <p><b>Example:</b></p>
859 for ( size_t cnt = 0; cnt < blockListLength(); cnt ++ )
866 <p><b>Note:</b> Unfortunately, this makes a function call for
867 each and every iteration. This increases the overhead in the
868 program, because the compiler has to look up the function each
869 time, call it, and return a value. Depending on what occurs in
870 the blockListLength() call, it might even be creating and
871 destroying structures with each iteration, even though in each
872 case it is comparing "cnt" to the same value, over and over.
873 Remember too - even a call to blockListLength() is a function
874 call, with the same overhead.</p>
876 <p>Instead of using a function call during the iterations,
877 assign the value to a variable, and evaluate using the
880 <p><b>Example:</b></p>
882 size_t len = blockListLength();
884 for ( size_t cnt = 0; cnt < len; cnt ++ )
891 <p><b>Exceptions:</b> if the value of blockListLength() *may*
892 change or could *potentially* change, then you must code the
893 function call in the for/while loop.</p>
899 <h2><a name="s29"></a>Pass and Return by Const Reference</h2>
901 <p><b>Explanation:</b></p>
903 <p>This allows a developer to define a const pointer and call
904 your function. If your function does not have the const
905 keyword, we may not be able to use your function. Consider
906 strcmp, if it were defined as: extern int strcmp( char *s1,
909 <p>I could then not use it to compare argv's in main: int main(
910 int argc, const char *argv[] ) { strcmp( argv[0], "junkbusters"
913 <p>Both these pointers are *const*! If the c runtime library
914 maintainers do it, we should too.</p>
920 <h2><a name="s30"></a>Pass and Return by Value</h2>
922 <p><b>Explanation:</b></p>
924 <p>Most structures cannot fit onto a normal stack entry (i.e.
925 they are not 4 bytes or less). Aka, a function declaration
926 like: int load_aclfile( struct client_state csp )</p>
928 <p>would not work. So, to be consistent, we should declare all
929 prototypes with "pass by value": int load_aclfile( struct
930 client_state *csp )</p>
936 <h2><a name="s31"></a>Use #include <fileName> and
937 #include "fileName" for locals</h2>
939 <p><b>Explanation:</b></p>
941 <p>Your include statements should contain the file name without
942 a path. The path should be listed in the Makefile, using -I as
943 processor directive to search the indicated paths. An exception
944 to this would be for some proprietary software that utilizes a
945 partial path to distinguish their header files from system or
946 other header files.</p>
948 <p><b>Example:</b></p>
950 #include <iostream.h> /* This is not a local include */
951 #include "config.h" /* This IS a local include */
955 <p><b>Exception:</b></p>
957 <p>/* This is not a local include, but requires a path element.
958 */ #include <sys/fileName.h></p>
960 <p><b>Note:</b> Please! do not add "-I." to the Makefile
961 without a _very_ good reason. This duplicates the #include
962 "file.h" behaviour.</p>
968 <h2><a name="s32"></a>Provide multiple inclusion
971 <p><b>Explanation:</b></p>
973 <p>Prevents compiler and linker errors resulting from
974 redefinition of items.</p>
976 <p>Wrap each header file with the following syntax to prevent
977 multiple inclusions of the file. Of course, replace PROJECT_H
978 with your file name, with "." Changed to "_", and make it
981 <p><b>Example:</b></p>
983 #ifndef PROJECT_H_INCLUDED
984 #define PROJECT_H_INCLUDED
986 #endif /* ndef PROJECT_H_INCLUDED */
995 <h2><a name="s33"></a>Use `extern "C"` when appropriate</h2>
997 <p><b>Explanation:</b></p>
999 <p>If our headers are included from C++, they must declare our
1000 functions as `extern "C"`. This has no cost in C, but increases
1001 the potential re-usability of our code.</p>
1003 <p><b>Example:</b></p>
1008 #endif /* def __cplusplus */
1010 ... function definitions here ...
1014 #endif /* def __cplusplus */
1023 <h2><a name="s34"></a>Where Possible, Use Forward Struct
1024 Declaration Instead of Includes</h2>
1026 <p><b>Explanation:</b></p>
1028 <p>Useful in headers that include pointers to other struct's.
1029 Modifications to excess header files may cause needless
1032 <p><b>Example:</b></p>
1034 /*********************************************************************
1035 * We're avoiding an include statement here!
1036 *********************************************************************/
1038 extern file_list *xyz;
1042 <p><b>Note:</b> If you declare "file_list xyz;" (without the
1043 pointer), then including the proper header file is necessary.
1044 If you only want to prototype a pointer, however, the header
1045 file is unneccessary.</p>
1047 <p><b>Status:</b> Use with discrection.</p>
1053 <h1><a name="s35"></a>General Coding Practices</h1>
1057 <h2><a name="s36"></a>Turn on warnings</h2>
1059 <p><b>Explanation</b></p>
1061 <p>Compiler warnings are meant to help you find bugs. You
1062 should turn on as many as possible. With GCC, the switch is
1063 "-Wall". Try and fix as many warnings as possible.</p>
1069 <h2><a name="s37"></a>Provide a default case for all switch
1072 <p><b>Explanation:</b></p>
1074 <p>What you think is guaranteed is never really guaranteed. The
1075 value that you don't think you need to check is the one that
1076 someday will be passed. So, to protect yourself from the
1077 unknown, always have a default step in a switch statement.</p>
1079 <p><b>Example:</b></p>
1081 switch( hash_string( cmd ) )
1083 case hash_actions_file :
1093 ... anomly code goes here ...
1094 continue; / break; / exit( 1 ); / etc ...
1096 } /* end switch( hash_string( cmd ) ) */
1100 <p><b>Note:</b> If you already have a default condition, you
1101 are obviously exempt from this point. Of note, most of the
1102 WIN32 code calls `DefWindowProc' after the switch statement.
1103 This API call *should* be included in a default statement.</p>
1105 <p><b>Another Note:</b> This is not so much a readability issue
1106 as a robust programming issue. The "anomly code goes here" may
1107 be no more than a print to the STDERR stream (as in
1108 load_config). Or it may really be an ABEND condition.</p>
1110 <p><b>Status:</b> Programmer discretion is advised.</p>
1116 <h2><a name="s38"></a>Try to avoid falling through cases in a
1117 switch statement.</h2>
1119 <p><b>Explanation:</b></p>
1121 <p>In general, you will want to have a 'break' statement within
1122 each 'case' of a switch statement. This allows for the code to
1123 be more readable and understandable, and furthermore can
1124 prevent unwanted surprises if someone else later gets creative
1125 and moves the code around.</p>
1127 <p>The language allows you to plan the fall through from one
1128 case statement to another simply by omitting the break
1129 statement within the case statement. This feature does have
1130 benefits, but should only be used in rare cases. In general,
1131 use a break statement for each case statement.</p>
1133 <p>If you choose to allow fall through, you should comment both
1134 the fact of the fall through and reason why you felt it was
1141 <h2><a name="s39"></a>Use 'long' or 'short' Instead of
1144 <p><b>Explanation:</b></p>
1146 <p>On 32-bit platforms, int usually has the range of long. On
1147 16-bit platforms, int has the range of short.</p>
1149 <p><b>Status:</b> open-to-debate. In the case of most FSF
1150 projects (including X/GNU-Emacs), there are typedefs to int4,
1151 int8, int16, (or equivalence ... I forget the exact typedefs
1152 now). Should we add these to IJB now that we have a "configure"
1159 <h2><a name="s40"></a>Don't mix size_t and other types</h2>
1161 <p><b>Explanation:</b></p>
1163 <p>The type of size_t varies across platforms. Do not make
1164 assumptions about whether it is signed or unsigned, or about
1165 how long it is. Do not compare a size_t against another
1166 variable of a different type (or even against a constant)
1167 without casting one of the values. Try to avoid using size_t if
1174 <h2><a name="s41"></a>Declare each variable and struct on its
1177 <p><b>Explanation:</b></p>
1179 <p>It can be tempting to declare a series of variables all on
1180 one line. Don't.</p>
1182 <p><b>Example:</b></p>
1190 <p><b>Instead of:</b></p>
1192 <p>long a, b, c;</p>
1194 <p><b>Explanation:</b> - there is more room for comments on the
1195 individual variables - easier to add new variables without
1196 messing up the original ones - when searching on a variable to
1197 find its type, there is less clutter to "visually"
1200 <p><b>Exceptions:</b> when you want to declare a bunch of loop
1201 variables or other trivial variables; feel free to declare them
1202 on 1 line. You should, although, provide a good comment on
1203 their functions.</p>
1205 <p><b>Status:</b> developer-discrection.</p>
1211 <h2><a name="s42"></a>Use malloc/zalloc sparingly</h2>
1213 <p><b>Explanation:</b></p>
1215 <p>Create a local stuct (on the stack) if the variable will
1216 live and die within the context of one function call.</p>
1218 <p>Only "malloc" a struct (on the heap) if the variable's life
1219 will extend beyond the context of one function call.</p>
1221 <p><b>Example:</b></p>
1223 If a function creates a struct and stores a pointer to it in a
1224 list, then it should definately be allocated via `malloc'.
1233 <h2><a name="s43"></a>The Programmer Who Uses 'malloc' is
1234 Responsible for Ensuring 'free'</h2>
1236 <p><b>Explanation:</b></p>
1238 <p>If you have to "malloc" an instance, you are responsible for
1239 insuring that the instance is `free'd, even if the deallocation
1240 event falls within some other programmer's code. You are also
1241 responsible for ensuring that deletion is timely (i.e. not too
1242 soon, not too late). This is known as "low-coupling" and is a
1243 "good thing (tm)". You may need to offer a
1244 free/unload/destuctor type function to accomodate this.</p>
1246 <p><b>Example:</b></p>
1248 int load_re_filterfile( struct client_state *csp ) { ... }
1249 static void unload_re_filterfile( void *f ) { ... }
1253 <p><b>Exceptions:</b></p>
1255 <p>The developer cannot be expected to provide `free'ing
1256 functions for C run-time library functions ... such as
1259 <p><b>Status:</b> developer-discrection. The "main" use of this
1260 standard is for allocating and freeing data structures (complex
1267 <h2><a name="s44"></a>Add loaders to the `file_list' structure
1270 <p><b>Explanation:</b></p>
1272 <p>I have ordered all of the "blocker" file code to be in alpha
1273 order. It is easier to add/read new blockers when you expect a
1276 <p><b>Note:</b> It may appear that the alpha order is broken in
1277 places by POPUP tests coming before PCRS tests. But since
1278 POPUPs can also be referred to as KILLPOPUPs, it is clear that
1279 it should come first.</p>
1285 <h2><a name="s45"></a>"Uncertain" new code and/or changes to
1286 exitinst code, use FIXME</h2>
1288 <p><b>Explanation:</b></p>
1290 <p>If you have enough confidence in new code or confidence in
1291 your changes, but are not *quite* sure of the reprocussions,
1294 <p>/* FIXME: this code has a logic error on platform XYZ, *
1295 attempthing to fix */ #ifdef PLATFORM ...changed code here...
1300 <p>/* FIXME: I think the original author really meant this...
1301 */ ...changed code here...</p>
1305 <p>/* FIXME: new code that *may* break something else... */
1306 ...new code here...</p>
1308 <p><b>Note:</b> If you make it clear that this may or may not
1309 be a "good thing (tm)", it will be easier to identify and
1310 include in the project (or conversly exclude from the
1317 <h1><a name="s46"></a>Addendum: Template for files and function
1318 comment blocks:</h1>
1320 <p><b>Example for file comments:</b></p>
1322 const char FILENAME_rcs[] = "$Id: STANDARDS.txt,v 1.5 2001/09/10 18:37:48 jongfoster Exp $";
1323 /*********************************************************************
1325 * File : $S<!-- Break CVS Substitution -->ource$
1327 * Purpose : (Fill me in with a good description!)
1329 * Copyright : Written by and Copyright (C) 2001 the SourceForge
1330 * IJBSWA team. http://ijbswa.sourceforge.net
1332 * Based on the Internet Junkbuster originally written
1333 * by and Copyright (C) 1997 Anonymous Coders and
1334 * Junkbusters Corporation. http://www.junkbusters.com
1336 * This program is free software; you can redistribute it
1337 * and/or modify it under the terms of the GNU General
1338 * Public License as published by the Free Software
1339 * Foundation; either version 2 of the License, or (at
1340 * your option) any later version.
1342 * This program is distributed in the hope that it will
1343 * be useful, but WITHOUT ANY WARRANTY; without even the
1344 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1345 * PARTICULAR PURPOSE. See the GNU General Public
1346 * License for more details.
1348 * The GNU General Public License should be included with
1349 * this file. If not, you can view it at
1350 * http://www.gnu.org/copyleft/gpl.html
1351 * or write to the Free Software Foundation, Inc., 59
1352 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1355 * $L<!-- Break CVS Substitution -->og$
1357 *********************************************************************/
1362 ...necessary include files for us to do our work...
1364 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
1369 <p><b>Note:</b> This declares the rcs variables that should be
1370 added to the "show-proxy-args" page. If this is a brand new
1371 creation by you, you are free to change the "Copyright" section
1372 to represent the rights you wish to maintain.</p>
1374 <p><b>Note:</b> The formfeed character that is present right
1375 after the comment flower box is handy for (X|GNU)Emacs users to
1376 skip the verbige and get to the heart of the code (via
1377 `forward-page' and `backward-page'). Please include it if you
1380 <p><b>Example for file header comments:</b></p>
1384 #define FILENAME_H_VERSION "$Id: STANDARDS.txt,v 1.5 2001/09/10 18:37:48 jongfoster Exp $"
1385 /*********************************************************************
1387 * File : $S<!-- Break CVS Substitution -->ource$
1389 * Purpose : (Fill me in with a good description!)
1391 * Copyright : Written by and Copyright (C) 2001 the SourceForge
1392 * IJBSWA team. http://ijbswa.sourceforge.net
1394 * Based on the Internet Junkbuster originally written
1395 * by and Copyright (C) 1997 Anonymous Coders and
1396 * Junkbusters Corporation. http://www.junkbusters.com
1398 * This program is free software; you can redistribute it
1399 * and/or modify it under the terms of the GNU General
1400 * Public License as published by the Free Software
1401 * Foundation; either version 2 of the License, or (at
1402 * your option) any later version.
1404 * This program is distributed in the hope that it will
1405 * be useful, but WITHOUT ANY WARRANTY; without even the
1406 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1407 * PARTICULAR PURPOSE. See the GNU General Public
1408 * License for more details.
1410 * The GNU General Public License should be included with
1411 * this file. If not, you can view it at
1412 * http://www.gnu.org/copyleft/gpl.html
1413 * or write to the Free Software Foundation, Inc., 59
1414 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1417 * $L<!-- Break CVS Substitution -->og$
1419 *********************************************************************/
1422 #include "project.h"
1428 ... function headers here ...
1431 /* Revision control strings from this header and associated .c file */
1432 extern const char FILENAME_rcs[];
1433 extern const char FILENAME_h_rcs[];
1440 #endif /* ndef _FILENAME_H */
1451 <p><b>Example for function comments:</b></p>
1453 /*********************************************************************
1455 * Function : FUNCTION_NAME
1457 * Description : (Fill me in with a good description!)
1460 * 1 : param1 = pointer to an important thing
1461 * 2 : x = pointer to something else
1463 * Returns : 0 => Ok, everything else is an error.
1465 *********************************************************************/
1466 int FUNCTION_NAME( void *param1, const char *x )
1476 <p><b>Note:</b> If we all follow this practice, we should be
1477 able to parse our code to create a "self-documenting" web