1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
2 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
5 <meta name="generator" content="HTML Tidy, see www.w3.org">
9 <meta name="GENERATOR" content=
10 "Modular DocBook HTML Stylesheet Version 1.79">
11 <link rel="HOME" title="Privoxy Developer Manual" href="index.html">
12 <link rel="PREVIOUS" title="Documentation Guidelines" href=
14 <link rel="NEXT" title="Testing Guidelines" href="testing.html">
15 <link rel="STYLESHEET" type="text/css" href="../p_doc.css">
16 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
17 <style type="text/css">
19 background-color: #EEEEEE;
22 :link { color: #0000FF }
23 :visited { color: #840084 }
24 :active { color: #0000FF }
25 hr.c1 {text-align: left}
29 <div class="NAVHEADER">
30 <table summary="Header navigation table" width="100%" border="0"
31 cellpadding="0" cellspacing="0">
33 <th colspan="3" align="center">
34 Privoxy Developer Manual
38 <td width="10%" align="left" valign="bottom">
39 <a href="documentation.html" accesskey="P">Prev</a>
41 <td width="80%" align="center" valign="bottom">
43 <td width="10%" align="right" valign="bottom">
44 <a href="testing.html" accesskey="N">Next</a>
48 <hr width="100%" class="c1">
52 <a name="CODING">4. Coding Guidelines</a>
56 <a name="S1">4.1. Introduction</a>
59 This set of standards is designed to make our lives easier. It is
60 developed with the simple goal of helping us keep the "new and
61 improved <span class="APPLICATION">Privoxy</span>" consistent and
62 reliable. Thus making maintenance easier and increasing chances of
63 success of the project.
66 And that of course comes back to us as individuals. If we can
67 increase our development and product efficiencies then we can solve
68 more of the request for changes/improvements and in general feel
69 good about ourselves. ;->
74 <a name="S2">4.2. Using Comments</a>
78 <a name="S3">4.2.1. Comment, Comment, Comment</a>
81 <span class="emphasis"><i class=
82 "EMPHASIS">Explanation:</i></span>
85 Comment as much as possible without commenting the obvious. For
86 example do not comment "variable_a is equal to variable_b".
87 Instead explain why variable_a should be equal to the variable_b.
88 Just because a person can read code does not mean they will
89 understand why or what is being done. A reader may spend a lot
90 more time figuring out what is going on when a simple comment or
91 explanation would have prevented the extra research. Please help
92 your brother IJB'ers out!
95 The comments will also help justify the intent of the code. If
96 the comment describes something different than what the code is
97 doing then maybe a programming error is occurring.
100 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
102 <table border="0" bgcolor="#E0E0E0" width="100%">
105 <pre class="PROGRAMLISTING">
106 /* if page size greater than 1k ... */
107 if ( page_length() > 1024 )
109 ... "block" the page up ...
112 /* if page size is small, send it in blocks */
113 if ( page_length() > 1024 )
115 ... "block" the page up ...
118 This demonstrates 2 cases of "what not to do". The first is a
119 "syntax comment". The second is a comment that does not fit what
120 is actually being done.
128 <a name="S4">4.2.2. Use blocks for comments</a>
131 <span class="emphasis"><i class=
132 "EMPHASIS">Explanation:</i></span>
135 Comments can help or they can clutter. They help when they are
136 differentiated from the code they describe. One line comments do
137 not offer effective separation between the comment and the code.
138 Block identifiers do, by surrounding the code with a clear,
142 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
144 <table border="0" bgcolor="#E0E0E0" width="100%">
147 <pre class="PROGRAMLISTING">
148 /*********************************************************************
149 * This will stand out clearly in your code!
150 *********************************************************************/
151 if ( this_variable == that_variable )
153 do_something_very_important();
157 /* unfortunately, this may not */
158 if ( this_variable == that_variable )
160 do_something_very_important();
164 if ( this_variable == that_variable ) /* this may not either */
166 do_something_very_important();
173 <span class="emphasis"><i class="EMPHASIS">Exception:</i></span>
176 If you are trying to add a small logic comment and do not wish to
177 "disrupt" the flow of the code, feel free to use a 1 line comment
178 which is NOT on the same line as the code.
183 <a name="S5">4.2.3. Keep Comments on their own line</a>
186 <span class="emphasis"><i class=
187 "EMPHASIS">Explanation:</i></span>
190 It goes back to the question of readability. If the comment is on
191 the same line as the code it will be harder to read than the
192 comment that is on its own line.
195 There are three exceptions to this rule, which should be violated
196 freely and often: during the definition of variables, at the end
197 of closing braces, when used to comment parameters.
200 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
202 <table border="0" bgcolor="#E0E0E0" width="100%">
205 <pre class="PROGRAMLISTING">
206 /*********************************************************************
207 * This will stand out clearly in your code,
208 * But the second example won't.
209 *********************************************************************/
210 if ( this_variable == this_variable )
212 do_something_very_important();
215 if ( this_variable == this_variable ) /*can you see me?*/
217 do_something_very_important(); /*not easily*/
221 /*********************************************************************
222 * But, the encouraged exceptions:
223 *********************************************************************/
224 int urls_read = 0; /* # of urls read + rejected */
225 int urls_rejected = 0; /* # of urls rejected */
229 do_something_very_important();
233 short do_something_very_important(
234 short firstparam, /* represents something */
235 short nextparam /* represents something else */ )
239 } /* -END- do_something_very_important */
247 <a name="S6">4.2.4. Comment each logical step</a>
250 <span class="emphasis"><i class=
251 "EMPHASIS">Explanation:</i></span>
254 Logical steps should be commented to help others follow the
255 intent of the written code and comments will make the code more
259 If you have 25 lines of code without a comment, you should
260 probably go back into it to see where you forgot to put one.
263 Most "for", "while", "do", etc... loops _probably_ need a
264 comment. After all, these are usually major logic containers.
269 <a name="S7">4.2.5. Comment All Functions Thoroughly</a>
272 <span class="emphasis"><i class=
273 "EMPHASIS">Explanation:</i></span>
276 A reader of the code should be able to look at the comments just
277 prior to the beginning of a function and discern the reason for
278 its existence and the consequences of using it. The reader should
279 not have to read through the code to determine if a given
280 function is safe for a desired use. The proper information
281 thoroughly presented at the introduction of a function not only
282 saves time for subsequent maintenance or debugging, it more
283 importantly aids in code reuse by allowing a user to determine
284 the safety and applicability of any function for the problem at
285 hand. As a result of such benefits, all functions should contain
286 the information presented in the addendum section of this
292 <a name="S8">4.2.6. Comment at the end of braces if the content
293 is more than one screen length</a>
296 <span class="emphasis"><i class=
297 "EMPHASIS">Explanation:</i></span>
300 Each closing brace should be followed on the same line by a
301 comment that describes the origination of the brace if the
302 original brace is off of the screen, or otherwise far away from
303 the closing brace. This will simplify the debugging, maintenance,
304 and readability of the code.
307 As a suggestion , use the following flags to make the comment and
308 its brace more readable:
311 use following a closing brace: } /* -END- if() or while () or
315 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
317 <table border="0" bgcolor="#E0E0E0" width="100%">
320 <pre class="PROGRAMLISTING">
323 do_something_very_important();
324 ...some long list of commands...
325 } /* -END- if x is 1 */
331 do_something_very_important();
332 ...some long list of commands...
333 } /* -END- if ( 1 == X ) */
342 <a name="S9">4.3. Naming Conventions</a>
346 <a name="S10">4.3.1. Variable Names</a>
349 <span class="emphasis"><i class=
350 "EMPHASIS">Explanation:</i></span>
353 Use all lowercase, and separate words via an underscore ('_'). Do
354 not start an identifier with an underscore. (ANSI C reserves
355 these for use by the compiler and system headers.) Do not use
356 identifiers which are reserved in ANSI C++. (E.g. template,
357 class, true, false, ...). This is in case we ever decide to port
361 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
363 <table border="0" bgcolor="#E0E0E0" width="100%">
366 <pre class="PROGRAMLISTING">
367 int ms_iis5_hack = 0;
373 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
375 <table border="0" bgcolor="#E0E0E0" width="100%">
378 <pre class="PROGRAMLISTING">
379 int msiis5hack = 0; int msIis5Hack = 0;
387 <a name="S11">4.3.2. Function Names</a>
390 <span class="emphasis"><i class=
391 "EMPHASIS">Explanation:</i></span>
394 Use all lowercase, and separate words via an underscore ('_'). Do
395 not start an identifier with an underscore. (ANSI C reserves
396 these for use by the compiler and system headers.) Do not use
397 identifiers which are reserved in ANSI C++. (E.g. template,
398 class, true, false, ...). This is in case we ever decide to port
402 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
404 <table border="0" bgcolor="#E0E0E0" width="100%">
407 <pre class="PROGRAMLISTING">
408 int load_some_file( struct client_state *csp )
414 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
416 <table border="0" bgcolor="#E0E0E0" width="100%">
419 <pre class="PROGRAMLISTING">
420 int loadsomefile( struct client_state *csp )
421 int loadSomeFile( struct client_state *csp )
429 <a name="S12">4.3.3. Header file prototypes</a>
432 <span class="emphasis"><i class=
433 "EMPHASIS">Explanation:</i></span>
436 Use a descriptive parameter name in the function prototype in
437 header files. Use the same parameter name in the header file that
438 you use in the c file.
441 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
443 <table border="0" bgcolor="#E0E0E0" width="100%">
446 <pre class="PROGRAMLISTING">
447 (.h) extern int load_aclfile( struct client_state *csp );
448 (.c) int load_aclfile( struct client_state *csp )
454 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
456 <table border="0" bgcolor="#E0E0E0" width="100%">
459 <pre class="PROGRAMLISTING">
460 (.h) extern int load_aclfile( struct client_state * ); or
461 (.h) extern int load_aclfile();
462 (.c) int load_aclfile( struct client_state *csp )
470 <a name="S13">4.3.4. Enumerations, and #defines</a>
473 <span class="emphasis"><i class=
474 "EMPHASIS">Explanation:</i></span>
477 Use all capital letters, with underscores between words. Do not
478 start an identifier with an underscore. (ANSI C reserves these
479 for use by the compiler and system headers.)
482 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
484 <table border="0" bgcolor="#E0E0E0" width="100%">
487 <pre class="PROGRAMLISTING">
488 (enumeration) : enum Boolean { FALSE, TRUE };
489 (#define) : #define DEFAULT_SIZE 100;
495 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> We
496 have a standard naming scheme for #defines that toggle a feature
497 in the preprocessor: FEATURE_>, where > is a short
498 (preferably 1 or 2 word) description.
501 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
503 <table border="0" bgcolor="#E0E0E0" width="100%">
506 <pre class="PROGRAMLISTING">
507 #define FEATURE_FORCE 1
510 #define FORCE_PREFIX blah
511 #endif /* def FEATURE_FORCE */
519 <a name="S14">4.3.5. Constants</a>
522 <span class="emphasis"><i class=
523 "EMPHASIS">Explanation:</i></span>
526 Spell common words out entirely (do not remove vowels).
529 Use only widely-known domain acronyms and abbreviations.
530 Capitalize all letters of an acronym.
533 Use underscore (_) to separate adjacent acronyms and
534 abbreviations. Never terminate a name with an underscore.
537 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
539 <table border="0" bgcolor="#E0E0E0" width="100%">
542 <pre class="PROGRAMLISTING">
543 #define USE_IMAGE_LIST 1
549 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
551 <table border="0" bgcolor="#E0E0E0" width="100%">
554 <pre class="PROGRAMLISTING">
555 #define USE_IMG_LST 1 or
556 #define _USE_IMAGE_LIST 1 or
557 #define USE_IMAGE_LIST_ 1 or
558 #define use_image_list 1 or
559 #define UseImageList 1
568 <a name="S15">4.4. Using Space</a>
572 <a name="S16">4.4.1. Put braces on a line by themselves.</a>
575 <span class="emphasis"><i class=
576 "EMPHASIS">Explanation:</i></span>
579 The brace needs to be on a line all by itself, not at the end of
580 the statement. Curly braces should line up with the construct
581 that they're associated with. This practice makes it easier to
582 identify the opening and closing braces for a block.
585 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
587 <table border="0" bgcolor="#E0E0E0" width="100%">
590 <pre class="PROGRAMLISTING">
600 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
603 if ( this == that ) { ... }
609 if ( this == that ) { ... }
612 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> In
613 the special case that the if-statement is inside a loop, and it
614 is trivial, i.e. it tests for a condition that is obvious from
615 the purpose of the block, one-liners as above may optically
616 preserve the loop structure and make it easier to read.
619 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
620 developer-discretion.
623 <span class="emphasis"><i class="EMPHASIS">Example
624 exception:</i></span>
626 <table border="0" bgcolor="#E0E0E0" width="100%">
629 <pre class="PROGRAMLISTING">
630 while ( more lines are read )
632 /* Please document what is/is not a comment line here */
633 if ( it's a comment ) continue;
635 do_something( line );
644 <a name="S17">4.4.2. ALL control statements should have a
648 <span class="emphasis"><i class=
649 "EMPHASIS">Explanation:</i></span>
652 Using braces to make a block will make your code more readable
653 and less prone to error. All control statements should have a
657 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
659 <table border="0" bgcolor="#E0E0E0" width="100%">
662 <pre class="PROGRAMLISTING">
673 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
676 if ( this == that ) do_something(); do_something_else();
682 if ( this == that ) do_something();
685 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> The
686 first example in "Instead of" will execute in a manner other than
687 that which the developer desired (per indentation). Using code
688 braces would have prevented this "feature". The "explanation" and
689 "exception" from the point above also applies.
694 <a name="S18">4.4.3. Do not belabor/blow-up boolean
698 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
700 <table border="0" bgcolor="#E0E0E0" width="100%">
703 <pre class="PROGRAMLISTING">
704 structure->flag = ( condition );
710 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
713 if ( condition ) { structure->flag = 1; } else {
714 structure->flag = 0; }
717 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> The
718 former is readable and concise. The later is wordy and
719 inefficient. Please assume that any developer new to the project
720 has at least a "good" knowledge of C/C++. (Hope I do not offend
721 by that last comment ... 8-)
726 <a name="S19">4.4.4. Use white space freely because it is
730 <span class="emphasis"><i class=
731 "EMPHASIS">Explanation:</i></span>
734 Make it readable. The notable exception to using white space
735 freely is listed in the next guideline.
738 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
740 <table border="0" bgcolor="#E0E0E0" width="100%">
743 <pre class="PROGRAMLISTING">
746 int another_value = 0;
747 int this_variable = 0;
749 if ( this_variable == this_variable )
751 first_value = old_value + ( ( some_value - another_value ) - whatever )
759 <a name="S20">4.4.5. Don't use white space around structure
763 <span class="emphasis"><i class=
764 "EMPHASIS">Explanation:</i></span>
767 - structure pointer operator ( "->" ) - member operator ( "."
768 ) - functions and parentheses
771 It is a general coding practice to put pointers, references, and
772 function parentheses next to names. With spaces, the connection
773 between the object and variable/function name is not as clear.
776 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
778 <table border="0" bgcolor="#E0E0E0" width="100%">
781 <pre class="PROGRAMLISTING">
782 a_struct->a_member;
790 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
791 a_struct -> a_member; a_struct . a_member; function_name ();
796 <a name="S21">4.4.6. Make the last brace of a function stand
800 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
802 <table border="0" bgcolor="#E0E0E0" width="100%">
805 <pre class="PROGRAMLISTING">
811 } /* -END- function1 */
816 } /* -END- function2 */
822 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
825 int function1( ... ) { ...code... return( ret_code ); } int
829 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> Use 1
830 blank line before the closing brace and 2 lines afterward. This
831 makes the end of function standout to the most casual viewer.
832 Although function comments help separate functions, this is still
833 a good coding practice. In fact, I follow these rules when using
834 blocks in "for", "while", "do" loops, and long if {} statements
835 too. After all whitespace is free!
838 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
839 developer-discretion on the number of blank lines. Enforced is
840 the end of function comments.
845 <a name="S22">4.4.7. Use 3 character indentions</a>
848 <span class="emphasis"><i class=
849 "EMPHASIS">Explanation:</i></span>
852 If some use 8 character TABs and some use 3 character TABs, the
853 code can look *very* ragged. So use 3 character indentions only.
854 If you like to use TABs, pass your code through a filter such as
855 "expand -t3" before checking in your code.
858 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
860 <table border="0" bgcolor="#E0E0E0" width="100%">
863 <pre class="PROGRAMLISTING">
864 static const char * const url_code_map[256] =
874 return( ALWAYS_TRUE );
878 return( HOW_DID_YOU_GET_HERE );
881 return( NEVER_GETS_HERE );
892 <a name="S23">4.5. Initializing</a>
896 <a name="S24">4.5.1. Initialize all variables</a>
899 <span class="emphasis"><i class=
900 "EMPHASIS">Explanation:</i></span>
903 Do not assume that the variables declared will not be used until
904 after they have been assigned a value somewhere else in the code.
905 Remove the chance of accidentally using an unassigned variable.
908 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
910 <table border="0" bgcolor="#E0E0E0" width="100%">
913 <pre class="PROGRAMLISTING">
922 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> It is
923 much easier to debug a SIGSEGV if the message says you are trying
924 to access memory address 00000000 and not 129FA012; or
925 array_ptr[20] causes a SIGSEV vs. array_ptr[0].
928 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
929 developer-discretion if and only if the variable is assigned a
930 value "shortly after" declaration.
936 <a name="S25">4.6. Functions</a>
940 <a name="S26">4.6.1. Name functions that return a boolean as a
944 <span class="emphasis"><i class=
945 "EMPHASIS">Explanation:</i></span>
948 Value should be phrased as a question that would logically be
949 answered as a true or false statement
952 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
954 <table border="0" bgcolor="#E0E0E0" width="100%">
957 <pre class="PROGRAMLISTING">
958 should_we_block_this();
968 <a name="S27">4.6.2. Always specify a return type for a
972 <span class="emphasis"><i class=
973 "EMPHASIS">Explanation:</i></span>
976 The default return for a function is an int. To avoid ambiguity,
977 create a return for a function when the return has a purpose, and
978 create a void return type if the function does not need to return
984 <a name="S28">4.6.3. Minimize function calls when iterating by
988 <span class="emphasis"><i class=
989 "EMPHASIS">Explanation:</i></span>
992 It is easy to write the following code, and a clear argument can
993 be made that the code is easy to understand:
996 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
998 <table border="0" bgcolor="#E0E0E0" width="100%">
1001 <pre class="PROGRAMLISTING">
1002 for ( size_t cnt = 0; cnt < block_list_length(); cnt++ )
1011 <span class="emphasis"><i class="EMPHASIS">Note:</i></span>
1012 Unfortunately, this makes a function call for each and every
1013 iteration. This increases the overhead in the program, because
1014 the compiler has to look up the function each time, call it, and
1015 return a value. Depending on what occurs in the
1016 block_list_length() call, it might even be creating and
1017 destroying structures with each iteration, even though in each
1018 case it is comparing "cnt" to the same value, over and over.
1019 Remember too - even a call to block_list_length() is a function
1020 call, with the same overhead.
1023 Instead of using a function call during the iterations, assign
1024 the value to a variable, and evaluate using the variable.
1027 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1029 <table border="0" bgcolor="#E0E0E0" width="100%">
1032 <pre class="PROGRAMLISTING">
1033 size_t len = block_list_length();
1035 for ( size_t cnt = 0; cnt < len; cnt++ )
1044 <span class="emphasis"><i class="EMPHASIS">Exceptions:</i></span>
1045 if the value of block_list_length() *may* change or could
1046 *potentially* change, then you must code the function call in the
1052 <a name="S29">4.6.4. Pass and Return by Const Reference</a>
1055 <span class="emphasis"><i class=
1056 "EMPHASIS">Explanation:</i></span>
1059 This allows a developer to define a const pointer and call your
1060 function. If your function does not have the const keyword, we
1061 may not be able to use your function. Consider strcmp, if it were
1062 defined as: extern int strcmp( char *s1, char *s2 );
1065 I could then not use it to compare argv's in main: int main( int
1066 argc, const char *argv[] ) { strcmp( argv[0], "privoxy" ); }
1069 Both these pointers are *const*! If the c runtime library
1070 maintainers do it, we should too.
1075 <a name="S30">4.6.5. Pass and Return by Value</a>
1078 <span class="emphasis"><i class=
1079 "EMPHASIS">Explanation:</i></span>
1082 Most structures cannot fit onto a normal stack entry (i.e. they
1083 are not 4 bytes or less). Aka, a function declaration like: int
1084 load_aclfile( struct client_state csp )
1087 would not work. So, to be consistent, we should declare all
1088 prototypes with "pass by value": int load_aclfile( struct
1094 <a name="S31">4.6.6. Names of include files</a>
1097 <span class="emphasis"><i class=
1098 "EMPHASIS">Explanation:</i></span>
1101 Your include statements should contain the file name without a
1102 path. The path should be listed in the Makefile, using -I as
1103 processor directive to search the indicated paths. An exception
1104 to this would be for some proprietary software that utilizes a
1105 partial path to distinguish their header files from system or
1109 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1111 <table border="0" bgcolor="#E0E0E0" width="100%">
1114 <pre class="PROGRAMLISTING">
1115 #include <iostream.h> /* This is not a local include */
1116 #include "config.h" /* This IS a local include */
1122 <span class="emphasis"><i class="EMPHASIS">Exception:</i></span>
1124 <table border="0" bgcolor="#E0E0E0" width="100%">
1127 <pre class="PROGRAMLISTING">
1128 /* This is not a local include, but requires a path element. */
1129 #include <sys/fileName.h>
1136 <span class="emphasis"><i class="EMPHASIS">Note:</i></span>
1137 Please! do not add "-I." to the Makefile without a _very_ good
1138 reason. This duplicates the #include "file.h" behavior.
1143 <a name="S32">4.6.7. Provide multiple inclusion protection</a>
1146 <span class="emphasis"><i class=
1147 "EMPHASIS">Explanation:</i></span>
1150 Prevents compiler and linker errors resulting from redefinition
1154 Wrap each header file with the following syntax to prevent
1155 multiple inclusions of the file. Of course, replace PROJECT_H
1156 with your file name, with "." Changed to "_", and make it
1160 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1162 <table border="0" bgcolor="#E0E0E0" width="100%">
1165 <pre class="PROGRAMLISTING">
1166 #ifndef PROJECT_H_INCLUDED
1167 #define PROJECT_H_INCLUDED
1169 #endif /* ndef PROJECT_H_INCLUDED */
1177 <a name="S33">4.6.8. Use `extern "C"` when appropriate</a>
1180 <span class="emphasis"><i class=
1181 "EMPHASIS">Explanation:</i></span>
1184 If our headers are included from C++, they must declare our
1185 functions as `extern "C"`. This has no cost in C, but increases
1186 the potential re-usability of our code.
1189 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1191 <table border="0" bgcolor="#E0E0E0" width="100%">
1194 <pre class="PROGRAMLISTING">
1198 #endif /* def __cplusplus */
1200 ... function definitions here ...
1204 #endif /* def __cplusplus */
1212 <a name="S34">4.6.9. Where Possible, Use Forward Struct
1213 Declaration Instead of Includes</a>
1216 <span class="emphasis"><i class=
1217 "EMPHASIS">Explanation:</i></span>
1220 Useful in headers that include pointers to other struct's.
1221 Modifications to excess header files may cause needless compiles.
1224 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1226 <table border="0" bgcolor="#E0E0E0" width="100%">
1229 <pre class="PROGRAMLISTING">
1230 /*********************************************************************
1231 * We're avoiding an include statement here!
1232 *********************************************************************/
1234 extern file_list *xyz;
1240 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If
1241 you declare "file_list xyz;" (without the pointer), then
1242 including the proper header file is necessary. If you only want
1243 to prototype a pointer, however, the header file is unnecessary.
1246 <span class="emphasis"><i class="EMPHASIS">Status:</i></span> Use
1253 <a name="S35">4.7. General Coding Practices</a>
1257 <a name="S36">4.7.1. Turn on warnings</a>
1260 <span class="emphasis"><i class="EMPHASIS">Explanation</i></span>
1263 Compiler warnings are meant to help you find bugs. You should
1264 turn on as many as possible. With GCC, the switch is "-Wall". Try
1265 and fix as many warnings as possible.
1270 <a name="S37">4.7.2. Provide a default case for all switch
1274 <span class="emphasis"><i class=
1275 "EMPHASIS">Explanation:</i></span>
1278 What you think is guaranteed is never really guaranteed. The
1279 value that you don't think you need to check is the one that
1280 someday will be passed. So, to protect yourself from the unknown,
1281 always have a default step in a switch statement.
1284 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1286 <table border="0" bgcolor="#E0E0E0" width="100%">
1289 <pre class="PROGRAMLISTING">
1290 switch( hash_string( cmd ) )
1292 case hash_actions_file :
1302 ... anomaly code goes here ...
1303 continue; / break; / exit( 1 ); / etc ...
1305 } /* end switch( hash_string( cmd ) ) */
1311 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If
1312 you already have a default condition, you are obviously exempt
1313 from this point. Of note, most of the WIN32 code calls
1314 `DefWindowProc' after the switch statement. This API call
1315 *should* be included in a default statement.
1318 <span class="emphasis"><i class="EMPHASIS">Another
1319 Note:</i></span> This is not so much a readability issue as a
1320 robust programming issue. The "anomaly code goes here" may be no
1321 more than a print to the STDERR stream (as in load_config). Or it
1322 may really be an abort condition.
1325 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
1326 Programmer discretion is advised.
1331 <a name="S38">4.7.3. Try to avoid falling through cases in a
1332 switch statement.</a>
1335 <span class="emphasis"><i class=
1336 "EMPHASIS">Explanation:</i></span>
1339 In general, you will want to have a 'break' statement within each
1340 'case' of a switch statement. This allows for the code to be more
1341 readable and understandable, and furthermore can prevent unwanted
1342 surprises if someone else later gets creative and moves the code
1346 The language allows you to plan the fall through from one case
1347 statement to another simply by omitting the break statement
1348 within the case statement. This feature does have benefits, but
1349 should only be used in rare cases. In general, use a break
1350 statement for each case statement.
1353 If you choose to allow fall through, you should comment both the
1354 fact of the fall through and reason why you felt it was
1360 <a name="S39">4.7.4. Use 'long' or 'short' Instead of 'int'</a>
1363 <span class="emphasis"><i class=
1364 "EMPHASIS">Explanation:</i></span>
1367 On 32-bit platforms, int usually has the range of long. On 16-bit
1368 platforms, int has the range of short.
1371 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
1372 open-to-debate. In the case of most FSF projects (including
1373 X/GNU-Emacs), there are typedefs to int4, int8, int16, (or
1374 equivalence ... I forget the exact typedefs now). Should we add
1375 these to IJB now that we have a "configure" script?
1380 <a name="S40">4.7.5. Don't mix size_t and other types</a>
1383 <span class="emphasis"><i class=
1384 "EMPHASIS">Explanation:</i></span>
1387 The type of size_t varies across platforms. Do not make
1388 assumptions about whether it is signed or unsigned, or about how
1389 long it is. Do not compare a size_t against another variable of a
1390 different type (or even against a constant) without casting one
1396 <a name="S41">4.7.6. Declare each variable and struct on its own
1400 <span class="emphasis"><i class=
1401 "EMPHASIS">Explanation:</i></span>
1404 It can be tempting to declare a series of variables all on one
1408 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1410 <table border="0" bgcolor="#E0E0E0" width="100%">
1413 <pre class="PROGRAMLISTING">
1422 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
1428 <span class="emphasis"><i class=
1429 "EMPHASIS">Explanation:</i></span> - there is more room for
1430 comments on the individual variables - easier to add new
1431 variables without messing up the original ones - when searching
1432 on a variable to find its type, there is less clutter to
1433 "visually" eliminate
1436 <span class="emphasis"><i class="EMPHASIS">Exceptions:</i></span>
1437 when you want to declare a bunch of loop variables or other
1438 trivial variables; feel free to declare them on one line. You
1439 should, although, provide a good comment on their functions.
1442 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
1443 developer-discretion.
1448 <a name="S42">4.7.7. Use malloc/zalloc sparingly</a>
1451 <span class="emphasis"><i class=
1452 "EMPHASIS">Explanation:</i></span>
1455 Create a local struct (on the stack) if the variable will live
1456 and die within the context of one function call.
1459 Only "malloc" a struct (on the heap) if the variable's life will
1460 extend beyond the context of one function call.
1463 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1465 <table border="0" bgcolor="#E0E0E0" width="100%">
1468 <pre class="PROGRAMLISTING">
1469 If a function creates a struct and stores a pointer to it in a
1470 list, then it should definitely be allocated via `malloc'.
1478 <a name="S43">4.7.8. The Programmer Who Uses 'malloc' is
1479 Responsible for Ensuring 'free'</a>
1482 <span class="emphasis"><i class=
1483 "EMPHASIS">Explanation:</i></span>
1486 If you have to "malloc" an instance, you are responsible for
1487 insuring that the instance is `free'd, even if the deallocation
1488 event falls within some other programmer's code. You are also
1489 responsible for ensuring that deletion is timely (i.e. not too
1490 soon, not too late). This is known as "low-coupling" and is a
1491 "good thing (tm)". You may need to offer a free/unload/destructor
1492 type function to accommodate this.
1495 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1497 <table border="0" bgcolor="#E0E0E0" width="100%">
1500 <pre class="PROGRAMLISTING">
1501 int load_re_filterfile( struct client_state *csp ) { ... }
1502 static void unload_re_filterfile( void *f ) { ... }
1508 <span class="emphasis"><i class="EMPHASIS">Exceptions:</i></span>
1511 The developer cannot be expected to provide `free'ing functions
1512 for C run-time library functions ... such as `strdup'.
1515 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
1516 developer-discretion. The "main" use of this standard is for
1517 allocating and freeing data structures (complex or nested).
1522 <a name="S44">4.7.9. Add loaders to the `file_list' structure and
1526 <span class="emphasis"><i class=
1527 "EMPHASIS">Explanation:</i></span>
1530 I have ordered all of the "blocker" file code to be in alpha
1531 order. It is easier to add/read new blockers when you expect a
1535 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> It
1536 may appear that the alpha order is broken in places by POPUP
1537 tests coming before PCRS tests. But since POPUPs can also be
1538 referred to as KILLPOPUPs, it is clear that it should come first.
1543 <a name="S45">4.7.10. "Uncertain" new code and/or changes to
1544 existing code, use FIXME or XXX</a>
1547 <span class="emphasis"><i class=
1548 "EMPHASIS">Explanation:</i></span>
1551 If you have enough confidence in new code or confidence in your
1552 changes, but are not *quite* sure of the repercussions, add this:
1555 /* FIXME: this code has a logic error on platform XYZ, *
1556 attempting to fix */ #ifdef PLATFORM ...changed code here...
1563 /* FIXME: I think the original author really meant this... */
1564 ...changed code here...
1570 /* FIXME: new code that *may* break something else... */ ...new
1574 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If
1575 you make it clear that this may or may not be a "good thing
1576 (tm)", it will be easier to identify and include in the project
1577 (or conversely exclude from the project).
1583 <a name="S46">4.8. Addendum: Template for files and function
1587 <span class="emphasis"><i class="EMPHASIS">Example for file
1588 comments:</i></span>
1590 <table border="0" bgcolor="#E0E0E0" width="100%">
1593 <pre class="PROGRAMLISTING">
1594 const char FILENAME_rcs[] = "$Id: coding.html,v 1.54 2010/11/13 12:50:18 fabiankeil Exp $";
1595 /*********************************************************************
1597 * File : $Source: /cvsroot/ijbswa/current/doc/webserver/developer-manual/coding.html,v $
1599 * Purpose : (Fill me in with a good description!)
1601 * Copyright : Written by and Copyright (C) 2001-2009
1602 * the Privoxy team. http://www.privoxy.org/
1604 * This program is free software; you can redistribute it
1605 * and/or modify it under the terms of the GNU General
1606 * Public License as published by the Free Software
1607 * Foundation; either version 2 of the License, or (at
1608 * your option) any later version.
1610 * This program is distributed in the hope that it will
1611 * be useful, but WITHOUT ANY WARRANTY; without even the
1612 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1613 * PARTICULAR PURPOSE. See the GNU General Public
1614 * License for more details.
1616 * The GNU General Public License should be included with
1617 * this file. If not, you can view it at
1618 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
1619 * or write to the Free Software Foundation, Inc.,
1620 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
1623 *********************************************************************/
1628 ...necessary include files for us to do our work...
1630 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
1636 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> This
1637 declares the rcs variables that should be added to the
1638 "show-proxy-args" page. If this is a brand new creation by you, you
1639 are free to change the "Copyright" section to represent the rights
1640 you wish to maintain.
1643 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> The
1644 formfeed character that is present right after the comment flower
1645 box is handy for (X|GNU)Emacs users to skip the verbiage and get to
1646 the heart of the code (via `forward-page' and `backward-page').
1647 Please include it if you can.
1650 <span class="emphasis"><i class="EMPHASIS">Example for file header
1651 comments:</i></span>
1653 <table border="0" bgcolor="#E0E0E0" width="100%">
1656 <pre class="PROGRAMLISTING">
1659 #define FILENAME_H_VERSION "$Id: coding.html,v 1.54 2010/11/13 12:50:18 fabiankeil Exp $"
1660 /*********************************************************************
1662 * File : $Source: /cvsroot/ijbswa/current/doc/webserver/developer-manual/coding.html,v $
1664 * Purpose : (Fill me in with a good description!)
1666 * Copyright : Written by and Copyright (C) 2001-2009
1667 * the Privoxy team. http://www.privoxy.org/
1669 * This program is free software; you can redistribute it
1670 * and/or modify it under the terms of the GNU General
1671 * Public License as published by the Free Software
1672 * Foundation; either version 2 of the License, or (at
1673 * your option) any later version.
1675 * This program is distributed in the hope that it will
1676 * be useful, but WITHOUT ANY WARRANTY; without even the
1677 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1678 * PARTICULAR PURPOSE. See the GNU General Public
1679 * License for more details.
1681 * The GNU General Public License should be included with
1682 * this file. If not, you can view it at
1683 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
1684 * or write to the Free Software Foundation, Inc.,
1685 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
1688 *********************************************************************/
1691 #include "project.h"
1697 ... function headers here ...
1700 /* Revision control strings from this header and associated .c file */
1701 extern const char FILENAME_rcs[];
1702 extern const char FILENAME_h_rcs[];
1709 #endif /* ndef _FILENAME_H */
1721 <span class="emphasis"><i class="EMPHASIS">Example for function
1722 comments:</i></span>
1724 <table border="0" bgcolor="#E0E0E0" width="100%">
1727 <pre class="PROGRAMLISTING">
1728 /*********************************************************************
1730 * Function : FUNCTION_NAME
1732 * Description : (Fill me in with a good description!)
1735 * 1 : param1 = pointer to an important thing
1736 * 2 : x = pointer to something else
1738 * Returns : 0 => Ok, everything else is an error.
1740 *********************************************************************/
1741 int FUNCTION_NAME( void *param1, const char *x )
1752 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If we
1753 all follow this practice, we should be able to parse our code to
1754 create a "self-documenting" web page.
1758 <div class="NAVFOOTER">
1759 <hr width="100%" class="c1">
1760 <table summary="Footer navigation table" width="100%" border="0"
1761 cellpadding="0" cellspacing="0">
1763 <td width="33%" align="left" valign="top">
1764 <a href="documentation.html" accesskey="P">Prev</a>
1766 <td width="34%" align="center" valign="top">
1767 <a href="index.html" accesskey="H">Home</a>
1769 <td width="33%" align="right" valign="top">
1770 <a href="testing.html" accesskey="N">Next</a>
1774 <td width="33%" align="left" valign="top">
1775 Documentation Guidelines
1777 <td width="34%" align="center" valign="top">
1780 <td width="33%" align="right" valign="top">