1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
6 <title>Coding Guidelines</title>
7 <meta name="GENERATOR" content=
8 "Modular DocBook HTML Stylesheet Version 1.79">
9 <link rel="HOME" title="Privoxy Developer Manual" href="index.html">
10 <link rel="PREVIOUS" title="Documentation Guidelines" href=
12 <link rel="NEXT" title="Testing Guidelines" href="testing.html">
13 <link rel="STYLESHEET" type="text/css" href="../p_doc.css">
14 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
15 <style type="text/css">
17 background-color: #EEEEEE;
20 :link { color: #0000FF }
21 :visited { color: #840084 }
22 :active { color: #0000FF }
23 table.c3 {background-color: #E0E0E0}
24 span.c2 {font-style: italic}
25 hr.c1 {text-align: left}
30 <div class="NAVHEADER">
31 <table summary="Header navigation table" width="100%" border="0"
32 cellpadding="0" cellspacing="0">
34 <th colspan="3" align="center">Privoxy Developer Manual</th>
38 <td width="10%" align="left" valign="bottom"><a href=
39 "documentation.html" accesskey="P">Prev</a></td>
41 <td width="80%" align="center" valign="bottom"></td>
43 <td width="10%" align="right" valign="bottom"><a href="testing.html"
44 accesskey="N">Next</a></td>
47 <hr class="c1" width="100%">
51 <h1 class="SECT1"><a name="CODING" id="CODING">4. Coding
55 <h2 class="SECT2"><a name="S1" id="S1">4.1. Introduction</a></h2>
57 <p>This set of standards is designed to make our lives easier. It is
58 developed with the simple goal of helping us keep the "new and improved
59 <span class="APPLICATION">Privoxy</span>" consistent and reliable. Thus
60 making maintenance easier and increasing chances of success of the
63 <p>And that of course comes back to us as individuals. If we can
64 increase our development and product efficiencies then we can solve
65 more of the request for changes/improvements and in general feel good
66 about ourselves. ;-></p>
70 <h2 class="SECT2"><a name="S2" id="S2">4.2. Using Comments</a></h2>
73 <h3 class="SECT3"><a name="S3" id="S3">4.2.1. Comment, Comment,
76 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
78 <p>Comment as much as possible without commenting the obvious. For
79 example do not comment "variable_a is equal to variable_b". Instead
80 explain why variable_a should be equal to the variable_b. Just
81 because a person can read code does not mean they will understand why
82 or what is being done. A reader may spend a lot more time figuring
83 out what is going on when a simple comment or explanation would have
84 prevented the extra research. Please help your brother IJB'ers
87 <p>The comments will also help justify the intent of the code. If the
88 comment describes something different than what the code is doing
89 then maybe a programming error is occurring.</p>
91 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
93 <table class="c3" border="0" width="100%">
96 <pre class="PROGRAMLISTING">
97 /* if page size greater than 1k ... */
98 if ( page_length() > 1024 )
100 ... "block" the page up ...
103 /* if page size is small, send it in blocks */
104 if ( page_length() > 1024 )
106 ... "block" the page up ...
109 This demonstrates 2 cases of "what not to do". The first is a
110 "syntax comment". The second is a comment that does not fit what
111 is actually being done.
119 <h3 class="SECT3"><a name="S4" id="S4">4.2.2. Use blocks for
122 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
124 <p>Comments can help or they can clutter. They help when they are
125 differentiated from the code they describe. One line comments do not
126 offer effective separation between the comment and the code. Block
127 identifiers do, by surrounding the code with a clear, definable
130 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
132 <table class="c3" border="0" width="100%">
135 <pre class="PROGRAMLISTING">
136 /*********************************************************************
137 * This will stand out clearly in your code!
138 *********************************************************************/
139 if ( this_variable == that_variable )
141 do_something_very_important();
145 /* unfortunately, this may not */
146 if ( this_variable == that_variable )
148 do_something_very_important();
152 if ( this_variable == that_variable ) /* this may not either */
154 do_something_very_important();
161 <p><span class="emphasis EMPHASIS c2">Exception:</span></p>
163 <p>If you are trying to add a small logic comment and do not wish to
164 "disrupt" the flow of the code, feel free to use a 1 line comment
165 which is NOT on the same line as the code.</p>
169 <h3 class="SECT3"><a name="S5" id="S5">4.2.3. Keep Comments on their
172 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
174 <p>It goes back to the question of readability. If the comment is on
175 the same line as the code it will be harder to read than the comment
176 that is on its own line.</p>
178 <p>There are three exceptions to this rule, which should be violated
179 freely and often: during the definition of variables, at the end of
180 closing braces, when used to comment parameters.</p>
182 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
184 <table class="c3" border="0" width="100%">
187 <pre class="PROGRAMLISTING">
188 /*********************************************************************
189 * This will stand out clearly in your code,
190 * But the second example won't.
191 *********************************************************************/
192 if ( this_variable == this_variable )
194 do_something_very_important();
197 if ( this_variable == this_variable ) /*can you see me?*/
199 do_something_very_important(); /*not easily*/
203 /*********************************************************************
204 * But, the encouraged exceptions:
205 *********************************************************************/
206 int urls_read = 0; /* # of urls read + rejected */
207 int urls_rejected = 0; /* # of urls rejected */
211 do_something_very_important();
215 short do_something_very_important(
216 short firstparam, /* represents something */
217 short nextparam /* represents something else */ )
221 } /* -END- do_something_very_important */
229 <h3 class="SECT3"><a name="S6" id="S6">4.2.4. Comment each logical
232 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
234 <p>Logical steps should be commented to help others follow the intent
235 of the written code and comments will make the code more
238 <p>If you have 25 lines of code without a comment, you should
239 probably go back into it to see where you forgot to put one.</p>
241 <p>Most "for", "while", "do", etc... loops _probably_ need a comment.
242 After all, these are usually major logic containers.</p>
246 <h3 class="SECT3"><a name="S7" id="S7">4.2.5. Comment All Functions
249 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
251 <p>A reader of the code should be able to look at the comments just
252 prior to the beginning of a function and discern the reason for its
253 existence and the consequences of using it. The reader should not
254 have to read through the code to determine if a given function is
255 safe for a desired use. The proper information thoroughly presented
256 at the introduction of a function not only saves time for subsequent
257 maintenance or debugging, it more importantly aids in code reuse by
258 allowing a user to determine the safety and applicability of any
259 function for the problem at hand. As a result of such benefits, all
260 functions should contain the information presented in the addendum
261 section of this document.</p>
265 <h3 class="SECT3"><a name="S8" id="S8">4.2.6. Comment at the end of
266 braces if the content is more than one screen length</a></h3>
268 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
270 <p>Each closing brace should be followed on the same line by a
271 comment that describes the origination of the brace if the original
272 brace is off of the screen, or otherwise far away from the closing
273 brace. This will simplify the debugging, maintenance, and readability
276 <p>As a suggestion , use the following flags to make the comment and
277 its brace more readable:</p>
279 <p>use following a closing brace: } /* -END- if() or while () or
282 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
284 <table class="c3" border="0" width="100%">
287 <pre class="PROGRAMLISTING">
290 do_something_very_important();
291 ...some long list of commands...
292 } /* -END- if x is 1 */
298 do_something_very_important();
299 ...some long list of commands...
300 } /* -END- if ( 1 == X ) */
309 <h2 class="SECT2"><a name="S9" id="S9">4.3. Naming Conventions</a></h2>
312 <h3 class="SECT3"><a name="S10" id="S10">4.3.1. Variable
315 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
317 <p>Use all lowercase, and separate words via an underscore ('_'). Do
318 not start an identifier with an underscore. (ANSI C reserves these
319 for use by the compiler and system headers.) Do not use identifiers
320 which are reserved in ANSI C++. (E.g. template, class, true, false,
321 ...). This is in case we ever decide to port Privoxy to C++.</p>
323 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
325 <table class="c3" border="0" width="100%">
328 <pre class="PROGRAMLISTING">
329 int ms_iis5_hack = 0;
335 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
337 <table class="c3" border="0" width="100%">
340 <pre class="PROGRAMLISTING">
341 int msiis5hack = 0; int msIis5Hack = 0;
349 <h3 class="SECT3"><a name="S11" id="S11">4.3.2. Function
352 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
354 <p>Use all lowercase, and separate words via an underscore ('_'). Do
355 not start an identifier with an underscore. (ANSI C reserves these
356 for use by the compiler and system headers.) Do not use identifiers
357 which are reserved in ANSI C++. (E.g. template, class, true, false,
358 ...). This is in case we ever decide to port Privoxy to C++.</p>
360 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
362 <table class="c3" border="0" width="100%">
365 <pre class="PROGRAMLISTING">
366 int load_some_file( struct client_state *csp )
372 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
374 <table class="c3" border="0" width="100%">
377 <pre class="PROGRAMLISTING">
378 int loadsomefile( struct client_state *csp )
379 int loadSomeFile( struct client_state *csp )
387 <h3 class="SECT3"><a name="S12" id="S12">4.3.3. Header file
390 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
392 <p>Use a descriptive parameter name in the function prototype in
393 header files. Use the same parameter name in the header file that you
394 use in the c file.</p>
396 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
398 <table class="c3" border="0" width="100%">
401 <pre class="PROGRAMLISTING">
402 (.h) extern int load_aclfile( struct client_state *csp );
403 (.c) int load_aclfile( struct client_state *csp )
409 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
411 <table class="c3" border="0" width="100%">
414 <pre class="PROGRAMLISTING">
415 (.h) extern int load_aclfile( struct client_state * ); or
416 (.h) extern int load_aclfile();
417 (.c) int load_aclfile( struct client_state *csp )
425 <h3 class="SECT3"><a name="S13" id="S13">4.3.4. Enumerations, and
428 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
430 <p>Use all capital letters, with underscores between words. Do not
431 start an identifier with an underscore. (ANSI C reserves these for
432 use by the compiler and system headers.)</p>
434 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
436 <table class="c3" border="0" width="100%">
439 <pre class="PROGRAMLISTING">
440 (enumeration) : enum Boolean { FALSE, TRUE };
441 (#define) : #define DEFAULT_SIZE 100;
447 <p><span class="emphasis EMPHASIS c2">Note:</span> We have a standard
448 naming scheme for #defines that toggle a feature in the preprocessor:
449 FEATURE_>, where > is a short (preferably 1 or 2 word)
452 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
454 <table class="c3" border="0" width="100%">
457 <pre class="PROGRAMLISTING">
458 #define FEATURE_FORCE 1
461 #define FORCE_PREFIX blah
462 #endif /* def FEATURE_FORCE */
470 <h3 class="SECT3"><a name="S14" id="S14">4.3.5. Constants</a></h3>
472 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
474 <p>Spell common words out entirely (do not remove vowels).</p>
476 <p>Use only widely-known domain acronyms and abbreviations.
477 Capitalize all letters of an acronym.</p>
479 <p>Use underscore (_) to separate adjacent acronyms and
480 abbreviations. Never terminate a name with an underscore.</p>
482 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
484 <table class="c3" border="0" width="100%">
487 <pre class="PROGRAMLISTING">
488 #define USE_IMAGE_LIST 1
494 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
496 <table class="c3" border="0" width="100%">
499 <pre class="PROGRAMLISTING">
500 #define USE_IMG_LST 1 or
501 #define _USE_IMAGE_LIST 1 or
502 #define USE_IMAGE_LIST_ 1 or
503 #define use_image_list 1 or
504 #define UseImageList 1
513 <h2 class="SECT2"><a name="S15" id="S15">4.4. Using Space</a></h2>
516 <h3 class="SECT3"><a name="S16" id="S16">4.4.1. Put braces on a line
517 by themselves.</a></h3>
519 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
521 <p>The brace needs to be on a line all by itself, not at the end of
522 the statement. Curly braces should line up with the construct that
523 they're associated with. This practice makes it easier to identify
524 the opening and closing braces for a block.</p>
526 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
528 <table class="c3" border="0" width="100%">
531 <pre class="PROGRAMLISTING">
541 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
543 <p>if ( this == that ) { ... }</p>
547 <p>if ( this == that ) { ... }</p>
549 <p><span class="emphasis EMPHASIS c2">Note:</span> In the special
550 case that the if-statement is inside a loop, and it is trivial, i.e.
551 it tests for a condition that is obvious from the purpose of the
552 block, one-liners as above may optically preserve the loop structure
553 and make it easier to read.</p>
555 <p><span class="emphasis EMPHASIS c2">Status:</span>
556 developer-discretion.</p>
558 <p><span class="emphasis EMPHASIS c2">Example exception:</span></p>
560 <table class="c3" border="0" width="100%">
563 <pre class="PROGRAMLISTING">
564 while ( more lines are read )
566 /* Please document what is/is not a comment line here */
567 if ( it's a comment ) continue;
569 do_something( line );
578 <h3 class="SECT3"><a name="S17" id="S17">4.4.2. ALL control
579 statements should have a block</a></h3>
581 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
583 <p>Using braces to make a block will make your code more readable and
584 less prone to error. All control statements should have a block
587 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
589 <table class="c3" border="0" width="100%">
592 <pre class="PROGRAMLISTING">
603 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
605 <p>if ( this == that ) do_something(); do_something_else();</p>
609 <p>if ( this == that ) do_something();</p>
611 <p><span class="emphasis EMPHASIS c2">Note:</span> The first example
612 in "Instead of" will execute in a manner other than that which the
613 developer desired (per indentation). Using code braces would have
614 prevented this "feature". The "explanation" and "exception" from the
615 point above also applies.</p>
619 <h3 class="SECT3"><a name="S18" id="S18">4.4.3. Do not
620 belabor/blow-up boolean expressions</a></h3>
622 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
624 <table class="c3" border="0" width="100%">
627 <pre class="PROGRAMLISTING">
628 structure->flag = ( condition );
634 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
636 <p>if ( condition ) { structure->flag = 1; } else {
637 structure->flag = 0; }</p>
639 <p><span class="emphasis EMPHASIS c2">Note:</span> The former is
640 readable and concise. The later is wordy and inefficient. Please
641 assume that any developer new to the project has at least a "good"
642 knowledge of C/C++. (Hope I do not offend by that last comment ...
647 <h3 class="SECT3"><a name="S19" id="S19">4.4.4. Use white space
648 freely because it is free</a></h3>
650 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
652 <p>Make it readable. The notable exception to using white space
653 freely is listed in the next guideline.</p>
655 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
657 <table class="c3" border="0" width="100%">
660 <pre class="PROGRAMLISTING">
663 int another_value = 0;
664 int this_variable = 0;
666 if ( this_variable == this_variable )
668 first_value = old_value + ( ( some_value - another_value ) - whatever )
676 <h3 class="SECT3"><a name="S20" id="S20">4.4.5. Don't use white space
677 around structure operators</a></h3>
679 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
681 <p>- structure pointer operator ( "->" ) - member operator ( "." )
682 - functions and parentheses</p>
684 <p>It is a general coding practice to put pointers, references, and
685 function parentheses next to names. With spaces, the connection
686 between the object and variable/function name is not as clear.</p>
688 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
690 <table class="c3" border="0" width="100%">
693 <pre class="PROGRAMLISTING">
694 a_struct->a_member;
702 <p><span class="emphasis EMPHASIS c2">Instead of:</span> a_struct
703 -> a_member; a_struct . a_member; function_name ();</p>
707 <h3 class="SECT3"><a name="S21" id="S21">4.4.6. Make the last brace
708 of a function stand out</a></h3>
710 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
712 <table class="c3" border="0" width="100%">
715 <pre class="PROGRAMLISTING">
721 } /* -END- function1 */
726 } /* -END- function2 */
732 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
734 <p>int function1( ... ) { ...code... return( ret_code ); } int
735 function2( ... ) { }</p>
737 <p><span class="emphasis EMPHASIS c2">Note:</span> Use 1 blank line
738 before the closing brace and 2 lines afterward. This makes the end of
739 function standout to the most casual viewer. Although function
740 comments help separate functions, this is still a good coding
741 practice. In fact, I follow these rules when using blocks in "for",
742 "while", "do" loops, and long if {} statements too. After all
743 whitespace is free!</p>
745 <p><span class="emphasis EMPHASIS c2">Status:</span>
746 developer-discretion on the number of blank lines. Enforced is the
747 end of function comments.</p>
751 <h3 class="SECT3"><a name="S22" id="S22">4.4.7. Use 3 character
754 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
756 <p>If some use 8 character TABs and some use 3 character TABs, the
757 code can look *very* ragged. So use 3 character indentions only. If
758 you like to use TABs, pass your code through a filter such as "expand
759 -t3" before checking in your code.</p>
761 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
763 <table class="c3" border="0" width="100%">
766 <pre class="PROGRAMLISTING">
767 static const char * const url_code_map[256] =
777 return( ALWAYS_TRUE );
781 return( HOW_DID_YOU_GET_HERE );
784 return( NEVER_GETS_HERE );
795 <h2 class="SECT2"><a name="S23" id="S23">4.5. Initializing</a></h2>
798 <h3 class="SECT3"><a name="S24" id="S24">4.5.1. Initialize all
801 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
803 <p>Do not assume that the variables declared will not be used until
804 after they have been assigned a value somewhere else in the code.
805 Remove the chance of accidentally using an unassigned variable.</p>
807 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
809 <table class="c3" border="0" width="100%">
812 <pre class="PROGRAMLISTING">
821 <p><span class="emphasis EMPHASIS c2">Note:</span> It is much easier
822 to debug a SIGSEGV if the message says you are trying to access
823 memory address 00000000 and not 129FA012; or array_ptr[20] causes a
824 SIGSEV vs. array_ptr[0].</p>
826 <p><span class="emphasis EMPHASIS c2">Status:</span>
827 developer-discretion if and only if the variable is assigned a value
828 "shortly after" declaration.</p>
833 <h2 class="SECT2"><a name="S25" id="S25">4.6. Functions</a></h2>
836 <h3 class="SECT3"><a name="S26" id="S26">4.6.1. Name functions that
837 return a boolean as a question.</a></h3>
839 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
841 <p>Value should be phrased as a question that would logically be
842 answered as a true or false statement</p>
844 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
846 <table class="c3" border="0" width="100%">
849 <pre class="PROGRAMLISTING">
850 should_we_block_this();
860 <h3 class="SECT3"><a name="S27" id="S27">4.6.2. Always specify a
861 return type for a function.</a></h3>
863 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
865 <p>The default return for a function is an int. To avoid ambiguity,
866 create a return for a function when the return has a purpose, and
867 create a void return type if the function does not need to return
872 <h3 class="SECT3"><a name="S28" id="S28">4.6.3. Minimize function
873 calls when iterating by using variables</a></h3>
875 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
877 <p>It is easy to write the following code, and a clear argument can
878 be made that the code is easy to understand:</p>
880 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
882 <table class="c3" border="0" width="100%">
885 <pre class="PROGRAMLISTING">
886 for ( size_t cnt = 0; cnt < block_list_length(); cnt++ )
895 <p><span class="emphasis EMPHASIS c2">Note:</span> Unfortunately,
896 this makes a function call for each and every iteration. This
897 increases the overhead in the program, because the compiler has to
898 look up the function each time, call it, and return a value.
899 Depending on what occurs in the block_list_length() call, it might
900 even be creating and destroying structures with each iteration, even
901 though in each case it is comparing "cnt" to the same value, over and
902 over. Remember too - even a call to block_list_length() is a function
903 call, with the same overhead.</p>
905 <p>Instead of using a function call during the iterations, assign the
906 value to a variable, and evaluate using the variable.</p>
908 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
910 <table class="c3" border="0" width="100%">
913 <pre class="PROGRAMLISTING">
914 size_t len = block_list_length();
916 for ( size_t cnt = 0; cnt < len; cnt++ )
925 <p><span class="emphasis EMPHASIS c2">Exceptions:</span> if the value
926 of block_list_length() *may* change or could *potentially* change,
927 then you must code the function call in the for/while loop.</p>
931 <h3 class="SECT3"><a name="S29" id="S29">4.6.4. Pass and Return by
932 Const Reference</a></h3>
934 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
936 <p>This allows a developer to define a const pointer and call your
937 function. If your function does not have the const keyword, we may
938 not be able to use your function. Consider strcmp, if it were defined
939 as: extern int strcmp( char *s1, char *s2 );</p>
941 <p>I could then not use it to compare argv's in main: int main( int
942 argc, const char *argv[] ) { strcmp( argv[0], "privoxy" ); }</p>
944 <p>Both these pointers are *const*! If the c runtime library
945 maintainers do it, we should too.</p>
949 <h3 class="SECT3"><a name="S30" id="S30">4.6.5. Pass and Return by
952 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
954 <p>Most structures cannot fit onto a normal stack entry (i.e. they
955 are not 4 bytes or less). Aka, a function declaration like: int
956 load_aclfile( struct client_state csp )</p>
958 <p>would not work. So, to be consistent, we should declare all
959 prototypes with "pass by value": int load_aclfile( struct
960 client_state *csp )</p>
964 <h3 class="SECT3"><a name="S31" id="S31">4.6.6. Names of include
967 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
969 <p>Your include statements should contain the file name without a
970 path. The path should be listed in the Makefile, using -I as
971 processor directive to search the indicated paths. An exception to
972 this would be for some proprietary software that utilizes a partial
973 path to distinguish their header files from system or other header
976 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
978 <table class="c3" border="0" width="100%">
981 <pre class="PROGRAMLISTING">
982 #include <iostream.h> /* This is not a local include */
983 #include "config.h" /* This IS a local include */
989 <p><span class="emphasis EMPHASIS c2">Exception:</span></p>
991 <table class="c3" border="0" width="100%">
994 <pre class="PROGRAMLISTING">
995 /* This is not a local include, but requires a path element. */
996 #include <sys/fileName.h>
1002 <p><span class="emphasis EMPHASIS c2">Note:</span> Please! do not add
1003 "-I." to the Makefile without a _very_ good reason. This duplicates
1004 the #include "file.h" behavior.</p>
1008 <h3 class="SECT3"><a name="S32" id="S32">4.6.7. Provide multiple
1009 inclusion protection</a></h3>
1011 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1013 <p>Prevents compiler and linker errors resulting from redefinition of
1016 <p>Wrap each header file with the following syntax to prevent
1017 multiple inclusions of the file. Of course, replace PROJECT_H with
1018 your file name, with "." Changed to "_", and make it uppercase.</p>
1020 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
1022 <table class="c3" border="0" width="100%">
1025 <pre class="PROGRAMLISTING">
1026 #ifndef PROJECT_H_INCLUDED
1027 #define PROJECT_H_INCLUDED
1029 #endif /* ndef PROJECT_H_INCLUDED */
1037 <h3 class="SECT3"><a name="S33" id="S33">4.6.8. Use `extern "C"` when
1038 appropriate</a></h3>
1040 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1042 <p>If our headers are included from C++, they must declare our
1043 functions as `extern "C"`. This has no cost in C, but increases the
1044 potential re-usability of our code.</p>
1046 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
1048 <table class="c3" border="0" width="100%">
1051 <pre class="PROGRAMLISTING">
1055 #endif /* def __cplusplus */
1057 ... function definitions here ...
1061 #endif /* def __cplusplus */
1069 <h3 class="SECT3"><a name="S34" id="S34">4.6.9. Where Possible, Use
1070 Forward Struct Declaration Instead of Includes</a></h3>
1072 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1074 <p>Useful in headers that include pointers to other struct's.
1075 Modifications to excess header files may cause needless compiles.</p>
1077 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
1079 <table class="c3" border="0" width="100%">
1082 <pre class="PROGRAMLISTING">
1083 /*********************************************************************
1084 * We're avoiding an include statement here!
1085 *********************************************************************/
1087 extern file_list *xyz;
1093 <p><span class="emphasis EMPHASIS c2">Note:</span> If you declare
1094 "file_list xyz;" (without the pointer), then including the proper
1095 header file is necessary. If you only want to prototype a pointer,
1096 however, the header file is unnecessary.</p>
1098 <p><span class="emphasis EMPHASIS c2">Status:</span> Use with
1104 <h2 class="SECT2"><a name="S35" id="S35">4.7. General Coding
1108 <h3 class="SECT3"><a name="S36" id="S36">4.7.1. Turn on
1111 <p><span class="emphasis EMPHASIS c2">Explanation</span></p>
1113 <p>Compiler warnings are meant to help you find bugs. You should turn
1114 on as many as possible. With GCC, the switch is "-Wall". Try and fix
1115 as many warnings as possible.</p>
1119 <h3 class="SECT3"><a name="S37" id="S37">4.7.2. Provide a default
1120 case for all switch statements</a></h3>
1122 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1124 <p>What you think is guaranteed is never really guaranteed. The value
1125 that you don't think you need to check is the one that someday will
1126 be passed. So, to protect yourself from the unknown, always have a
1127 default step in a switch statement.</p>
1129 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
1131 <table class="c3" border="0" width="100%">
1134 <pre class="PROGRAMLISTING">
1135 switch( hash_string( cmd ) )
1137 case hash_actions_file :
1147 ... anomaly code goes here ...
1148 continue; / break; / exit( 1 ); / etc ...
1150 } /* end switch( hash_string( cmd ) ) */
1156 <p><span class="emphasis EMPHASIS c2">Note:</span> If you already
1157 have a default condition, you are obviously exempt from this point.
1158 Of note, most of the WIN32 code calls `DefWindowProc' after the
1159 switch statement. This API call *should* be included in a default
1162 <p><span class="emphasis EMPHASIS c2">Another Note:</span> This is
1163 not so much a readability issue as a robust programming issue. The
1164 "anomaly code goes here" may be no more than a print to the STDERR
1165 stream (as in load_config). Or it may really be an abort
1168 <p><span class="emphasis EMPHASIS c2">Status:</span> Programmer
1169 discretion is advised.</p>
1173 <h3 class="SECT3"><a name="S38" id="S38">4.7.3. Try to avoid falling
1174 through cases in a switch statement.</a></h3>
1176 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1178 <p>In general, you will want to have a 'break' statement within each
1179 'case' of a switch statement. This allows for the code to be more
1180 readable and understandable, and furthermore can prevent unwanted
1181 surprises if someone else later gets creative and moves the code
1184 <p>The language allows you to plan the fall through from one case
1185 statement to another simply by omitting the break statement within
1186 the case statement. This feature does have benefits, but should only
1187 be used in rare cases. In general, use a break statement for each
1190 <p>If you choose to allow fall through, you should comment both the
1191 fact of the fall through and reason why you felt it was
1196 <h3 class="SECT3"><a name="S39" id="S39">4.7.4. Use 'long' or 'short'
1197 Instead of 'int'</a></h3>
1199 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1201 <p>On 32-bit platforms, int usually has the range of long. On 16-bit
1202 platforms, int has the range of short.</p>
1204 <p><span class="emphasis EMPHASIS c2">Status:</span> open-to-debate.
1205 In the case of most FSF projects (including X/GNU-Emacs), there are
1206 typedefs to int4, int8, int16, (or equivalence ... I forget the exact
1207 typedefs now). Should we add these to IJB now that we have a
1208 "configure" script?</p>
1212 <h3 class="SECT3"><a name="S40" id="S40">4.7.5. Don't mix size_t and
1213 other types</a></h3>
1215 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1217 <p>The type of size_t varies across platforms. Do not make
1218 assumptions about whether it is signed or unsigned, or about how long
1219 it is. Do not compare a size_t against another variable of a
1220 different type (or even against a constant) without casting one of
1225 <h3 class="SECT3"><a name="S41" id="S41">4.7.6. Declare each variable
1226 and struct on its own line.</a></h3>
1228 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1230 <p>It can be tempting to declare a series of variables all on one
1233 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
1235 <table class="c3" border="0" width="100%">
1238 <pre class="PROGRAMLISTING">
1247 <p><span class="emphasis EMPHASIS c2">Instead of:</span></p>
1249 <p>long a, b, c;</p>
1251 <p><span class="emphasis EMPHASIS c2">Explanation:</span> - there is
1252 more room for comments on the individual variables - easier to add
1253 new variables without messing up the original ones - when searching
1254 on a variable to find its type, there is less clutter to "visually"
1257 <p><span class="emphasis EMPHASIS c2">Exceptions:</span> when you
1258 want to declare a bunch of loop variables or other trivial variables;
1259 feel free to declare them on one line. You should, although, provide
1260 a good comment on their functions.</p>
1262 <p><span class="emphasis EMPHASIS c2">Status:</span>
1263 developer-discretion.</p>
1267 <h3 class="SECT3"><a name="S42" id="S42">4.7.7. Use malloc/zalloc
1270 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1272 <p>Create a local struct (on the stack) if the variable will live and
1273 die within the context of one function call.</p>
1275 <p>Only "malloc" a struct (on the heap) if the variable's life will
1276 extend beyond the context of one function call.</p>
1278 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
1280 <table class="c3" border="0" width="100%">
1283 <pre class="PROGRAMLISTING">
1284 If a function creates a struct and stores a pointer to it in a
1285 list, then it should definitely be allocated via `malloc'.
1293 <h3 class="SECT3"><a name="S43" id="S43">4.7.8. The Programmer Who
1294 Uses 'malloc' is Responsible for Ensuring 'free'</a></h3>
1296 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1298 <p>If you have to "malloc" an instance, you are responsible for
1299 insuring that the instance is `free'd, even if the deallocation event
1300 falls within some other programmer's code. You are also responsible
1301 for ensuring that deletion is timely (i.e. not too soon, not too
1302 late). This is known as "low-coupling" and is a "good thing (tm)".
1303 You may need to offer a free/unload/destructor type function to
1304 accommodate this.</p>
1306 <p><span class="emphasis EMPHASIS c2">Example:</span></p>
1308 <table class="c3" border="0" width="100%">
1311 <pre class="PROGRAMLISTING">
1312 int load_re_filterfile( struct client_state *csp ) { ... }
1313 static void unload_re_filterfile( void *f ) { ... }
1319 <p><span class="emphasis EMPHASIS c2">Exceptions:</span></p>
1321 <p>The developer cannot be expected to provide `free'ing functions
1322 for C run-time library functions ... such as `strdup'.</p>
1324 <p><span class="emphasis EMPHASIS c2">Status:</span>
1325 developer-discretion. The "main" use of this standard is for
1326 allocating and freeing data structures (complex or nested).</p>
1330 <h3 class="SECT3"><a name="S44" id="S44">4.7.9. Add loaders to the
1331 `file_list' structure and in order</a></h3>
1333 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1335 <p>I have ordered all of the "blocker" file code to be in alpha
1336 order. It is easier to add/read new blockers when you expect a
1339 <p><span class="emphasis EMPHASIS c2">Note:</span> It may appear that
1340 the alpha order is broken in places by POPUP tests coming before PCRS
1341 tests. But since POPUPs can also be referred to as KILLPOPUPs, it is
1342 clear that it should come first.</p>
1346 <h3 class="SECT3"><a name="S45" id="S45">4.7.10. "Uncertain" new code
1347 and/or changes to existing code, use FIXME or XXX</a></h3>
1349 <p><span class="emphasis EMPHASIS c2">Explanation:</span></p>
1351 <p>If you have enough confidence in new code or confidence in your
1352 changes, but are not *quite* sure of the repercussions, add this:</p>
1354 <p>/* FIXME: this code has a logic error on platform XYZ, *
1355 attempting to fix */ #ifdef PLATFORM ...changed code here...
1360 <p>/* FIXME: I think the original author really meant this... */
1361 ...changed code here...</p>
1365 <p>/* FIXME: new code that *may* break something else... */ ...new
1368 <p><span class="emphasis EMPHASIS c2">Note:</span> If you make it
1369 clear that this may or may not be a "good thing (tm)", it will be
1370 easier to identify and include in the project (or conversely exclude
1371 from the project).</p>
1376 <h2 class="SECT2"><a name="S46" id="S46">4.8. Addendum: Template for
1377 files and function comment blocks:</a></h2>
1379 <p><span class="emphasis EMPHASIS c2">Example for file
1380 comments:</span></p>
1382 <table class="c3" border="0" width="100%">
1385 <pre class="PROGRAMLISTING">
1386 const char FILENAME_rcs[] = "$Id$";
1387 /*********************************************************************
1391 * Purpose : (Fill me in with a good description!)
1393 * Copyright : Written by and Copyright (C) 2001-2009
1394 * the Privoxy team. http://www.privoxy.org/
1396 * This program is free software; you can redistribute it
1397 * and/or modify it under the terms of the GNU General
1398 * Public License as published by the Free Software
1399 * Foundation; either version 2 of the License, or (at
1400 * your option) any later version.
1402 * This program is distributed in the hope that it will
1403 * be useful, but WITHOUT ANY WARRANTY; without even the
1404 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1405 * PARTICULAR PURPOSE. See the GNU General Public
1406 * License for more details.
1408 * The GNU General Public License should be included with
1409 * this file. If not, you can view it at
1410 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
1411 * or write to the Free Software Foundation, Inc.,
1412 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
1415 *********************************************************************/
1420 ...necessary include files for us to do our work...
1422 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
1428 <p><span class="emphasis EMPHASIS c2">Note:</span> This declares the
1429 rcs variables that should be added to the "show-proxy-args" page. If
1430 this is a brand new creation by you, you are free to change the
1431 "Copyright" section to represent the rights you wish to maintain.</p>
1433 <p><span class="emphasis EMPHASIS c2">Note:</span> The formfeed
1434 character that is present right after the comment flower box is handy
1435 for (X|GNU)Emacs users to skip the verbiage and get to the heart of the
1436 code (via `forward-page' and `backward-page'). Please include it if you
1439 <p><span class="emphasis EMPHASIS c2">Example for file header
1440 comments:</span></p>
1442 <table class="c3" border="0" width="100%">
1445 <pre class="PROGRAMLISTING">
1448 #define FILENAME_H_VERSION "$Id$"
1449 /*********************************************************************
1453 * Purpose : (Fill me in with a good description!)
1455 * Copyright : Written by and Copyright (C) 2001-2009
1456 * the Privoxy team. http://www.privoxy.org/
1458 * This program is free software; you can redistribute it
1459 * and/or modify it under the terms of the GNU General
1460 * Public License as published by the Free Software
1461 * Foundation; either version 2 of the License, or (at
1462 * your option) any later version.
1464 * This program is distributed in the hope that it will
1465 * be useful, but WITHOUT ANY WARRANTY; without even the
1466 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1467 * PARTICULAR PURPOSE. See the GNU General Public
1468 * License for more details.
1470 * The GNU General Public License should be included with
1471 * this file. If not, you can view it at
1472 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
1473 * or write to the Free Software Foundation, Inc.,
1474 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
1477 *********************************************************************/
1480 #include "project.h"
1486 ... function headers here ...
1489 /* Revision control strings from this header and associated .c file */
1490 extern const char FILENAME_rcs[];
1491 extern const char FILENAME_h_rcs[];
1498 #endif /* ndef _FILENAME_H */
1510 <p><span class="emphasis EMPHASIS c2">Example for function
1511 comments:</span></p>
1513 <table class="c3" border="0" width="100%">
1516 <pre class="PROGRAMLISTING">
1517 /*********************************************************************
1519 * Function : FUNCTION_NAME
1521 * Description : (Fill me in with a good description!)
1524 * 1 : param1 = pointer to an important thing
1525 * 2 : x = pointer to something else
1527 * Returns : 0 => Ok, everything else is an error.
1529 *********************************************************************/
1530 int FUNCTION_NAME( void *param1, const char *x )
1541 <p><span class="emphasis EMPHASIS c2">Note:</span> If we all follow
1542 this practice, we should be able to parse our code to create a
1543 "self-documenting" web page.</p>
1547 <div class="NAVFOOTER">
1548 <hr class="c1" width="100%">
1550 <table summary="Footer navigation table" width="100%" border="0"
1551 cellpadding="0" cellspacing="0">
1553 <td width="33%" align="left" valign="top"><a href=
1554 "documentation.html" accesskey="P">Prev</a></td>
1556 <td width="34%" align="center" valign="top"><a href="index.html"
1557 accesskey="H">Home</a></td>
1559 <td width="33%" align="right" valign="top"><a href="testing.html"
1560 accesskey="N">Next</a></td>
1564 <td width="33%" align="left" valign="top">Documentation
1567 <td width="34%" align="center" valign="top"> </td>
1569 <td width="33%" align="right" valign="top">Testing Guidelines</td>