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 Capitalize the first letter of each word in a variable name
219 except the first word.
227 int ms_iis5_hack = 0;
229 @@@ Note: This is not an "enforcable" issue since too much of IJB uses
230 the underscore (_) to seperate words.
232 @@@ Status: developer-discrection. This item is at developer
233 discrection and is currently open to debate by the IJB developer team.
234 Too much of IJB violates this proposed standard.
241 Capitalize the first letter of each word in a function name.
245 int loadAclFile( struct client_state *csp )
249 int load_aclfile( struct client_state *csp )
251 @@@ Status: developer-discrection. This item is at developer
252 discrection and is currently open to debate by the IJB developer team.
253 Too much of IJB violates this proposed standard.
255 @@@ Note: on the 2 above "standards" ... if enough of the current
256 developer team agrees, I can use XEmacs to apply a few regular
257 expressions to make the current tree comply with these 2 points.
258 Otherwise I will remove them from this document. Afterall, there is
259 no need to add to the "multiple personallity" syndrome that IJB now
260 has (or recently had before standardization).
263 @@ Header file prototypes
267 Use a descriptive parameter name in the function prototype in
268 header files. Use the same parameter name in the header file
269 that you use in the c file.
273 (.h) extern int load_aclfile( struct client_state *csp );
274 (.c) int load_aclfile( struct client_state *csp )
277 (.h) extern int load_aclfile( struct client_state * ); or
278 (.h) extern int load_aclfile();
279 (.c) int load_aclfile( struct client_state *csp )
282 18. Ennumerations, and #defines
286 Use all capital letters, with underscores between words.
290 (enumeration) : enum Boolean { FALSE, TRUE };
291 (#define) : #define DEFAULT_SIZE 100;
293 @@@ Note: We should have a standard naming scheme for Symbols that
294 toggle a feature in the precompiler, and the constants used by that
295 feature. I'd propose that the toggles should be just one word, with
296 a common prefix, and that any depandant constants should be
297 prefixed by that word.
299 The prefix could be WITH_, HAVE_, ENABLE_, FEATURE_ etc.
301 @@@ Status: I see some this in the code currently! Anybody "figured"
302 out a standard way to do this?
306 #define ENABLE_FORCE 1
309 #define FORCE_PREFIX blah
310 #endif /* def ENABLE_FORCE */
317 Spell common words out entirely (do not remove vowels).
319 Use only widely-known domain acronyms and abbreviations. Capitalize
320 all letters of an acronym.
322 Use underscore (_) to separate adjacent acronyms and
323 abbreviations. Never terminate a name with an underscore.
327 #define USE_IMAGE_LIST 1
331 #define USE_IMG_LST 1 or
332 #define _USE_IMAGE_LIST 1 or
333 #define USE_IMAGE_LIST_ 1 or
334 #define use_image_list 1 or
335 #define UseImageList 1
341 @@ Put braces on a line by themselves.
345 The brace needs to be on a line all by itself, not at the end of
346 the statement. Curly braces should line up with the construct
347 that they're associated with. This practice makes it easier to
348 identify the opening and closing braces for a block.
359 if ( this == that ) {
365 if ( this == that ) { ... }
367 @@@ Note: In the special case that the if-statement is inside a loop,
368 and it is trivial, i.e. it tests for a condidtion that is obvious
369 from the purpose of the block, one-liners as above may optically
370 preserve the loop structure and make it easier to read.
372 @@@ Status: developer-discrection.
374 @@@ Example exception:
376 while ( more lines are read )
378 /* Please document what is/is not a comment line here */
379 if ( it's a comment ) continue;
381 do_something( line );
385 @@ ALL control statements should have a block
389 Using braces to make a block will make your code more readable
390 and less prone to error. All control statements should have a
409 if ( this == that ) DoSomething();
411 @@@ Note: The first example in "Instead of" will execute in a manner
412 other than that which the developer desired (per indentation). Using
413 code braces would have prevented this "feature". The "explanation"
414 and "exception" from the point above also applies.
417 @@ Do not belabor/blow-up boolean expressions
421 structure->flag = ( condition );
434 @@@ Note: The former is readable and consice. The later is wordy and
435 inefficient. Please assume that any developer new to the project has
436 at least a "good" knowledge of C/C++. (Hope I do not offend by that
440 @@ Use white space freely because it is free
444 Make it readable. The notable exception to using white space
445 freely is listed in the next guideline.
451 int anotherValue = 0;
452 int thisVariable = 0;
454 if ( thisVariable == thatVariable )
456 firstValue = oldValue + ( ( someValue - anotherValue ) - whatever )
459 @@ Don't use white space around structure operators
463 - structure pointer operator ( "->" )
464 - member operator ( "." )
465 - functions and parentheses
467 It is a general coding practice to put pointers, references, and
468 function parentheses next to names. With spaces, the connection
469 between the object and variable/function name is not as clear.
483 @@ Make the last brace of a function stand out
492 } /* -END- function1 */
497 } /* -END- function2 */
511 @@@ Note: Use 1 blank line before the closing brace and 2 lines
512 afterwards. This makes the end of function standout to the most
513 casual viewer. Although function comments help seperate functions,
514 this is still a good coding practice. In fact, I follow these rules
515 when using blocks in "for", "while", "do" loops, and long if {}
516 statements too. After all whitespace is free!
518 @@@ Status: developer-discrection on the number of blank lines.
519 Enforced is the end of function comments.
522 @@ Use 3 character indentions
526 If some use 8 character TABs and some use 3 character TABs, the code
527 can look *very* ragged. So use 3 character indentions only. If you
528 like to use TABs, pass your code through a filter such as "expand -t3"
529 before checking in your code.
533 static const char * const url_code_map[256] =
543 return( ALWAYS_TRUE );
547 return( HOW_DID_YOU_GET_HERE );
550 return( NEVER_GETS_HERE );
558 @@ Initialize all variables
562 Do not assume that the variables declared will not be used until
563 after they have been assigned a value somewhere else in the
564 code. Remove the chance of accidentally using an unassigned
573 @@@ Note: It is much easier to debug a SIGSEGV if the message says
574 you are trying to access memory address 00000000 and not
575 129FA012; or arrayPtr[20] causes a SIGSEV vs. arrayPtr[0].
577 @@@ Status: developer-discrection if and only if the variable is
578 assigned a value "shortly after" declaration.
584 @@ Name functions that return a boolean as a question.
588 Value should be phrased as a question that would logically be
589 answered as a true or false statement
598 @@ Always specify a return type for a function.
602 The default return for a function is an int. To avoid ambiguity,
603 create a return for a function when the return has a purpose, and
604 create a void return type if the function does not need to return
608 @@ Minimize function calls when iterating by using variables
612 It is easy to write the following code, and a clear argument can
613 be made that the code is easy to understand:
617 for ( size_t cnt = 0; cnt < blockListLength(); cnt ++ )
622 @@@ Note: Unfortunately, this makes a function call for each and every
623 iteration. This increases the overhead in the program, because the
624 compiler has to look up the function each time, call it, and return a
625 value. Depending on what occurs in the blockListLength() call, it
626 might even be creating and destroying structures with each iteration,
627 even though in each case it is comparing "cnt" to the same value, over
628 and over. Remember too - even a call to blockListLength() is a
629 function call, with the same overhead.
631 Instead of using a function call during the iterations, assign
632 the value to a variable, and evaluate using the variable.
636 size_t len = blockListLength();
638 for ( size_t cnt = 0; cnt < len; cnt ++ )
643 @@@ Exceptions: if the value of blockListLength() *may* change or could
644 *potentially* change, then you must code the function call in the
648 @@ Pass and Return by Const Reference
652 This allows a developer to define a const pointer and call your
653 function. If your function does not have the const keyword, we
654 may not be able to use your function. Consider strcmp, if it
656 extern int strcmp( char *s1, char *s2 );
658 I could then not use it to compare argv's in main:
659 int main( int argc, const char *argv[] )
661 strcmp( argv[0], "junkbusters" );
664 Both these pointers are *const*! If the c runtime library maintainers
665 do it, we should too.
668 @@ Pass and Return by Value
672 Most structures cannot fit onto a normal stack entry (i.e. they
673 are not 4 bytes or less). Aka, a function declaration like:
674 int load_aclfile( struct client_state csp )
676 would not work. So, to be consistent, we should declare all
677 prototypes with "pass by value":
678 int load_aclfile( struct client_state *csp )
681 @@ Use #include <fileName> and #include "fileName" for locals
685 Your include statements should contain the file name without a
686 path. The path should be listed in the Makefile, using -I as
687 processor directive to search the indicated paths. An exception
688 to this would be for some proprietary software that utilizes a
689 partial path to distinguish their header files from system or
694 #include <iostream.h> /* This is not a local include */
695 #include "config.h" /* This IS a local include */
699 /* This is not a local include, but requires a path element. */
700 #include <sys/fileName.h>
702 @@@ Note: Please! do not add "-I." to the Makefile without a _very_
703 good reason. This duplicates the #include "file.h" behaviour.
706 @@ Provide multiple inclusion protection
710 Prevents compiler and linker errors resulting from redefinition of
713 Wrap each header file with the following syntax to prevent multiple
714 inclusions of the file. Of course, replace FILENAME_UPPERCASE with
715 your file name, with "." Changed to "_", and make it uppercase.
722 #endif /* ndef _PROJECT_H */
725 @@ Where Possible, Use Forward Struct Declaration Instead of Includes
729 Useful in headers that include pointers to other struct's.
730 Modifications to excess header files may cause needless compiles.
734 /*********************************************************************
735 * We're avoiding an include statement here!
736 *********************************************************************/
738 extern file_list *xyz;
740 @@@ Note: If you declare "file_list xyz;" (without the pointer), then
741 including the proper header file is necessary. If you only want to
742 prototype a pointer, however, the header file is unneccessary.
744 @@@ Status: Use with discrection.
747 @ General Coding Practices
750 @@ Provide a default case for all switch statements
754 What you think is guaranteed is never really guaranteed. The value
755 that you don't think you need to check is the one that someday will be
756 passed. So, to protect yourself from the unknown, always have a
757 default step in a switch statement.
761 switch( hash_string( cmd ) )
763 case hash_actions_file :
773 ... anomly code goes here ...
774 continue; / break; / exit( 1 ); / etc ...
776 } /* end switch( hash_string( cmd ) ) */
778 @@@ Note: If you already have a default condition, you are obviously
779 exempt from this point. Of note, most of the WIN32 code calls
780 `DefWindowProc' after the switch statement. This API call
781 *should* be included in a default statement.
783 @@@ Another Note: This is not so much a readability issue as a robust
784 programming issue. The "anomly code goes here" may be no more than a
785 print to the STDERR stream (as in load_config). Or it may really be
788 @@@ Status: Programmer discretion is advised.
791 @@ Try to avoid falling through cases in a switch statement.
795 In general, you will want to have a 'break' statement within each
796 'case' of a switch statement. This allows for the code to be more
797 readable and understandable, and furthermore can prevent unwanted
798 surprises if someone else later gets creative and moves the code
801 The language allows you to plan the fall through from one case
802 statement to another simply by omitting the break statement within the
803 case statement. This feature does have benefits, but should only be
804 used in rare cases. In general, use a break statement for each case
807 If you choose to allow fall through, you should comment both the fact
808 of the fall through and reason why you felt it was necessary.
811 @@ Use 'long' or 'short' Instead of 'int'
815 On 32-bit platforms, int usually has the range of long. On 16-bit
816 platforms, int has the range of short.
818 @@@ Status: open-to-debate. In the case of most FSF projects
819 (including X/GNU-Emacs), there are typedefs to int4, int8, int16, (or
820 equivalence ... I forget the exact typedefs now). Should we add these
821 to IJB now that we have a "configure" script?
824 @@ Declare each variable and struct on its own line.
828 It can be tempting to declare a series of variables all on one line.
842 - there is more room for comments on the individual variables
843 - easier to add new variables without messing up the original ones
844 - when searching on a variable to find its type, there is less
845 clutter to "visually" eliminate
847 @@@ Exceptions: when you want to declare a bunch of loop variables or
848 other trivial variables; feel free to declare them on 1 line. You
849 should, although, provide a good comment on their functions.
851 @@@ Status: developer-discrection.
854 @@ Use malloc/zalloc sparingly
858 Create a local stuct (on the stack) if the variable will live
859 and die within the context of one function call.
861 Only "malloc" a struct (on the heap) if the variable's life will
862 extend beyond the context of one function call.
866 If a function creates a struct and stores a pointer to it in a
867 list, then it should definately be allocated via `malloc'.
870 @@ The Programmer Who Uses 'malloc' is Responsible for Ensuring 'free'
874 If you have to "malloc" an instance, you are responsible for insuring
875 that the instance is `free'd, even if the deallocation event falls
876 within some other programmer's code. You are also responsible for
877 ensuring that deletion is timely (i.e. not too soon, not too late).
878 This is known as "low-coupling" and is a "good thing (tm)". You may
879 need to offer a free/unload/destuctor type function to accomodate
884 int load_re_filterfile( struct client_state *csp ) { ... }
885 static void unload_re_filterfile( void *f ) { ... }
889 The developer cannot be expected to provide `free'ing functions for
890 C run-time library functions ... such as `strdup'.
892 @@@ Status: developer-discrection. The "main" use of this standard is
893 for allocating and freeing data structures (complex or nested).
896 @@ Add loaders to the `file_list' structure and in order
900 I have ordered all of the "blocker" file code to be in alpha
901 order. It is easier to add/read new blockers when you expect a
904 @@@ Note: It may appear that the alpha order is broken in places by
905 POPUP tests coming before PCRS tests. But since POPUPs can also
906 be referred to as KILLPOPUPs, it is clear that it should come
910 @@ "Uncertain" new code and/or changes to exitinst code, use FIXME
914 If you have enough confidence in new code or confidence in your
915 changes, but are not *quite* sure of the reprocussions, add this:
917 /* FIXME: this code has a logic error on platform XYZ,
921 ...changed code here...
926 /* FIXME: I think the original author really meant this... */
927 ...changed code here...
931 /* FIXME: new code that *may* break something else... */
934 @@@ Note: If you make it clear that this may or may not be a "good
935 thing (tm)", it will be easier to identify and include in the project
936 (or conversly exclude from the project).
939 @ Addendum: Template for files and function comment blocks:
942 @@@ Example for file comments:
944 const char FILENAME_rcs[] = "$Id: STANDARDS.txt,v 1.2 2001/06/28 04:02:42 iwanttokeepanon Exp $";
945 /*********************************************************************
947 * File : $Source: /cvsroot/ijbswa/current/doc/STANDARDS.txt,v $
949 * Purpose : (Fill me in with a good description!)
951 * Copyright : Written by and Copyright (C) 2001 the SourceForge
952 * IJBSWA team. http://ijbswa.sourceforge.net
954 * Based on the Internet Junkbuster originally written
955 * by and Copyright (C) 1997 Anonymous Coders and
956 * Junkbusters Corporation. http://www.junkbusters.com
958 * This program is free software; you can redistribute it
959 * and/or modify it under the terms of the GNU General
960 * Public License as published by the Free Software
961 * Foundation; either version 2 of the License, or (at
962 * your option) any later version.
964 * This program is distributed in the hope that it will
965 * be useful, but WITHOUT ANY WARRANTY; without even the
966 * implied warranty of MERCHANTABILITY or FITNESS FOR A
967 * PARTICULAR PURPOSE. See the GNU General Public
968 * License for more details.
970 * The GNU General Public License should be included with
971 * this file. If not, you can view it at
972 * http://www.gnu.org/copyleft/gpl.html
973 * or write to the Free Software Foundation, Inc., 59
974 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
977 * $Log: STANDARDS.txt,v $
978 * Revision 1.2 2001/06/28 04:02:42 iwanttokeepanon
979 * Testing XEmacs and VC/CVS modes. Will this work? We shall see...
981 * Revision 1.1 2001/06/28 03:01:32 iwanttokeepanon
982 * A suggested standard for IJB. Outline-mode formatting and spell checking to follow. Developer comments encouraged and requested.
985 *********************************************************************/
990 ...necessary include files for us to do our work...
992 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
995 @@@ Note: This declares the rcs variables that should be added to the
996 "show-proxy-args" page. If this is a brand new creation by you, you
997 are free to change the "Copyright" section to represent the rights you
1000 @@@ Note: The formfeed character that is present right after the
1001 comment flower box is handy for (X|GNU)Emacs users to skip the verbige
1002 and get to the heart of the code (via `forward-page' and
1003 `backward-page'). Please include it if you can.
1005 @@@ Example for file header comments:
1009 #define FILENAME_H_VERSION "$Id: STANDARDS.txt,v 1.2 2001/06/28 04:02:42 iwanttokeepanon Exp $"
1010 /*********************************************************************
1012 * File : $Source: /cvsroot/ijbswa/current/doc/STANDARDS.txt,v $
1014 * Purpose : (Fill me in with a good description!)
1016 * Copyright : Written by and Copyright (C) 2001 the SourceForge
1017 * IJBSWA team. http://ijbswa.sourceforge.net
1019 * Based on the Internet Junkbuster originally written
1020 * by and Copyright (C) 1997 Anonymous Coders and
1021 * Junkbusters Corporation. http://www.junkbusters.com
1023 * This program is free software; you can redistribute it
1024 * and/or modify it under the terms of the GNU General
1025 * Public License as published by the Free Software
1026 * Foundation; either version 2 of the License, or (at
1027 * your option) any later version.
1029 * This program is distributed in the hope that it will
1030 * be useful, but WITHOUT ANY WARRANTY; without even the
1031 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1032 * PARTICULAR PURPOSE. See the GNU General Public
1033 * License for more details.
1035 * The GNU General Public License should be included with
1036 * this file. If not, you can view it at
1037 * http://www.gnu.org/copyleft/gpl.html
1038 * or write to the Free Software Foundation, Inc., 59
1039 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1042 * $Log: STANDARDS.txt,v $
1043 * Revision 1.2 2001/06/28 04:02:42 iwanttokeepanon
1044 * Testing XEmacs and VC/CVS modes. Will this work? We shall see...
1046 * Revision 1.1 2001/06/28 03:01:32 iwanttokeepanon
1047 * A suggested standard for IJB. Outline-mode formatting and spell checking to follow. Developer comments encouraged and requested.
1050 *********************************************************************/
1053 #include "project.h"
1059 ... function headers here ...
1062 /* Revision control strings from this header and associated .c file */
1063 extern const char FILENAME_rcs[];
1064 extern const char FILENAME_h_rcs[];
1071 #endif /* ndef _FILENAME_H */
1080 @@@ Example for function comments:
1082 /*********************************************************************
1084 * Function : FUNCTION_NAME
1086 * Description : (Fill me in with a good description!)
1089 * 1 : param1 = pointer to an important thing
1090 * 2 : x = pointer to something else
1092 * Returns : 0 => Ok, everything else is an error.
1094 *********************************************************************/
1095 int FUNCTION_NAME( void *param1, const char *x )
1103 @@@ Note: If we all follow this practice, we should be able to parse
1104 our code to create a "self-documenting" web page.
1107 @ Local variables for this standards file
1114 outline-regexp: "[@]+"