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();
91 If you are trying to add a small logic comment and do not wish to
92 "disrubt" the flow of the code, feel free to use a 1 line comment
93 which is NOT on the same line as the code.
96 * Keep Comments on their own line
100 It goes back to the question of readability. If the comment is on
101 the same line as the code it will be harder to read than the
102 comment that is on its own line.
104 There are three exceptions to this rule, which should be violated
105 freely and often: during the definition of variables, at the end
106 of closing braces, when used to comment parameters.
110 /*********************************************************************
111 * This will stand out clearly in your code,
112 * But the second example won't.
113 *********************************************************************/
114 if ( thisVariable == thatVariable )
116 DoSomethingVeryImportant();
119 if ( thisVariable == thatVariable ) /*can you see me?*/
121 DoSomethingVeryImportant(); /*not easily*/
125 /*********************************************************************
126 * But, the encouraged exceptions:
127 *********************************************************************/
128 int urls_read = 0; /* # of urls read + rejected */
129 int urls_rejected = 0; /* # of urls rejected */
133 DoSomethingVeryImportant();
137 short DoSomethingVeryImportant(
138 short firstParam, /* represents something */
139 short nextParam /* represents something else */ )
143 } /* -END- DoSomethingVeryImportant */
146 * Comment each logical step
150 Logical steps should be commented to help others follow the
151 intent of the written code and comments will make the code more
154 If you have 25 lines of code without a comment, you should
155 probably go back into it to see where you forgot to put one.
157 Most "for", "while", "do", etc... loops _probably_ need a
158 comment. After all, these are usually major logic containers.
161 * Comment All Functions Thoroughly
165 A reader of the code should be able to look at the comments just
166 prior to the beginning of a function and discern the reason for
167 its existence and the consequences of using it. The reader
168 should not have to read through the code to determine if a given
169 function is safe for a desired use. The proper information
170 thoroughly presented at the introduction of a function not only
171 saves time for subsequent maintenance or debugging, it more
172 importantly aids in code reuse by allowing a user to determine
173 the safety and applicability of any function for the problem at
174 hand. As a result of such benefits, all functions should contain
175 the information presented in the addendum section of this
179 * Comment at the end of braces if the content is more than one screen length
183 Each closing brace should be followed on the same line by a
184 comment that describes the origination of the brace if the
185 original brace is off of the screen, or otherwise far away from
186 the closing brace. This will simplify the debugging, maintenance,
187 and readability of the code.
189 As a suggestion , use the following flags to make the comment and
190 its brace more readable:
192 use following a closing brace:
193 } /* -END- if() or while () or etc... */
199 DoSomethingVeryImportant();
200 ...some long list of commands...
201 } /* -END- if x is 1 */
207 DoSomethingVeryImportant();
208 ...some long list of commands...
209 } /* -END- if ( 1 == X ) */
212 ** Naming Conventions
219 Capitalize the first letter of each word in a variable name
220 except the first word.
229 int ms_iis5_hack = 0;
231 Note: This is not an "enforcable" issue since too much of IJB uses
232 the underscore (_) to seperate words.
234 Status: developer-descrition. This item is at developer
235 discrection and is currently open to debate by the IJB developer
236 team. Too much of IJB violates this proposed standard.
243 Capitalize the first letter of each word in a function name.
247 int loadAclFile(struct client_state *csp)
251 int load_aclfile(struct client_state *csp)
253 Status: developer-descrition. This item is at developer
254 discrection and is currently open to debate by the IJB developer
255 team. Too much of IJB violates this proposed standard.
257 Note: on the 2 above "standards" ... if enough of the current
258 developer team agrees, I can use XEmacs to apply a few regular
259 expressions to make the current tree comply with these 2 points.
260 Otherwise I will remove them from this document. Afterall, there
261 is no need to add to the "multiple personallity" syndrome that IJB
265 * Header file prototypes
269 Use a descriptive parameter name in the function prototype in
270 header files. Use the same parameter name in the header file
271 that you use in the c file.
275 (.h) extern int load_aclfile(struct client_state *csp);
276 (.c) int load_aclfile(struct client_state *csp)
279 (.h) extern int load_aclfile(struct client_state *); or
280 (.h) extern int load_aclfile();
283 18. Ennumerations, and #defines
287 Use all capital letters, with underscores between words.
291 (enumeration) : enum Boolean { FALSE, TRUE };
292 (#define) : #define DEFAULT_SIZE 100;
294 Note: We should have a standard naming scheme for Symbols that
295 toggle a feature in the precompiler, and the constants used by that
296 feature. I'd propose that the toggles should be just one word, with
297 a common prefix, and that any depandant constants should be
298 prefixed by that word.
300 The prefix could be WITH_, HAVE_, ENABLE_, FEATURE_ etc.
302 Status: I see some this in the code currently! Anybody "figured"
303 out a standard way to do this?
307 #define ENABLE_FORCE 1
310 #define FORCE_PREFIX blah
311 #endif /* def ENABLE_FORCE */
318 Spell common words out entirely (do not remove vowels).
320 Use only widely-known domain acronyms and abbreviations.
321 Capitalize all letters of an acronym.
323 Use underscore (_) to separate adjacent acronyms and
324 abbreviations. Never terminate a name with an underscore.
328 #define USE_IMAGE_LIST 1
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-descrition.
376 while (more lines are read)
378 /* Please document what is/is not a comment line here */
379 if (it's a comment) continue;
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
407 if ( this == that ) DoSomething();
409 Note: see the exception above.
412 * Do not belabor/blow-up boolean expressions
416 structure->flag = (condition);
429 Note: The former is readable and consice. The later is wordy and
430 inefficient. Please assume that any new developer has at least a
431 "good" knowledge of C/C++. (Hope I do not offend by that last
435 * Use white space freely because it is free
439 Make it readable. The notable exception to using white space
440 freely is listed in the next guideline.
446 int anotherValue = 0;
447 int thisVariable = 0;
449 if ( thisVariable == thatVariable )
451 firstValue = oldValue + ( ( someValue - anotherValue ) - whatever )
454 * Don't use white space around structure operators
458 - structure pointer operator ( "->" )
459 - member operator ( "." )
460 - functions and parentheses
462 It is a general coding practice to put pointers, references, and
463 function parentheses next to names. With spaces, the connection
464 between the object and variable/function name is not as clear.
478 * Make the last brace of a function stand out
487 } /* -END- function1 */
492 } /* -END- function2 */
506 NOTE: Use 1 blank line before the closing brace and 2 lines
507 afterwards. This makes the end of function standout to the most
508 casual viewer. Although function comments help seperate
509 functions, this is still a good coding practice. In fact, I
510 follow these rules when using blocks in "for", "while", "do"
511 loops, and long if {} statements too. After all whitespace is
514 Status: developer-descrition on the number of blank lines.
515 Enforced is the end of function comments.
518 * Use 3 character indentions
522 If some use 8 character TABs and some use 3 character TABs, the
523 code can look *very* ragged. So use 3 character indentions only.
524 If you like to use TABs, pass your code through a filter such as
525 "expand -t3" before checking in your code.
529 static const char * const url_code_map[256] =
539 return( ALWAYS_TRUE );
543 return( HOW_DID_YOU_GET_HERE );
546 return( NEVER_GETS_HERE );
554 * Initialize all variables
558 Do not assume that the variables declared will not be used until
559 after they have been assigned a value somewhere else in the
560 code. Remove the chance of accidentally using an unassigned
569 NOTE: It is much easier to debug a SIGSEGV if the message says
570 you are trying to access memory address 00000000 and not
571 129FA012; or arrayPtr[20] causes a SIGSEV vs. arrayPtr[0].
573 Status: developer-descrition if and only if the variable is
574 assigned a value "shortly after" declaration.
580 * Name functions that return a boolean as a question.
584 Value should be phrased as a question that would logically be
585 answered as a true or false statement
594 * Always specify a return type for a function.
598 The default return for a function is an int. To avoid ambiguity,
599 create a return for a function when the return has a purpose, and
600 create a void return type if the function does not need to return
604 * Minimize function calls when iterating by using variables
608 It is easy to write the following code, and a clear argument can
609 be made that the code is easy to understand:
613 for ( size_t curr = 0; curr < PageLength(); curr ++ )
618 Unfortunately, this makes a function call for each and every
619 iteration. This increases the overhead in the program, because
620 the compiler has to look up the function each time, call it, and
621 return a value. Depending on what occurs in the PageLength()
622 call, it might even be creating and destroying structures with
623 each iteration, even though in each case it is comparing "curr"
624 to the same value, over and over. Remember too - even a call to
625 PageLength() is a function call, with the same overhead.
627 Instead of using a function call during the iterations, assign
628 the value to a variable, and evaluate using the variable.
632 size_t len = PageLength();
634 for ( size_t curr = 0; curr < len; curr ++ )
639 Exceptions: if the value of PageLength() *may* change or could
640 *potentially* change, then you must code the function call in the
644 * Pass and Return by Const Reference
648 This allows a developer to define a const pointer and call your
649 function. If your function does not have the const keyword, we
650 may not be able to use your function. Consider strcmp, if it
652 extern int strcmp( char *s1, char *s2 );
654 I could then not use it to compare argv's in main:
655 int main( int argc, const char *argv[] )
657 strcmp( argv[0], "junkbusters" );
660 Both these pointers are *const*! If the c runtime library
661 maintainers do it, we should too.
664 * Pass and Return by Value
668 Most structures cannot fit onto a normal stack entry (i.e. they
669 are not 4 bytes or less). Aka, a function declaration like:
670 int load_aclfile(struct client_state csp)
672 would not work. So, to be consistent, we should declare all
673 prototypes with "pass by value":
674 int load_aclfile(struct client_state *csp)
677 * Use #include <fileName> and #include "fileName" for locals
681 Your include statements should contain the file name without a
682 path. The path should be listed in the Makefile, using -I as
683 processor directive to search the indicated paths. An exception
684 to this would be for some proprietary software that utilizes a
685 partial path to distinguish their header files from system or
690 #include <iostream.h> /* This is not a local include */
691 #include "config.h" /* This IS a local include */
695 /* This is not a local include, but requires a path element. */
696 #include <sys/fileName.h>
699 * Provide multiple inclusion protection
703 Prevents compiler and linker errors resulting from redefinition of
706 Wrap each header file with the following syntax to prevent multiple
707 inclusions of the file. Of course, replace FILENAME_UPPERCASE with
708 your file name, with "." Changed to "_", and make it uppercase.
710 Example (from project.h):
717 #endif /* ndef _PROJECT_H */
720 * Where Possible, Use Forward Struct Declaration Instead of Includes
724 Useful in headers that include pointers to other struct's.
725 Modifications to excess header files may cause needless compiles.
729 /*********************************************************************
730 * We're avoiding an include statement here!
731 *********************************************************************/
735 NOTE: If you declare "file_list xyz;" (without the pointer), then
736 including the proper header file is necessary. If you only want to
737 prototype a pointer, however, the header file is unneccessary. Use
741 ** General Coding Practices
744 * Provide a default case for all switch statements
748 What you think is guaranteed is never really guaranteed. The
749 value that you don't think you need to check is the one that
750 someday will be passed. So, to protect yourself from the unknown,
751 always have a default step in a switch statement.
755 switch( hash_string( cmd ) )
757 case hash_actions_file :
768 continue; / break; / exit(1); / etc ...
769 } /* end switch( hash_string(cmd) ) */
771 NOTE: If you already have a default condition, you are obviously
772 exempt from this point. Of note, most of the WIN32 code calls
773 `DefWindowProc' after the switch statement. This API call
774 *should* be included in a default statement.
776 Another NOTE: This is not so much a readability issue as a robust
777 programming issue. The "anomly code goes here" may be no more
778 than a print to the STDERR stream (as in load_config). Or it may
779 really be an ABEND condition. Programmer discretion is advised.
782 * Try to avoid falling through cases in a switch statement.
786 In general, you will want to have a 'break' statement within each
787 'case' of a switch statement. This allows for the code to be more
788 readable and understandable, and furthermore can prevent unwanted
789 surprises if someone else later gets creative and moves the code
792 The language allows you to plan the fall through from one case
793 statement to another simply by omitting the break statement within
794 the case statement. This feature does have benefits, but should
795 only be used in rare cases. In general, use a break statement for
798 If you choose to allow fall through, you should comment both the
799 fact of the fall through and reason why you felt it was
803 * Use 'long' or 'short' Instead of 'int'
807 On 32-bit platforms, int usually has the range of long. On 16-bit
808 platforms, int has the range of short.
810 Status: open-to-debate. In the case of most FSF projects
811 (including X/GNU-Emacs), there are typedefs to int4, int8, int16,
812 (or equivalence ... I forget the exact typedefs now). Should we
813 add these to IJB now that we have a "configure" script?
816 * Declare each variable and struct on its own line.
820 It can be tempting to declare a series of variables all on one
834 - there is more room for comments on the individual variables
835 - easier to add new variables without messing up the original ones
836 - when searching on a variable to find its type, there is less
837 clutter to "visually" eliminate
839 Exceptions: when you want to declare a bunch of loop variables or
840 other trivial variables; feel free to declare them on 1 line. You
841 should, although, provide a good comment on their functions.
843 Status: developer-descrition.
846 * Use malloc/zalloc sparingly
850 Create a local stuct (on the stack) if the variable will live
851 and die within the context of one function call.
853 Only "malloc" a struct (on the heap) if the variable's life will
854 extend beyond the context of one function call.
858 If a function creates a struct and stores a pointer to it in a
859 list, then it should definately be allocated via `malloc'.
862 * The Programmer Who Uses 'malloc' is Responsible for Ensuring 'free'
866 If you have to "malloc" an instance, you are responsible for
867 insuring that the instance is `free'd, even if the deallocation
868 event falls within some other programmer's code. You are also
869 responsible for ensuring that deletion is timely (i.e. not too
870 soon, not too late). This is known as "low-coupling" and is a
871 "good thing (tm)". You may need to offer a free/unload/destuctor
872 type function to accomodate this.
876 static void unload_re_filterfile(void *f) { ... }
877 int load_re_filterfile(struct client_state *csp) { ... }
881 The developer cannot be expected to provide `free'ing functions for
882 C run-time library functions ... such as `strdup'.
884 Status: developer-descrition. The "main" use of this standard is
885 for allocating and freeing data structures (complex or nested).
888 * Add loaders to the `file_list' structure and in order
892 I have ordered all of the "blocker" file code to be in alpha
893 order. It is easier to add/read new blockers when you expect a
896 NOTE: It may appear that the alpha order is broken in places by
897 POPUP tests coming before PCRS tests. But since POPUPs can also
898 be referred to as KILLPOPUPs, it is clear that it should come
902 * "Uncertain" new code and/or changes to exitinst code, use FIXME
906 If you have enough confidence in new code or confidence in your
907 changes, but are not *quite* sure of the reprocussions, add this:
909 /* FIXME: this code has a logic error on platform XYZ,
913 ...changed code here...
918 /* FIXME: I think the original author really meant this... */
919 ...changed code here...
923 /* FIXME: new code that *may* break something else... */
926 NOTE: If you make it clear that this may or may not be a "good
927 thing (tm)", it will be easier to identify and include in the
928 project (or conversly exclude from the project).
931 ** Addendum: Template for files and function comment blocks:
934 Example for file comments:
936 const char FILENAME_rcs[] = "$Id: STANDARDS.txt,v 1.1 2001/06/28 03:01:32 iwanttokeepanon Exp $";
937 /*********************************************************************
939 * File : $Source: /cvsroot/ijbswa/current/doc/STANDARDS.txt,v $
941 * Purpose : (Fill me in with a good description!)
943 * Copyright : Written by and Copyright (C) 2001 the SourceForge
944 * IJBSWA team. http://ijbswa.sourceforge.net
946 * Based on the Internet Junkbuster originally written
947 * by and Copyright (C) 1997 Anonymous Coders and
948 * Junkbusters Corporation. http://www.junkbusters.com
950 * This program is free software; you can redistribute it
951 * and/or modify it under the terms of the GNU General
952 * Public License as published by the Free Software
953 * Foundation; either version 2 of the License, or (at
954 * your option) any later version.
956 * This program is distributed in the hope that it will
957 * be useful, but WITHOUT ANY WARRANTY; without even the
958 * implied warranty of MERCHANTABILITY or FITNESS FOR A
959 * PARTICULAR PURPOSE. See the GNU General Public
960 * License for more details.
962 * The GNU General Public License should be included with
963 * this file. If not, you can view it at
964 * http://www.gnu.org/copyleft/gpl.html
965 * or write to the Free Software Foundation, Inc., 59
966 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
969 * $Log: STANDARDS.txt,v $
970 * Revision 1.1 2001/06/28 03:01:32 iwanttokeepanon
971 * A suggested standard for IJB. Outline-mode formatting and spell checking to follow. Developer comments encouraged and requested.
974 *********************************************************************/
979 ...necessary include files for us to do our work...
981 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
984 NOTE: This declares the rcs variables that should be added to the
985 "show-proxy-args" page. If this is a brand new creation by you,
986 you are free to change the "Copyright" section to represent the
987 rights you wish to maintain.
989 NOTE: The formfeed character that is present right after the
990 comment flower box is handy for (X|GNU)Emacs users to skip the
991 verbige and get to the heart of the code (via `forward-page' and
992 `backward-page'). Please include it if you can.
995 Example for file header comments:
999 #define FILENAME_H_VERSION "$Id: STANDARDS.txt,v 1.1 2001/06/28 03:01:32 iwanttokeepanon Exp $"
1000 /*********************************************************************
1002 * File : $Source: /cvsroot/ijbswa/current/doc/STANDARDS.txt,v $
1004 * Purpose : (Fill me in with a good description!)
1006 * Copyright : Written by and Copyright (C) 2001 the SourceForge
1007 * IJBSWA team. http://ijbswa.sourceforge.net
1009 * Based on the Internet Junkbuster originally written
1010 * by and Copyright (C) 1997 Anonymous Coders and
1011 * Junkbusters Corporation. http://www.junkbusters.com
1013 * This program is free software; you can redistribute it
1014 * and/or modify it under the terms of the GNU General
1015 * Public License as published by the Free Software
1016 * Foundation; either version 2 of the License, or (at
1017 * your option) any later version.
1019 * This program is distributed in the hope that it will
1020 * be useful, but WITHOUT ANY WARRANTY; without even the
1021 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1022 * PARTICULAR PURPOSE. See the GNU General Public
1023 * License for more details.
1025 * The GNU General Public License should be included with
1026 * this file. If not, you can view it at
1027 * http://www.gnu.org/copyleft/gpl.html
1028 * or write to the Free Software Foundation, Inc., 59
1029 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1032 * $Log: STANDARDS.txt,v $
1033 * Revision 1.1 2001/06/28 03:01:32 iwanttokeepanon
1034 * A suggested standard for IJB. Outline-mode formatting and spell checking to follow. Developer comments encouraged and requested.
1037 *********************************************************************/
1040 #include "project.h"
1046 ... function headers here ...
1049 /* Revision control strings from this header and associated .c file */
1050 extern const char FILENAME_rcs[];
1051 extern const char FILENAME_h_rcs[];
1058 #endif /* ndef _FILENAME_H */
1067 Example for function comments:
1069 /*********************************************************************
1071 * Function : FUNCTION_NAME
1073 * Description : (Fill me in with a good description!)
1076 * 1 : param1 = pointer to an important thing
1077 * 2 : x = pointer to something else
1079 * Returns : 0 => Ok, everything else is an error.
1081 *********************************************************************/
1082 int FUNCTION_NAME( void *param1, const char *x )
1090 NOTE: If we all follow this practice, we should be able to parse
1091 our code to create a "self-documenting" web page.
1094 ** XEmacs and VC mode
1099 These are the files I have in current/doc/CVS. I am experimenting
1100 with XEmacs and VC/CVS modes. Can I check this in? We will see...
1102 * current/doc/CVS/Entries:
1104 /USER_DOC_IS_WIDELY_OBSOLETED/1.1.1.1/Tue May 15 13:59:57 2001//
1105 /changes.txt/1.1.1.1/Tue May 15 13:59:50 2001//
1106 /fb.gif/1.1/Thu May 17 22:56:17 2001//
1107 /gpl.html/1.2/Thu May 17 22:56:17 2001//
1108 /ijbfaq.html/1.2/Thu May 17 22:56:17 2001//
1109 /ijbman.html/1.2/Thu May 17 22:56:17 2001//
1110 /top.gif/1.1/Thu May 17 22:56:17 2001//
1112 /STANDARDS.txt/1.1/Thu Jun 28 03:01:32 2001//
1114 * current/doc/CVS/Repository:
1118 * current/doc/CVS/Root:
1120 :ext:IwantToKeepAnon@cvs.ijbswa.sourceforge.net:/cvsroot/ijbswa
1123 ** Local variables for this standards file
1126 * Hopefully this will be in outline-mode soon.