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 Seperate words via an underscore ('_').
222 int ms_iis5_hack = 0;
234 Seperate words via an underscore ('_').
238 int load_aclfile( struct client_state *csp )
242 int loadaclfile( struct client_state *csp )
243 int loadAclFile( struct client_state *csp )
246 @@ Header file prototypes
250 Use a descriptive parameter name in the function prototype in
251 header files. Use the same parameter name in the header file
252 that you use in the c file.
256 (.h) extern int load_aclfile( struct client_state *csp );
257 (.c) int load_aclfile( struct client_state *csp )
260 (.h) extern int load_aclfile( struct client_state * ); or
261 (.h) extern int load_aclfile();
262 (.c) int load_aclfile( struct client_state *csp )
265 18. Ennumerations, and #defines
269 Use all capital letters, with underscores between words.
273 (enumeration) : enum Boolean { FALSE, TRUE };
274 (#define) : #define DEFAULT_SIZE 100;
276 @@@ Note: We should have a standard naming scheme for Symbols that
277 toggle a feature in the precompiler, and the constants used by that
278 feature. I'd propose that the toggles should be just one word, with
279 a common prefix, and that any depandant constants should be
280 prefixed by that word.
282 The prefix could be WITH_, HAVE_, ENABLE_, FEATURE_ etc.
284 @@@ Status: I see some this in the code currently! Anybody "figured"
285 out a standard way to do this?
289 #define ENABLE_FORCE 1
292 #define FORCE_PREFIX blah
293 #endif /* def ENABLE_FORCE */
300 Spell common words out entirely (do not remove vowels).
302 Use only widely-known domain acronyms and abbreviations. Capitalize
303 all letters of an acronym.
305 Use underscore (_) to separate adjacent acronyms and
306 abbreviations. Never terminate a name with an underscore.
310 #define USE_IMAGE_LIST 1
314 #define USE_IMG_LST 1 or
315 #define _USE_IMAGE_LIST 1 or
316 #define USE_IMAGE_LIST_ 1 or
317 #define use_image_list 1 or
318 #define UseImageList 1
324 @@ Put braces on a line by themselves.
328 The brace needs to be on a line all by itself, not at the end of
329 the statement. Curly braces should line up with the construct
330 that they're associated with. This practice makes it easier to
331 identify the opening and closing braces for a block.
342 if ( this == that ) {
348 if ( this == that ) { ... }
350 @@@ Note: In the special case that the if-statement is inside a loop,
351 and it is trivial, i.e. it tests for a condidtion that is obvious
352 from the purpose of the block, one-liners as above may optically
353 preserve the loop structure and make it easier to read.
355 @@@ Status: developer-discrection.
357 @@@ Example exception:
359 while ( more lines are read )
361 /* Please document what is/is not a comment line here */
362 if ( it's a comment ) continue;
364 do_something( line );
368 @@ ALL control statements should have a block
372 Using braces to make a block will make your code more readable
373 and less prone to error. All control statements should have a
392 if ( this == that ) DoSomething();
394 @@@ Note: The first example in "Instead of" will execute in a manner
395 other than that which the developer desired (per indentation). Using
396 code braces would have prevented this "feature". The "explanation"
397 and "exception" from the point above also applies.
400 @@ Do not belabor/blow-up boolean expressions
404 structure->flag = ( condition );
417 @@@ Note: The former is readable and consice. The later is wordy and
418 inefficient. Please assume that any developer new to the project has
419 at least a "good" knowledge of C/C++. (Hope I do not offend by that
423 @@ Use white space freely because it is free
427 Make it readable. The notable exception to using white space
428 freely is listed in the next guideline.
434 int anotherValue = 0;
435 int thisVariable = 0;
437 if ( thisVariable == thatVariable )
439 firstValue = oldValue + ( ( someValue - anotherValue ) - whatever )
442 @@ Don't use white space around structure operators
446 - structure pointer operator ( "->" )
447 - member operator ( "." )
448 - functions and parentheses
450 It is a general coding practice to put pointers, references, and
451 function parentheses next to names. With spaces, the connection
452 between the object and variable/function name is not as clear.
466 @@ Make the last brace of a function stand out
475 } /* -END- function1 */
480 } /* -END- function2 */
494 @@@ Note: Use 1 blank line before the closing brace and 2 lines
495 afterwards. This makes the end of function standout to the most
496 casual viewer. Although function comments help seperate functions,
497 this is still a good coding practice. In fact, I follow these rules
498 when using blocks in "for", "while", "do" loops, and long if {}
499 statements too. After all whitespace is free!
501 @@@ Status: developer-discrection on the number of blank lines.
502 Enforced is the end of function comments.
505 @@ Use 3 character indentions
509 If some use 8 character TABs and some use 3 character TABs, the code
510 can look *very* ragged. So use 3 character indentions only. If you
511 like to use TABs, pass your code through a filter such as "expand -t3"
512 before checking in your code.
516 static const char * const url_code_map[256] =
526 return( ALWAYS_TRUE );
530 return( HOW_DID_YOU_GET_HERE );
533 return( NEVER_GETS_HERE );
541 @@ Initialize all variables
545 Do not assume that the variables declared will not be used until
546 after they have been assigned a value somewhere else in the
547 code. Remove the chance of accidentally using an unassigned
556 @@@ Note: It is much easier to debug a SIGSEGV if the message says
557 you are trying to access memory address 00000000 and not
558 129FA012; or arrayPtr[20] causes a SIGSEV vs. arrayPtr[0].
560 @@@ Status: developer-discrection if and only if the variable is
561 assigned a value "shortly after" declaration.
567 @@ Name functions that return a boolean as a question.
571 Value should be phrased as a question that would logically be
572 answered as a true or false statement
581 @@ Always specify a return type for a function.
585 The default return for a function is an int. To avoid ambiguity,
586 create a return for a function when the return has a purpose, and
587 create a void return type if the function does not need to return
591 @@ Minimize function calls when iterating by using variables
595 It is easy to write the following code, and a clear argument can
596 be made that the code is easy to understand:
600 for ( size_t cnt = 0; cnt < blockListLength(); cnt ++ )
605 @@@ Note: Unfortunately, this makes a function call for each and every
606 iteration. This increases the overhead in the program, because the
607 compiler has to look up the function each time, call it, and return a
608 value. Depending on what occurs in the blockListLength() call, it
609 might even be creating and destroying structures with each iteration,
610 even though in each case it is comparing "cnt" to the same value, over
611 and over. Remember too - even a call to blockListLength() is a
612 function call, with the same overhead.
614 Instead of using a function call during the iterations, assign
615 the value to a variable, and evaluate using the variable.
619 size_t len = blockListLength();
621 for ( size_t cnt = 0; cnt < len; cnt ++ )
626 @@@ Exceptions: if the value of blockListLength() *may* change or could
627 *potentially* change, then you must code the function call in the
631 @@ Pass and Return by Const Reference
635 This allows a developer to define a const pointer and call your
636 function. If your function does not have the const keyword, we
637 may not be able to use your function. Consider strcmp, if it
639 extern int strcmp( char *s1, char *s2 );
641 I could then not use it to compare argv's in main:
642 int main( int argc, const char *argv[] )
644 strcmp( argv[0], "junkbusters" );
647 Both these pointers are *const*! If the c runtime library maintainers
648 do it, we should too.
651 @@ Pass and Return by Value
655 Most structures cannot fit onto a normal stack entry (i.e. they
656 are not 4 bytes or less). Aka, a function declaration like:
657 int load_aclfile( struct client_state csp )
659 would not work. So, to be consistent, we should declare all
660 prototypes with "pass by value":
661 int load_aclfile( struct client_state *csp )
664 @@ Use #include <fileName> and #include "fileName" for locals
668 Your include statements should contain the file name without a
669 path. The path should be listed in the Makefile, using -I as
670 processor directive to search the indicated paths. An exception
671 to this would be for some proprietary software that utilizes a
672 partial path to distinguish their header files from system or
677 #include <iostream.h> /* This is not a local include */
678 #include "config.h" /* This IS a local include */
682 /* This is not a local include, but requires a path element. */
683 #include <sys/fileName.h>
685 @@@ Note: Please! do not add "-I." to the Makefile without a _very_
686 good reason. This duplicates the #include "file.h" behaviour.
689 @@ Provide multiple inclusion protection
693 Prevents compiler and linker errors resulting from redefinition of
696 Wrap each header file with the following syntax to prevent multiple
697 inclusions of the file. Of course, replace FILENAME_UPPERCASE with
698 your file name, with "." Changed to "_", and make it uppercase.
705 #endif /* ndef _PROJECT_H */
708 @@ Where Possible, Use Forward Struct Declaration Instead of Includes
712 Useful in headers that include pointers to other struct's.
713 Modifications to excess header files may cause needless compiles.
717 /*********************************************************************
718 * We're avoiding an include statement here!
719 *********************************************************************/
721 extern file_list *xyz;
723 @@@ Note: If you declare "file_list xyz;" (without the pointer), then
724 including the proper header file is necessary. If you only want to
725 prototype a pointer, however, the header file is unneccessary.
727 @@@ Status: Use with discrection.
730 @ General Coding Practices
733 @@ Provide a default case for all switch statements
737 What you think is guaranteed is never really guaranteed. The value
738 that you don't think you need to check is the one that someday will be
739 passed. So, to protect yourself from the unknown, always have a
740 default step in a switch statement.
744 switch( hash_string( cmd ) )
746 case hash_actions_file :
756 ... anomly code goes here ...
757 continue; / break; / exit( 1 ); / etc ...
759 } /* end switch( hash_string( cmd ) ) */
761 @@@ Note: If you already have a default condition, you are obviously
762 exempt from this point. Of note, most of the WIN32 code calls
763 `DefWindowProc' after the switch statement. This API call
764 *should* be included in a default statement.
766 @@@ Another Note: This is not so much a readability issue as a robust
767 programming issue. The "anomly code goes here" may be no more than a
768 print to the STDERR stream (as in load_config). Or it may really be
771 @@@ Status: Programmer discretion is advised.
774 @@ Try to avoid falling through cases in a switch statement.
778 In general, you will want to have a 'break' statement within each
779 'case' of a switch statement. This allows for the code to be more
780 readable and understandable, and furthermore can prevent unwanted
781 surprises if someone else later gets creative and moves the code
784 The language allows you to plan the fall through from one case
785 statement to another simply by omitting the break statement within the
786 case statement. This feature does have benefits, but should only be
787 used in rare cases. In general, use a break statement for each case
790 If you choose to allow fall through, you should comment both the fact
791 of the fall through and reason why you felt it was necessary.
794 @@ Use 'long' or 'short' Instead of 'int'
798 On 32-bit platforms, int usually has the range of long. On 16-bit
799 platforms, int has the range of short.
801 @@@ Status: open-to-debate. In the case of most FSF projects
802 (including X/GNU-Emacs), there are typedefs to int4, int8, int16, (or
803 equivalence ... I forget the exact typedefs now). Should we add these
804 to IJB now that we have a "configure" script?
807 @@ Declare each variable and struct on its own line.
811 It can be tempting to declare a series of variables all on one line.
825 - there is more room for comments on the individual variables
826 - easier to add new variables without messing up the original ones
827 - when searching on a variable to find its type, there is less
828 clutter to "visually" eliminate
830 @@@ Exceptions: when you want to declare a bunch of loop variables or
831 other trivial variables; feel free to declare them on 1 line. You
832 should, although, provide a good comment on their functions.
834 @@@ Status: developer-discrection.
837 @@ Use malloc/zalloc sparingly
841 Create a local stuct (on the stack) if the variable will live
842 and die within the context of one function call.
844 Only "malloc" a struct (on the heap) if the variable's life will
845 extend beyond the context of one function call.
849 If a function creates a struct and stores a pointer to it in a
850 list, then it should definately be allocated via `malloc'.
853 @@ The Programmer Who Uses 'malloc' is Responsible for Ensuring 'free'
857 If you have to "malloc" an instance, you are responsible for insuring
858 that the instance is `free'd, even if the deallocation event falls
859 within some other programmer's code. You are also responsible for
860 ensuring that deletion is timely (i.e. not too soon, not too late).
861 This is known as "low-coupling" and is a "good thing (tm)". You may
862 need to offer a free/unload/destuctor type function to accomodate
867 int load_re_filterfile( struct client_state *csp ) { ... }
868 static void unload_re_filterfile( void *f ) { ... }
872 The developer cannot be expected to provide `free'ing functions for
873 C run-time library functions ... such as `strdup'.
875 @@@ Status: developer-discrection. The "main" use of this standard is
876 for allocating and freeing data structures (complex or nested).
879 @@ Add loaders to the `file_list' structure and in order
883 I have ordered all of the "blocker" file code to be in alpha
884 order. It is easier to add/read new blockers when you expect a
887 @@@ Note: It may appear that the alpha order is broken in places by
888 POPUP tests coming before PCRS tests. But since POPUPs can also
889 be referred to as KILLPOPUPs, it is clear that it should come
893 @@ "Uncertain" new code and/or changes to exitinst code, use FIXME
897 If you have enough confidence in new code or confidence in your
898 changes, but are not *quite* sure of the reprocussions, add this:
900 /* FIXME: this code has a logic error on platform XYZ,
904 ...changed code here...
909 /* FIXME: I think the original author really meant this... */
910 ...changed code here...
914 /* FIXME: new code that *may* break something else... */
917 @@@ Note: If you make it clear that this may or may not be a "good
918 thing (tm)", it will be easier to identify and include in the project
919 (or conversly exclude from the project).
922 @ Addendum: Template for files and function comment blocks:
925 @@@ Example for file comments:
927 const char FILENAME_rcs[] = "$Id: STANDARDS.txt,v 1.3 2001/07/02 01:50:04 iwanttokeepanon Exp $";
928 /*********************************************************************
930 * File : $Source: /cvsroot/ijbswa/current/doc/STANDARDS.txt,v $
932 * Purpose : (Fill me in with a good description!)
934 * Copyright : Written by and Copyright (C) 2001 the SourceForge
935 * IJBSWA team. http://ijbswa.sourceforge.net
937 * Based on the Internet Junkbuster originally written
938 * by and Copyright (C) 1997 Anonymous Coders and
939 * Junkbusters Corporation. http://www.junkbusters.com
941 * This program is free software; you can redistribute it
942 * and/or modify it under the terms of the GNU General
943 * Public License as published by the Free Software
944 * Foundation; either version 2 of the License, or (at
945 * your option) any later version.
947 * This program is distributed in the hope that it will
948 * be useful, but WITHOUT ANY WARRANTY; without even the
949 * implied warranty of MERCHANTABILITY or FITNESS FOR A
950 * PARTICULAR PURPOSE. See the GNU General Public
951 * License for more details.
953 * The GNU General Public License should be included with
954 * this file. If not, you can view it at
955 * http://www.gnu.org/copyleft/gpl.html
956 * or write to the Free Software Foundation, Inc., 59
957 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
960 * $Log: STANDARDS.txt,v $
961 * Revision 1.3 2001/07/02 01:50:04 iwanttokeepanon
962 * A modified STANDARDS.txt file. I removed my XEmacs test lines (commited in v1.2)
963 * and made the file Outline-mode compatible. I used "@" for my header lines instead
964 * of "*" (which interfered in some C comments). I also removed the formfeed character
965 * from the `outline-regexp' variable because it interfered with the "H" file header and
966 * the "C" file header comments.
968 * All of the "stardards points/issues" are still available for discussion, cussing,
969 * and/or flaming <G>.
971 * Revision 1.2 2001/06/28 04:02:42 iwanttokeepanon
972 * Testing XEmacs and VC/CVS modes. Will this work? We shall see...
974 * Revision 1.1 2001/06/28 03:01:32 iwanttokeepanon
975 * A suggested standard for IJB. Outline-mode formatting and spell checking to follow. Developer comments encouraged and requested.
978 *********************************************************************/
983 ...necessary include files for us to do our work...
985 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
988 @@@ Note: This declares the rcs variables that should be added to the
989 "show-proxy-args" page. If this is a brand new creation by you, you
990 are free to change the "Copyright" section to represent the rights you
993 @@@ Note: The formfeed character that is present right after the
994 comment flower box is handy for (X|GNU)Emacs users to skip the verbige
995 and get to the heart of the code (via `forward-page' and
996 `backward-page'). Please include it if you can.
998 @@@ Example for file header comments:
1002 #define FILENAME_H_VERSION "$Id: STANDARDS.txt,v 1.3 2001/07/02 01:50:04 iwanttokeepanon Exp $"
1003 /*********************************************************************
1005 * File : $Source: /cvsroot/ijbswa/current/doc/STANDARDS.txt,v $
1007 * Purpose : (Fill me in with a good description!)
1009 * Copyright : Written by and Copyright (C) 2001 the SourceForge
1010 * IJBSWA team. http://ijbswa.sourceforge.net
1012 * Based on the Internet Junkbuster originally written
1013 * by and Copyright (C) 1997 Anonymous Coders and
1014 * Junkbusters Corporation. http://www.junkbusters.com
1016 * This program is free software; you can redistribute it
1017 * and/or modify it under the terms of the GNU General
1018 * Public License as published by the Free Software
1019 * Foundation; either version 2 of the License, or (at
1020 * your option) any later version.
1022 * This program is distributed in the hope that it will
1023 * be useful, but WITHOUT ANY WARRANTY; without even the
1024 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1025 * PARTICULAR PURPOSE. See the GNU General Public
1026 * License for more details.
1028 * The GNU General Public License should be included with
1029 * this file. If not, you can view it at
1030 * http://www.gnu.org/copyleft/gpl.html
1031 * or write to the Free Software Foundation, Inc., 59
1032 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1035 * $Log: STANDARDS.txt,v $
1036 * Revision 1.3 2001/07/02 01:50:04 iwanttokeepanon
1037 * A modified STANDARDS.txt file. I removed my XEmacs test lines (commited in v1.2)
1038 * and made the file Outline-mode compatible. I used "@" for my header lines instead
1039 * of "*" (which interfered in some C comments). I also removed the formfeed character
1040 * from the `outline-regexp' variable because it interfered with the "H" file header and
1041 * the "C" file header comments.
1043 * All of the "stardards points/issues" are still available for discussion, cussing,
1044 * and/or flaming <G>.
1046 * Revision 1.2 2001/06/28 04:02:42 iwanttokeepanon
1047 * Testing XEmacs and VC/CVS modes. Will this work? We shall see...
1049 * Revision 1.1 2001/06/28 03:01:32 iwanttokeepanon
1050 * A suggested standard for IJB. Outline-mode formatting and spell checking to follow. Developer comments encouraged and requested.
1053 *********************************************************************/
1056 #include "project.h"
1062 ... function headers here ...
1065 /* Revision control strings from this header and associated .c file */
1066 extern const char FILENAME_rcs[];
1067 extern const char FILENAME_h_rcs[];
1074 #endif /* ndef _FILENAME_H */
1083 @@@ Example for function comments:
1085 /*********************************************************************
1087 * Function : FUNCTION_NAME
1089 * Description : (Fill me in with a good description!)
1092 * 1 : param1 = pointer to an important thing
1093 * 2 : x = pointer to something else
1095 * Returns : 0 => Ok, everything else is an error.
1097 *********************************************************************/
1098 int FUNCTION_NAME( void *param1, const char *x )
1106 @@@ Note: If we all follow this practice, we should be able to parse
1107 our code to create a "self-documenting" web page.
1110 @ Local variables for this standards file
1117 outline-regexp: "[@]+"