4 This set of standards is designed to make our lives easier. It is
5 developed with the simple goal of helping us keep the "new and
6 improved Junkbusters" consistent and reliable. Thus making
7 maintenance easier and increasing chances of success of the
10 And that of course comes back to us as individuals. If we can
11 increase our development and product efficiencies then we can
12 solve more of the request for changes/improvements and in general
13 feel good about ourselves. ;->
19 @@ Comment, Comment, Comment
23 Comment as much as possible without commenting the obvious. For
24 example do not comment "aVariable is equal to bVariable". Instead
25 explain why aVariable should be equal to the bVariable. Just
26 because a person can read code does not mean they will understand
27 why or what is being done. A reader may spend a lot more time
28 figuring out what is going on when a simple comment or explanation
29 would have prevented the extra research. Please help your brother
32 The comments will also help justify the intent of the code. If the
33 comment describes something different than what the code is doing
34 then maybe a programming error is occurring.
38 /* if page size greater than 1k ... */
39 if ( PageLength() > 1024 )
41 ... "block" the page up ...
44 /* if page size is small, send it in blocks */
45 if ( PageLength() > 1024 )
47 ... "block" the page up ...
50 This demonstrates 2 cases of "what not to do". The first is a
51 "syntax comment". The second is a comment that does not fit what
52 is actually being done.
55 @@ Use blocks for comments
59 Comments can help or they can clutter. They help when they are
60 differentiated from the code they describe. One line comments do
61 not offer effective separation between the comment and the code.
62 Block identifiers do, by surrounding the code with a clear,
67 /*********************************************************************
68 * This will stand out clearly in your code!
69 *********************************************************************/
70 if ( thisVariable == thatVariable )
72 DoSomethingVeryImportant();
76 /* unfortunately, this may not */
77 if ( thisVariable == thatVariable )
79 DoSomethingVeryImportant();
83 if ( thisVariable == thatVariable ) /* this may not either */
85 DoSomethingVeryImportant();
90 If you are trying to add a small logic comment and do not wish to
91 "disrubt" the flow of the code, feel free to use a 1 line comment
92 which is NOT on the same line as the code.
95 @@ Keep Comments on their own line
99 It goes back to the question of readability. If the comment is on
100 the same line as the code it will be harder to read than the
101 comment that is on its own line.
103 There are three exceptions to this rule, which should be violated
104 freely and often: during the definition of variables, at the end
105 of closing braces, when used to comment parameters.
109 /*********************************************************************
110 * This will stand out clearly in your code,
111 * But the second example won't.
112 *********************************************************************/
113 if ( thisVariable == thatVariable )
115 DoSomethingVeryImportant();
118 if ( thisVariable == thatVariable ) /*can you see me?*/
120 DoSomethingVeryImportant(); /*not easily*/
124 /*********************************************************************
125 * But, the encouraged exceptions:
126 *********************************************************************/
127 int urls_read = 0; /* # of urls read + rejected */
128 int urls_rejected = 0; /* # of urls rejected */
132 DoSomethingVeryImportant();
136 short DoSomethingVeryImportant(
137 short firstParam, /* represents something */
138 short nextParam /* represents something else */ )
142 } /* -END- DoSomethingVeryImportant */
145 @@ Comment each logical step
149 Logical steps should be commented to help others follow the
150 intent of the written code and comments will make the code more
153 If you have 25 lines of code without a comment, you should
154 probably go back into it to see where you forgot to put one.
156 Most "for", "while", "do", etc... loops _probably_ need a
157 comment. After all, these are usually major logic containers.
160 @@ Comment All Functions Thoroughly
164 A reader of the code should be able to look at the comments just
165 prior to the beginning of a function and discern the reason for
166 its existence and the consequences of using it. The reader
167 should not have to read through the code to determine if a given
168 function is safe for a desired use. The proper information
169 thoroughly presented at the introduction of a function not only
170 saves time for subsequent maintenance or debugging, it more
171 importantly aids in code reuse by allowing a user to determine
172 the safety and applicability of any function for the problem at
173 hand. As a result of such benefits, all functions should contain
174 the information presented in the addendum section of this
178 @@ Comment at the end of braces if the content is more than one screen length
182 Each closing brace should be followed on the same line by a
183 comment that describes the origination of the brace if the
184 original brace is off of the screen, or otherwise far away from
185 the closing brace. This will simplify the debugging, maintenance,
186 and readability of the code.
188 As a suggestion , use the following flags to make the comment and
189 its brace more readable:
191 use following a closing brace:
192 } /* -END- if() or while () or etc... */
198 DoSomethingVeryImportant();
199 ...some long list of commands...
200 } /* -END- if x is 1 */
206 DoSomethingVeryImportant();
207 ...some long list of commands...
208 } /* -END- if ( 1 == X ) */
218 Use all lowercase, and seperate words via an underscore ('_').
219 Do not start an identifier with an underscore. (ANSI C
220 reserves these for use by the compiler and system headers.)
221 Do not use identifiers which are reserved in ANSI C++.
222 (E.g. template, class, true, false, ...). This is in case
223 we ever decide to port JunkBuster to C++.
227 int ms_iis5_hack = 0;
239 Use all lowercase, and seperate words via an underscore ('_').
240 Do not start an identifier with an underscore. (ANSI C
241 reserves these for use by the compiler and system headers.)
242 Do not use identifiers which are reserved in ANSI C++.
243 (E.g. template, class, true, false, ...). This is in case
244 we ever decide to port JunkBuster to C++.
248 int load_some_file( struct client_state *csp )
252 int loadsomefile( struct client_state *csp )
253 int loadSomeFile( struct client_state *csp )
256 @@ Header file prototypes
260 Use a descriptive parameter name in the function prototype in
261 header files. Use the same parameter name in the header file
262 that you use in the c file.
266 (.h) extern int load_aclfile( struct client_state *csp );
267 (.c) int load_aclfile( struct client_state *csp )
270 (.h) extern int load_aclfile( struct client_state * ); or
271 (.h) extern int load_aclfile();
272 (.c) int load_aclfile( struct client_state *csp )
275 @@ Enumerations, and #defines
279 Use all capital letters, with underscores between words.
280 Do not start an identifier with an underscore. (ANSI C
281 reserves these for use by the compiler and system headers.)
285 (enumeration) : enum Boolean { FALSE, TRUE };
286 (#define) : #define DEFAULT_SIZE 100;
288 @@@ Note: We have a standard naming scheme for #defines that
289 toggle a feature in the preprocessor: FEATURE_xxx, where
290 xxx is a short (preferably 1 or 2 word) description.
294 #define FEATURE_FORCE 1
297 #define FORCE_PREFIX blah
298 #endif /* def FEATURE_FORCE */
305 Spell common words out entirely (do not remove vowels).
307 Use only widely-known domain acronyms and abbreviations. Capitalize
308 all letters of an acronym.
310 Use underscore (_) to separate adjacent acronyms and
311 abbreviations. Never terminate a name with an underscore.
315 #define USE_IMAGE_LIST 1
319 #define USE_IMG_LST 1 or
320 #define _USE_IMAGE_LIST 1 or
321 #define USE_IMAGE_LIST_ 1 or
322 #define use_image_list 1 or
323 #define UseImageList 1
329 @@ Put braces on a line by themselves.
333 The brace needs to be on a line all by itself, not at the end of
334 the statement. Curly braces should line up with the construct
335 that they're associated with. This practice makes it easier to
336 identify the opening and closing braces for a block.
347 if ( this == that ) {
353 if ( this == that ) { ... }
355 @@@ Note: In the special case that the if-statement is inside a loop,
356 and it is trivial, i.e. it tests for a condidtion that is obvious
357 from the purpose of the block, one-liners as above may optically
358 preserve the loop structure and make it easier to read.
360 @@@ Status: developer-discrection.
362 @@@ Example exception:
364 while ( more lines are read )
366 /* Please document what is/is not a comment line here */
367 if ( it's a comment ) continue;
369 do_something( line );
373 @@ ALL control statements should have a block
377 Using braces to make a block will make your code more readable
378 and less prone to error. All control statements should have a
397 if ( this == that ) DoSomething();
399 @@@ Note: The first example in "Instead of" will execute in a manner
400 other than that which the developer desired (per indentation). Using
401 code braces would have prevented this "feature". The "explanation"
402 and "exception" from the point above also applies.
405 @@ Do not belabor/blow-up boolean expressions
409 structure->flag = ( condition );
422 @@@ Note: The former is readable and consice. The later is wordy and
423 inefficient. Please assume that any developer new to the project has
424 at least a "good" knowledge of C/C++. (Hope I do not offend by that
428 @@ Use white space freely because it is free
432 Make it readable. The notable exception to using white space
433 freely is listed in the next guideline.
439 int anotherValue = 0;
440 int thisVariable = 0;
442 if ( thisVariable == thatVariable )
444 firstValue = oldValue + ( ( someValue - anotherValue ) - whatever )
447 @@ Don't use white space around structure operators
451 - structure pointer operator ( "->" )
452 - member operator ( "." )
453 - functions and parentheses
455 It is a general coding practice to put pointers, references, and
456 function parentheses next to names. With spaces, the connection
457 between the object and variable/function name is not as clear.
471 @@ Make the last brace of a function stand out
480 } /* -END- function1 */
485 } /* -END- function2 */
499 @@@ Note: Use 1 blank line before the closing brace and 2 lines
500 afterwards. This makes the end of function standout to the most
501 casual viewer. Although function comments help seperate functions,
502 this is still a good coding practice. In fact, I follow these rules
503 when using blocks in "for", "while", "do" loops, and long if {}
504 statements too. After all whitespace is free!
506 @@@ Status: developer-discrection on the number of blank lines.
507 Enforced is the end of function comments.
510 @@ Use 3 character indentions
514 If some use 8 character TABs and some use 3 character TABs, the code
515 can look *very* ragged. So use 3 character indentions only. If you
516 like to use TABs, pass your code through a filter such as "expand -t3"
517 before checking in your code.
521 static const char * const url_code_map[256] =
531 return( ALWAYS_TRUE );
535 return( HOW_DID_YOU_GET_HERE );
538 return( NEVER_GETS_HERE );
546 @@ Initialize all variables
550 Do not assume that the variables declared will not be used until
551 after they have been assigned a value somewhere else in the
552 code. Remove the chance of accidentally using an unassigned
561 @@@ Note: It is much easier to debug a SIGSEGV if the message says
562 you are trying to access memory address 00000000 and not
563 129FA012; or arrayPtr[20] causes a SIGSEV vs. arrayPtr[0].
565 @@@ Status: developer-discrection if and only if the variable is
566 assigned a value "shortly after" declaration.
572 @@ Name functions that return a boolean as a question.
576 Value should be phrased as a question that would logically be
577 answered as a true or false statement
586 @@ Always specify a return type for a function.
590 The default return for a function is an int. To avoid ambiguity,
591 create a return for a function when the return has a purpose, and
592 create a void return type if the function does not need to return
596 @@ Minimize function calls when iterating by using variables
600 It is easy to write the following code, and a clear argument can
601 be made that the code is easy to understand:
605 for ( size_t cnt = 0; cnt < blockListLength(); cnt ++ )
610 @@@ Note: Unfortunately, this makes a function call for each and every
611 iteration. This increases the overhead in the program, because the
612 compiler has to look up the function each time, call it, and return a
613 value. Depending on what occurs in the blockListLength() call, it
614 might even be creating and destroying structures with each iteration,
615 even though in each case it is comparing "cnt" to the same value, over
616 and over. Remember too - even a call to blockListLength() is a
617 function call, with the same overhead.
619 Instead of using a function call during the iterations, assign
620 the value to a variable, and evaluate using the variable.
624 size_t len = blockListLength();
626 for ( size_t cnt = 0; cnt < len; cnt ++ )
631 @@@ Exceptions: if the value of blockListLength() *may* change or could
632 *potentially* change, then you must code the function call in the
636 @@ Pass and Return by Const Reference
640 This allows a developer to define a const pointer and call your
641 function. If your function does not have the const keyword, we
642 may not be able to use your function. Consider strcmp, if it
644 extern int strcmp( char *s1, char *s2 );
646 I could then not use it to compare argv's in main:
647 int main( int argc, const char *argv[] )
649 strcmp( argv[0], "junkbusters" );
652 Both these pointers are *const*! If the c runtime library maintainers
653 do it, we should too.
656 @@ Pass and Return by Value
660 Most structures cannot fit onto a normal stack entry (i.e. they
661 are not 4 bytes or less). Aka, a function declaration like:
662 int load_aclfile( struct client_state csp )
664 would not work. So, to be consistent, we should declare all
665 prototypes with "pass by value":
666 int load_aclfile( struct client_state *csp )
669 @@ Use #include <fileName> and #include "fileName" for locals
673 Your include statements should contain the file name without a
674 path. The path should be listed in the Makefile, using -I as
675 processor directive to search the indicated paths. An exception
676 to this would be for some proprietary software that utilizes a
677 partial path to distinguish their header files from system or
682 #include <iostream.h> /* This is not a local include */
683 #include "config.h" /* This IS a local include */
687 /* This is not a local include, but requires a path element. */
688 #include <sys/fileName.h>
690 @@@ Note: Please! do not add "-I." to the Makefile without a _very_
691 good reason. This duplicates the #include "file.h" behaviour.
694 @@ Provide multiple inclusion protection
698 Prevents compiler and linker errors resulting from redefinition of
701 Wrap each header file with the following syntax to prevent multiple
702 inclusions of the file. Of course, replace PROJECT_H with
703 your file name, with "." Changed to "_", and make it uppercase.
707 #ifndef PROJECT_H_INCLUDED
708 #define PROJECT_H_INCLUDED
710 #endif /* ndef PROJECT_H_INCLUDED */
713 @@ Use `extern "C"` when appropriate
717 If our headers are included from C++, they must declare our
718 functions as `extern "C"`. This has no cost in C, but increases
719 the potential re-usability of our code.
726 #endif /* def __cplusplus */
728 ... function definitions here ...
732 #endif /* def __cplusplus */
735 @@ Where Possible, Use Forward Struct Declaration Instead of Includes
739 Useful in headers that include pointers to other struct's.
740 Modifications to excess header files may cause needless compiles.
744 /*********************************************************************
745 * We're avoiding an include statement here!
746 *********************************************************************/
748 extern file_list *xyz;
750 @@@ Note: If you declare "file_list xyz;" (without the pointer), then
751 including the proper header file is necessary. If you only want to
752 prototype a pointer, however, the header file is unneccessary.
754 @@@ Status: Use with discrection.
757 @ General Coding Practices
764 Compiler warnings are meant to help you find bugs. You should turn
765 on as many as possible. With GCC, the switch is "-Wall". Try and
766 fix as many warnings as possible.
769 @@ Provide a default case for all switch statements
773 What you think is guaranteed is never really guaranteed. The value
774 that you don't think you need to check is the one that someday will be
775 passed. So, to protect yourself from the unknown, always have a
776 default step in a switch statement.
780 switch( hash_string( cmd ) )
782 case hash_actions_file :
792 ... anomly code goes here ...
793 continue; / break; / exit( 1 ); / etc ...
795 } /* end switch( hash_string( cmd ) ) */
797 @@@ Note: If you already have a default condition, you are obviously
798 exempt from this point. Of note, most of the WIN32 code calls
799 `DefWindowProc' after the switch statement. This API call
800 *should* be included in a default statement.
802 @@@ Another Note: This is not so much a readability issue as a robust
803 programming issue. The "anomly code goes here" may be no more than a
804 print to the STDERR stream (as in load_config). Or it may really be
807 @@@ Status: Programmer discretion is advised.
810 @@ Try to avoid falling through cases in a switch statement.
814 In general, you will want to have a 'break' statement within each
815 'case' of a switch statement. This allows for the code to be more
816 readable and understandable, and furthermore can prevent unwanted
817 surprises if someone else later gets creative and moves the code
820 The language allows you to plan the fall through from one case
821 statement to another simply by omitting the break statement within the
822 case statement. This feature does have benefits, but should only be
823 used in rare cases. In general, use a break statement for each case
826 If you choose to allow fall through, you should comment both the fact
827 of the fall through and reason why you felt it was necessary.
830 @@ Use 'long' or 'short' Instead of 'int'
834 On 32-bit platforms, int usually has the range of long. On 16-bit
835 platforms, int has the range of short.
837 @@@ Status: open-to-debate. In the case of most FSF projects
838 (including X/GNU-Emacs), there are typedefs to int4, int8, int16, (or
839 equivalence ... I forget the exact typedefs now). Should we add these
840 to IJB now that we have a "configure" script?
843 @@ Don't mix size_t and other types
847 The type of size_t varies across platforms. Do not make assumptions
848 about whether it is signed or unsigned, or about how long it is.
849 Do not compare a size_t against another variable of a different type
850 (or even against a constant) without casting one of the values.
851 Try to avoid using size_t if you can.
854 @@ Declare each variable and struct on its own line.
858 It can be tempting to declare a series of variables all on one line.
872 - there is more room for comments on the individual variables
873 - easier to add new variables without messing up the original ones
874 - when searching on a variable to find its type, there is less
875 clutter to "visually" eliminate
877 @@@ Exceptions: when you want to declare a bunch of loop variables or
878 other trivial variables; feel free to declare them on 1 line. You
879 should, although, provide a good comment on their functions.
881 @@@ Status: developer-discrection.
884 @@ Use malloc/zalloc sparingly
888 Create a local stuct (on the stack) if the variable will live
889 and die within the context of one function call.
891 Only "malloc" a struct (on the heap) if the variable's life will
892 extend beyond the context of one function call.
896 If a function creates a struct and stores a pointer to it in a
897 list, then it should definately be allocated via `malloc'.
900 @@ The Programmer Who Uses 'malloc' is Responsible for Ensuring 'free'
904 If you have to "malloc" an instance, you are responsible for insuring
905 that the instance is `free'd, even if the deallocation event falls
906 within some other programmer's code. You are also responsible for
907 ensuring that deletion is timely (i.e. not too soon, not too late).
908 This is known as "low-coupling" and is a "good thing (tm)". You may
909 need to offer a free/unload/destuctor type function to accomodate
914 int load_re_filterfile( struct client_state *csp ) { ... }
915 static void unload_re_filterfile( void *f ) { ... }
919 The developer cannot be expected to provide `free'ing functions for
920 C run-time library functions ... such as `strdup'.
922 @@@ Status: developer-discrection. The "main" use of this standard is
923 for allocating and freeing data structures (complex or nested).
926 @@ Add loaders to the `file_list' structure and in order
930 I have ordered all of the "blocker" file code to be in alpha
931 order. It is easier to add/read new blockers when you expect a
934 @@@ Note: It may appear that the alpha order is broken in places by
935 POPUP tests coming before PCRS tests. But since POPUPs can also
936 be referred to as KILLPOPUPs, it is clear that it should come
940 @@ "Uncertain" new code and/or changes to exitinst code, use FIXME
944 If you have enough confidence in new code or confidence in your
945 changes, but are not *quite* sure of the reprocussions, add this:
947 /* FIXME: this code has a logic error on platform XYZ,
951 ...changed code here...
956 /* FIXME: I think the original author really meant this... */
957 ...changed code here...
961 /* FIXME: new code that *may* break something else... */
964 @@@ Note: If you make it clear that this may or may not be a "good
965 thing (tm)", it will be easier to identify and include in the project
966 (or conversly exclude from the project).
969 @ Addendum: Template for files and function comment blocks:
972 @@@ Example for file comments:
974 const char FILENAME_rcs[] = "$Id: STANDARDS.txt,v 1.4 2001/07/13 01:27:19 iwanttokeepanon Exp $";
975 /*********************************************************************
977 * File : $S<!-- Break CVS Substitution -->ource$
979 * Purpose : (Fill me in with a good description!)
981 * Copyright : Written by and Copyright (C) 2001 the SourceForge
982 * IJBSWA team. http://ijbswa.sourceforge.net
984 * Based on the Internet Junkbuster originally written
985 * by and Copyright (C) 1997 Anonymous Coders and
986 * Junkbusters Corporation. http://www.junkbusters.com
988 * This program is free software; you can redistribute it
989 * and/or modify it under the terms of the GNU General
990 * Public License as published by the Free Software
991 * Foundation; either version 2 of the License, or (at
992 * your option) any later version.
994 * This program is distributed in the hope that it will
995 * be useful, but WITHOUT ANY WARRANTY; without even the
996 * implied warranty of MERCHANTABILITY or FITNESS FOR A
997 * PARTICULAR PURPOSE. See the GNU General Public
998 * License for more details.
1000 * The GNU General Public License should be included with
1001 * this file. If not, you can view it at
1002 * http://www.gnu.org/copyleft/gpl.html
1003 * or write to the Free Software Foundation, Inc., 59
1004 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1007 * $L<!-- Break CVS Substitution -->og$
1009 *********************************************************************/
1014 ...necessary include files for us to do our work...
1016 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
1019 @@@ Note: This declares the rcs variables that should be added to the
1020 "show-proxy-args" page. If this is a brand new creation by you, you
1021 are free to change the "Copyright" section to represent the rights you
1024 @@@ Note: The formfeed character that is present right after the
1025 comment flower box is handy for (X|GNU)Emacs users to skip the verbige
1026 and get to the heart of the code (via `forward-page' and
1027 `backward-page'). Please include it if you can.
1029 @@@ Example for file header comments:
1033 #define FILENAME_H_VERSION "$Id: STANDARDS.txt,v 1.4 2001/07/13 01:27:19 iwanttokeepanon Exp $"
1034 /*********************************************************************
1036 * File : $S<!-- Break CVS Substitution -->ource$
1038 * Purpose : (Fill me in with a good description!)
1040 * Copyright : Written by and Copyright (C) 2001 the SourceForge
1041 * IJBSWA team. http://ijbswa.sourceforge.net
1043 * Based on the Internet Junkbuster originally written
1044 * by and Copyright (C) 1997 Anonymous Coders and
1045 * Junkbusters Corporation. http://www.junkbusters.com
1047 * This program is free software; you can redistribute it
1048 * and/or modify it under the terms of the GNU General
1049 * Public License as published by the Free Software
1050 * Foundation; either version 2 of the License, or (at
1051 * your option) any later version.
1053 * This program is distributed in the hope that it will
1054 * be useful, but WITHOUT ANY WARRANTY; without even the
1055 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1056 * PARTICULAR PURPOSE. See the GNU General Public
1057 * License for more details.
1059 * The GNU General Public License should be included with
1060 * this file. If not, you can view it at
1061 * http://www.gnu.org/copyleft/gpl.html
1062 * or write to the Free Software Foundation, Inc., 59
1063 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1066 * $L<!-- Break CVS Substitution -->og$
1068 *********************************************************************/
1071 #include "project.h"
1077 ... function headers here ...
1080 /* Revision control strings from this header and associated .c file */
1081 extern const char FILENAME_rcs[];
1082 extern const char FILENAME_h_rcs[];
1089 #endif /* ndef _FILENAME_H */
1098 @@@ Example for function comments:
1100 /*********************************************************************
1102 * Function : FUNCTION_NAME
1104 * Description : (Fill me in with a good description!)
1107 * 1 : param1 = pointer to an important thing
1108 * 2 : x = pointer to something else
1110 * Returns : 0 => Ok, everything else is an error.
1112 *********************************************************************/
1113 int FUNCTION_NAME( void *param1, const char *x )
1121 @@@ Note: If we all follow this practice, we should be able to parse
1122 our code to create a "self-documenting" web page.
1125 @ Local variables for this standards file
1132 outline-regexp: "[@]+"