1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
2 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
8 <meta name="GENERATOR" content=
9 "Modular DocBook HTML Stylesheet Version 1.79">
10 <link rel="HOME" title="Privoxy Developer Manual" href="index.html">
11 <link rel="PREVIOUS" title="Documentation Guidelines" href=
13 <link rel="NEXT" title="Testing Guidelines" href="testing.html">
14 <link rel="STYLESHEET" type="text/css" href="../p_doc.css">
15 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
17 <body class="SECT1" bgcolor="#EEEEEE" text="#000000" link="#0000FF" vlink=
18 "#840084" alink="#0000FF">
19 <div class="NAVHEADER">
20 <table summary="Header navigation table" width="100%" border="0"
21 cellpadding="0" cellspacing="0">
23 <th colspan="3" align="center">
24 Privoxy Developer Manual
28 <td width="10%" align="left" valign="bottom">
29 <a href="documentation.html" accesskey="P">Prev</a>
31 <td width="80%" align="center" valign="bottom">
33 <td width="10%" align="right" valign="bottom">
34 <a href="testing.html" accesskey="N">Next</a>
38 <hr align="LEFT" width="100%">
42 <a name="CODING">4. Coding Guidelines</a>
46 <a name="S1">4.1. Introduction</a>
49 This set of standards is designed to make our lives easier. It is
50 developed with the simple goal of helping us keep the "new and
51 improved <span class="APPLICATION">Privoxy</span>" consistent and
52 reliable. Thus making maintenance easier and increasing chances of
53 success of the project.
56 And that of course comes back to us as individuals. If we can
57 increase our development and product efficiencies then we can solve
58 more of the request for changes/improvements and in general feel
59 good about ourselves. ;->
64 <a name="S2">4.2. Using Comments</a>
68 <a name="S3">4.2.1. Comment, Comment, Comment</a>
71 <span class="emphasis"><i class=
72 "EMPHASIS">Explanation:</i></span>
75 Comment as much as possible without commenting the obvious. For
76 example do not comment "variable_a is equal to variable_b".
77 Instead explain why variable_a should be equal to the variable_b.
78 Just because a person can read code does not mean they will
79 understand why or what is being done. A reader may spend a lot
80 more time figuring out what is going on when a simple comment or
81 explanation would have prevented the extra research. Please help
82 your fellow Privoxy developers out!
85 The comments will also help justify the intent of the code. If
86 the comment describes something different than what the code is
87 doing then maybe a programming error is occurring.
90 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
92 <table border="0" bgcolor="#E0E0E0" width="100%">
95 <pre class="PROGRAMLISTING">
96 /* if page size greater than 1k ... */
97 if (page_length() > 1024)
99 ... "block" the page up ...
102 /* if page size is small, send it in blocks */
103 if (page_length() > 1024)
105 ... "block" the page up ...
108 This demonstrates 2 cases of "what not to do". The first is a
109 "syntax comment". The second is a comment that does not fit what
110 is actually being done.
118 <a name="S4">4.2.2. Use blocks for comments</a>
121 <span class="emphasis"><i class=
122 "EMPHASIS">Explanation:</i></span>
125 Comments can help or they can clutter. They help when they are
126 differentiated from the code they describe. One line comments do
127 not offer effective separation between the comment and the code.
128 Block identifiers do, by surrounding the code with a clear,
132 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
134 <table border="0" bgcolor="#E0E0E0" width="100%">
137 <pre class="PROGRAMLISTING">
138 /*********************************************************************
139 * This will stand out clearly in your code!
140 *********************************************************************/
141 if (this_variable == that_variable)
143 do_something_very_important();
147 /* unfortunately, this may not */
148 if (this_variable == that_variable)
150 do_something_very_important();
154 if (this_variable == that_variable) /* this may not either */
156 do_something_very_important();
163 <span class="emphasis"><i class="EMPHASIS">Exception:</i></span>
166 If you are trying to add a small logic comment and do not wish to
167 "disrupt" the flow of the code, feel free to use a 1 line comment
168 which is NOT on the same line as the code.
173 <a name="S5">4.2.3. Keep Comments on their own line</a>
176 <span class="emphasis"><i class=
177 "EMPHASIS">Explanation:</i></span>
180 It goes back to the question of readability. If the comment is on
181 the same line as the code it will be harder to read than the
182 comment that is on its own line.
185 There are three exceptions to this rule, which should be violated
186 freely and often: during the definition of variables, at the end
187 of closing braces, when used to comment parameters.
190 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
192 <table border="0" bgcolor="#E0E0E0" width="100%">
195 <pre class="PROGRAMLISTING">
196 /*********************************************************************
197 * This will stand out clearly in your code,
198 * But the second example won't.
199 *********************************************************************/
200 if (this_variable == this_variable)
202 do_something_very_important();
205 if (this_variable == this_variable) /*can you see me?*/
207 do_something_very_important(); /*not easily*/
211 /*********************************************************************
212 * But, the encouraged exceptions:
213 *********************************************************************/
214 int urls_read = 0; /* # of urls read + rejected */
215 int urls_rejected = 0; /* # of urls rejected */
219 do_something_very_important();
223 short do_something_very_important(
224 short firstparam, /* represents something */
225 short nextparam /* represents something else */ )
229 } /* -END- do_something_very_important */
237 <a name="S6">4.2.4. Comment each logical step</a>
240 <span class="emphasis"><i class=
241 "EMPHASIS">Explanation:</i></span>
244 Logical steps should be commented to help others follow the
245 intent of the written code and comments will make the code more
249 If you have 25 lines of code without a comment, you should
250 probably go back into it to see where you forgot to put one.
253 Most "for", "while", "do", etc... loops _probably_ need a
254 comment. After all, these are usually major logic containers.
259 <a name="S7">4.2.5. Comment All Functions Thoroughly</a>
262 <span class="emphasis"><i class=
263 "EMPHASIS">Explanation:</i></span>
266 A reader of the code should be able to look at the comments just
267 prior to the beginning of a function and discern the reason for
268 its existence and the consequences of using it. The reader should
269 not have to read through the code to determine if a given
270 function is safe for a desired use. The proper information
271 thoroughly presented at the introduction of a function not only
272 saves time for subsequent maintenance or debugging, it more
273 importantly aids in code reuse by allowing a user to determine
274 the safety and applicability of any function for the problem at
275 hand. As a result of such benefits, all functions should contain
276 the information presented in the addendum section of this
282 <a name="S8">4.2.6. Comment at the end of braces if the content
283 is more than one screen length</a>
286 <span class="emphasis"><i class=
287 "EMPHASIS">Explanation:</i></span>
290 Each closing brace should be followed on the same line by a
291 comment that describes the origination of the brace if the
292 original brace is off of the screen, or otherwise far away from
293 the closing brace. This will simplify the debugging, maintenance,
294 and readability of the code.
297 As a suggestion , use the following flags to make the comment and
298 its brace more readable:
301 use following a closing brace: } /* -END- if() or while () or
305 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
307 <table border="0" bgcolor="#E0E0E0" width="100%">
310 <pre class="PROGRAMLISTING">
313 do_something_very_important();
314 ...some long list of commands...
315 } /* -END- if x is 1 */
321 do_something_very_important();
322 ...some long list of commands...
323 } /* -END- if (1 == X) */
332 <a name="S9">4.3. Naming Conventions</a>
336 <a name="S10">4.3.1. Variable Names</a>
339 <span class="emphasis"><i class=
340 "EMPHASIS">Explanation:</i></span>
343 Use all lowercase, and separate words via an underscore ('_'). Do
344 not start an identifier with an underscore. (ANSI C reserves
345 these for use by the compiler and system headers.) Do not use
346 identifiers which are reserved in ANSI C++. (E.g. template,
347 class, true, false, ...). This is in case we ever decide to port
351 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
353 <table border="0" bgcolor="#E0E0E0" width="100%">
356 <pre class="PROGRAMLISTING">
357 int ms_iis5_hack = 0;
363 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
365 <table border="0" bgcolor="#E0E0E0" width="100%">
368 <pre class="PROGRAMLISTING">
369 int msiis5hack = 0; int msIis5Hack = 0;
377 <a name="S11">4.3.2. Function Names</a>
380 <span class="emphasis"><i class=
381 "EMPHASIS">Explanation:</i></span>
384 Use all lowercase, and separate words via an underscore ('_'). Do
385 not start an identifier with an underscore. (ANSI C reserves
386 these for use by the compiler and system headers.) Do not use
387 identifiers which are reserved in ANSI C++. (E.g. template,
388 class, true, false, ...). This is in case we ever decide to port
392 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
394 <table border="0" bgcolor="#E0E0E0" width="100%">
397 <pre class="PROGRAMLISTING">
398 int load_some_file(struct client_state *csp)
404 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
406 <table border="0" bgcolor="#E0E0E0" width="100%">
409 <pre class="PROGRAMLISTING">
410 int loadsomefile(struct client_state *csp)
411 int loadSomeFile(struct client_state *csp)
419 <a name="S12">4.3.3. Header file prototypes</a>
422 <span class="emphasis"><i class=
423 "EMPHASIS">Explanation:</i></span>
426 Use a descriptive parameter name in the function prototype in
427 header files. Use the same parameter name in the header file that
428 you use in the c file.
431 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
433 <table border="0" bgcolor="#E0E0E0" width="100%">
436 <pre class="PROGRAMLISTING">
437 (.h) extern int load_aclfile(struct client_state *csp);
438 (.c) int load_aclfile(struct client_state *csp)
444 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
446 <table border="0" bgcolor="#E0E0E0" width="100%">
449 <pre class="PROGRAMLISTING">
450 (.h) extern int load_aclfile(struct client_state *); or
451 (.h) extern int load_aclfile();
452 (.c) int load_aclfile(struct client_state *csp)
460 <a name="S13">4.3.4. Enumerations, and #defines</a>
463 <span class="emphasis"><i class=
464 "EMPHASIS">Explanation:</i></span>
467 Use all capital letters, with underscores between words. Do not
468 start an identifier with an underscore. (ANSI C reserves these
469 for use by the compiler and system headers.)
472 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
474 <table border="0" bgcolor="#E0E0E0" width="100%">
477 <pre class="PROGRAMLISTING">
478 (enumeration) : enum Boolean {FALSE, TRUE};
479 (#define) : #define DEFAULT_SIZE 100;
485 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> We
486 have a standard naming scheme for #defines that toggle a feature
487 in the preprocessor: FEATURE_>, where > is a short
488 (preferably 1 or 2 word) description.
491 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
493 <table border="0" bgcolor="#E0E0E0" width="100%">
496 <pre class="PROGRAMLISTING">
497 #define FEATURE_FORCE 1
500 #define FORCE_PREFIX blah
501 #endif /* def FEATURE_FORCE */
509 <a name="S14">4.3.5. Constants</a>
512 <span class="emphasis"><i class=
513 "EMPHASIS">Explanation:</i></span>
516 Spell common words out entirely (do not remove vowels).
519 Use only widely-known domain acronyms and abbreviations.
520 Capitalize all letters of an acronym.
523 Use underscore (_) to separate adjacent acronyms and
524 abbreviations. Never terminate a name with an underscore.
527 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
529 <table border="0" bgcolor="#E0E0E0" width="100%">
532 <pre class="PROGRAMLISTING">
533 #define USE_IMAGE_LIST 1
539 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
541 <table border="0" bgcolor="#E0E0E0" width="100%">
544 <pre class="PROGRAMLISTING">
545 #define USE_IMG_LST 1 or
546 #define _USE_IMAGE_LIST 1 or
547 #define USE_IMAGE_LIST_ 1 or
548 #define use_image_list 1 or
549 #define UseImageList 1
558 <a name="S15">4.4. Using Space</a>
562 <a name="S16">4.4.1. Put braces on a line by themselves.</a>
565 <span class="emphasis"><i class=
566 "EMPHASIS">Explanation:</i></span>
569 The brace needs to be on a line all by itself, not at the end of
570 the statement. Curly braces should line up with the construct
571 that they're associated with. This practice makes it easier to
572 identify the opening and closing braces for a block.
575 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
577 <table border="0" bgcolor="#E0E0E0" width="100%">
580 <pre class="PROGRAMLISTING">
590 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
593 if (this == that) { ... }
599 if (this == that) { ... }
602 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> In
603 the special case that the if-statement is inside a loop, and it
604 is trivial, i.e. it tests for a condition that is obvious from
605 the purpose of the block, one-liners as above may optically
606 preserve the loop structure and make it easier to read.
609 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
610 developer-discretion.
613 <span class="emphasis"><i class="EMPHASIS">Example
614 exception:</i></span>
616 <table border="0" bgcolor="#E0E0E0" width="100%">
619 <pre class="PROGRAMLISTING">
620 while (more lines are read)
622 /* Please document what is/is not a comment line here */
623 if (it's a comment) continue;
634 <a name="S17">4.4.2. ALL control statements should have a
638 <span class="emphasis"><i class=
639 "EMPHASIS">Explanation:</i></span>
642 Using braces to make a block will make your code more readable
643 and less prone to error. All control statements should have a
647 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
649 <table border="0" bgcolor="#E0E0E0" width="100%">
652 <pre class="PROGRAMLISTING">
663 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
666 if (this == that) do_something(); do_something_else();
672 if (this == that) do_something();
675 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> The
676 first example in "Instead of" will execute in a manner other than
677 that which the developer desired (per indentation). Using code
678 braces would have prevented this "feature". The "explanation" and
679 "exception" from the point above also applies.
684 <a name="S18">4.4.3. Do not belabor/blow-up boolean
688 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
690 <table border="0" bgcolor="#E0E0E0" width="100%">
693 <pre class="PROGRAMLISTING">
694 structure->flag = (condition);
700 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
703 if (condition) { structure->flag = 1; } else {
704 structure->flag = 0; }
707 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> The
708 former is readable and concise. The later is wordy and
709 inefficient. Please assume that any developer new to the project
710 has at least a "good" knowledge of C/C++. (Hope I do not offend
711 by that last comment ... 8-)
716 <a name="S19">4.4.4. Use white space freely because it is
720 <span class="emphasis"><i class=
721 "EMPHASIS">Explanation:</i></span>
724 Make it readable. The notable exception to using white space
725 freely is listed in the next guideline.
728 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
730 <table border="0" bgcolor="#E0E0E0" width="100%">
733 <pre class="PROGRAMLISTING">
736 int another_value = 0;
737 int this_variable = 0;
745 <a name="S20">4.4.5. Don't use white space around structure
749 <span class="emphasis"><i class=
750 "EMPHASIS">Explanation:</i></span>
753 - structure pointer operator ( "->" ) - member operator ( "."
754 ) - functions and parentheses
757 It is a general coding practice to put pointers, references, and
758 function parentheses next to names. With spaces, the connection
759 between the object and variable/function name is not as clear.
762 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
764 <table border="0" bgcolor="#E0E0E0" width="100%">
767 <pre class="PROGRAMLISTING">
768 a_struct->a_member;
776 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
777 a_struct -> a_member; a_struct . a_member; function_name ();
782 <a name="S21">4.4.6. Make the last brace of a function stand
786 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
788 <table border="0" bgcolor="#E0E0E0" width="100%">
791 <pre class="PROGRAMLISTING">
797 } /* -END- function1 */
802 } /* -END- function2 */
808 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
811 int function1( ... ) { ...code... return(ret_code); } int
815 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> Use 1
816 blank line before the closing brace and 2 lines afterward. This
817 makes the end of function standout to the most casual viewer.
818 Although function comments help separate functions, this is still
819 a good coding practice. In fact, I follow these rules when using
820 blocks in "for", "while", "do" loops, and long if {} statements
821 too. After all whitespace is free!
824 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
825 developer-discretion on the number of blank lines. Enforced is
826 the end of function comments.
831 <a name="S22">4.4.7. Use 3 character indentions</a>
834 <span class="emphasis"><i class=
835 "EMPHASIS">Explanation:</i></span>
838 If some use 8 character TABs and some use 3 character TABs, the
839 code can look *very* ragged. So use 3 character indentions only.
840 If you like to use TABs, pass your code through a filter such as
841 "expand -t3" before checking in your code.
844 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
846 <table border="0" bgcolor="#E0E0E0" width="100%">
849 <pre class="PROGRAMLISTING">
850 static const char * const url_code_map[256] =
864 return HOW_DID_YOU_GET_HERE;
867 return NEVER_GETS_HERE;
878 <a name="S23">4.5. Initializing</a>
882 <a name="S24">4.5.1. Initialize all variables</a>
885 <span class="emphasis"><i class=
886 "EMPHASIS">Explanation:</i></span>
889 Do not assume that the variables declared will not be used until
890 after they have been assigned a value somewhere else in the code.
891 Remove the chance of accidentally using an unassigned variable.
894 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
896 <table border="0" bgcolor="#E0E0E0" width="100%">
899 <pre class="PROGRAMLISTING">
908 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> It is
909 much easier to debug a SIGSEGV if the message says you are trying
910 to access memory address 00000000 and not 129FA012; or
911 array_ptr[20] causes a SIGSEV vs. array_ptr[0].
914 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
915 developer-discretion if and only if the variable is assigned a
916 value "shortly after" declaration.
922 <a name="S25">4.6. Functions</a>
926 <a name="S26">4.6.1. Name functions that return a boolean as a
930 <span class="emphasis"><i class=
931 "EMPHASIS">Explanation:</i></span>
934 Value should be phrased as a question that would logically be
935 answered as a true or false statement
938 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
940 <table border="0" bgcolor="#E0E0E0" width="100%">
943 <pre class="PROGRAMLISTING">
944 should_we_block_this();
954 <a name="S27">4.6.2. Always specify a return type for a
958 <span class="emphasis"><i class=
959 "EMPHASIS">Explanation:</i></span>
962 The default return for a function is an int. To avoid ambiguity,
963 create a return for a function when the return has a purpose, and
964 create a void return type if the function does not need to return
970 <a name="S28">4.6.3. Minimize function calls when iterating by
974 <span class="emphasis"><i class=
975 "EMPHASIS">Explanation:</i></span>
978 It is easy to write the following code, and a clear argument can
979 be made that the code is easy to understand:
982 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
984 <table border="0" bgcolor="#E0E0E0" width="100%">
987 <pre class="PROGRAMLISTING">
988 for (size_t cnt = 0; cnt < block_list_length(); cnt++)
997 <span class="emphasis"><i class="EMPHASIS">Note:</i></span>
998 Unfortunately, this makes a function call for each and every
999 iteration. This increases the overhead in the program, because
1000 the compiler has to look up the function each time, call it, and
1001 return a value. Depending on what occurs in the
1002 block_list_length() call, it might even be creating and
1003 destroying structures with each iteration, even though in each
1004 case it is comparing "cnt" to the same value, over and over.
1005 Remember too - even a call to block_list_length() is a function
1006 call, with the same overhead.
1009 Instead of using a function call during the iterations, assign
1010 the value to a variable, and evaluate using the variable.
1013 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1015 <table border="0" bgcolor="#E0E0E0" width="100%">
1018 <pre class="PROGRAMLISTING">
1019 size_t len = block_list_length();
1021 for (size_t cnt = 0; cnt < len; cnt++)
1030 <span class="emphasis"><i class="EMPHASIS">Exceptions:</i></span>
1031 if the value of block_list_length() *may* change or could
1032 *potentially* change, then you must code the function call in the
1038 <a name="S29">4.6.4. Pass and Return by Const Reference</a>
1041 <span class="emphasis"><i class=
1042 "EMPHASIS">Explanation:</i></span>
1045 This allows a developer to define a const pointer and call your
1046 function. If your function does not have the const keyword, we
1047 may not be able to use your function. Consider strcmp, if it were
1048 defined as: extern int strcmp(char *s1, char *s2);
1051 I could then not use it to compare argv's in main: int main(int
1052 argc, const char *argv[]) { strcmp(argv[0], "privoxy"); }
1055 Both these pointers are *const*! If the c runtime library
1056 maintainers do it, we should too.
1061 <a name="S30">4.6.5. Pass and Return by Value</a>
1064 <span class="emphasis"><i class=
1065 "EMPHASIS">Explanation:</i></span>
1068 Most structures cannot fit onto a normal stack entry (i.e. they
1069 are not 4 bytes or less). Aka, a function declaration like: int
1070 load_aclfile(struct client_state csp)
1073 would not work. So, to be consistent, we should declare all
1074 prototypes with "pass by value": int load_aclfile(struct
1080 <a name="S31">4.6.6. Names of include files</a>
1083 <span class="emphasis"><i class=
1084 "EMPHASIS">Explanation:</i></span>
1087 Your include statements should contain the file name without a
1088 path. The path should be listed in the Makefile, using -I as
1089 processor directive to search the indicated paths. An exception
1090 to this would be for some proprietary software that utilizes a
1091 partial path to distinguish their header files from system or
1095 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1097 <table border="0" bgcolor="#E0E0E0" width="100%">
1100 <pre class="PROGRAMLISTING">
1101 #include <iostream.h> /* This is not a local include */
1102 #include "config.h" /* This IS a local include */
1108 <span class="emphasis"><i class="EMPHASIS">Exception:</i></span>
1110 <table border="0" bgcolor="#E0E0E0" width="100%">
1113 <pre class="PROGRAMLISTING">
1114 /* This is not a local include, but requires a path element. */
1115 #include <sys/fileName.h>
1122 <span class="emphasis"><i class="EMPHASIS">Note:</i></span>
1123 Please! do not add "-I." to the Makefile without a _very_ good
1124 reason. This duplicates the #include "file.h" behavior.
1129 <a name="S32">4.6.7. Provide multiple inclusion protection</a>
1132 <span class="emphasis"><i class=
1133 "EMPHASIS">Explanation:</i></span>
1136 Prevents compiler and linker errors resulting from redefinition
1140 Wrap each header file with the following syntax to prevent
1141 multiple inclusions of the file. Of course, replace PROJECT_H
1142 with your file name, with "." Changed to "_", and make it
1146 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1148 <table border="0" bgcolor="#E0E0E0" width="100%">
1151 <pre class="PROGRAMLISTING">
1152 #ifndef PROJECT_H_INCLUDED
1153 #define PROJECT_H_INCLUDED
1155 #endif /* ndef PROJECT_H_INCLUDED */
1163 <a name="S33">4.6.8. Use `extern "C"` when appropriate</a>
1166 <span class="emphasis"><i class=
1167 "EMPHASIS">Explanation:</i></span>
1170 If our headers are included from C++, they must declare our
1171 functions as `extern "C"`. This has no cost in C, but increases
1172 the potential re-usability of our code.
1175 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1177 <table border="0" bgcolor="#E0E0E0" width="100%">
1180 <pre class="PROGRAMLISTING">
1184 #endif /* def __cplusplus */
1186 ... function definitions here ...
1190 #endif /* def __cplusplus */
1198 <a name="S34">4.6.9. Where Possible, Use Forward Struct
1199 Declaration Instead of Includes</a>
1202 <span class="emphasis"><i class=
1203 "EMPHASIS">Explanation:</i></span>
1206 Useful in headers that include pointers to other struct's.
1207 Modifications to excess header files may cause needless compiles.
1210 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1212 <table border="0" bgcolor="#E0E0E0" width="100%">
1215 <pre class="PROGRAMLISTING">
1216 /*********************************************************************
1217 * We're avoiding an include statement here!
1218 *********************************************************************/
1220 extern file_list *xyz;
1226 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If
1227 you declare "file_list xyz;" (without the pointer), then
1228 including the proper header file is necessary. If you only want
1229 to prototype a pointer, however, the header file is unnecessary.
1232 <span class="emphasis"><i class="EMPHASIS">Status:</i></span> Use
1239 <a name="S35">4.7. General Coding Practices</a>
1243 <a name="S36">4.7.1. Turn on warnings</a>
1246 <span class="emphasis"><i class="EMPHASIS">Explanation</i></span>
1249 Compiler warnings are meant to help you find bugs. You should
1250 turn on as many as possible. With GCC, the switch is "-Wall". Try
1251 and fix as many warnings as possible.
1256 <a name="S37">4.7.2. Provide a default case for all switch
1260 <span class="emphasis"><i class=
1261 "EMPHASIS">Explanation:</i></span>
1264 What you think is guaranteed is never really guaranteed. The
1265 value that you don't think you need to check is the one that
1266 someday will be passed. So, to protect yourself from the unknown,
1267 always have a default step in a switch statement.
1270 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1272 <table border="0" bgcolor="#E0E0E0" width="100%">
1275 <pre class="PROGRAMLISTING">
1276 switch (hash_string(cmd))
1278 case hash_actions_file:
1288 ... anomaly code goes here ...
1289 continue; / break; / exit( 1 ); / etc ...
1291 } /* end switch (hash_string(cmd)) */
1297 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If
1298 you already have a default condition, you are obviously exempt
1299 from this point. Of note, most of the WIN32 code calls
1300 `DefWindowProc' after the switch statement. This API call
1301 *should* be included in a default statement.
1304 <span class="emphasis"><i class="EMPHASIS">Another
1305 Note:</i></span> This is not so much a readability issue as a
1306 robust programming issue. The "anomaly code goes here" may be no
1307 more than a print to the STDERR stream (as in load_config). Or it
1308 may really be an abort condition.
1311 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
1312 Programmer discretion is advised.
1317 <a name="S38">4.7.3. Try to avoid falling through cases in a
1318 switch statement.</a>
1321 <span class="emphasis"><i class=
1322 "EMPHASIS">Explanation:</i></span>
1325 In general, you will want to have a 'break' statement within each
1326 'case' of a switch statement. This allows for the code to be more
1327 readable and understandable, and furthermore can prevent unwanted
1328 surprises if someone else later gets creative and moves the code
1332 The language allows you to plan the fall through from one case
1333 statement to another simply by omitting the break statement
1334 within the case statement. This feature does have benefits, but
1335 should only be used in rare cases. In general, use a break
1336 statement for each case statement.
1339 If you choose to allow fall through, you should comment both the
1340 fact of the fall through and reason why you felt it was
1346 <a name="S40">4.7.4. Don't mix size_t and other types</a>
1349 <span class="emphasis"><i class=
1350 "EMPHASIS">Explanation:</i></span>
1353 The type of size_t varies across platforms. Do not make
1354 assumptions about whether it is signed or unsigned, or about how
1355 long it is. Do not compare a size_t against another variable of a
1356 different type (or even against a constant) without casting one
1362 <a name="S41">4.7.5. Declare each variable and struct on its own
1366 <span class="emphasis"><i class=
1367 "EMPHASIS">Explanation:</i></span>
1370 It can be tempting to declare a series of variables all on one
1374 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1376 <table border="0" bgcolor="#E0E0E0" width="100%">
1379 <pre class="PROGRAMLISTING">
1388 <span class="emphasis"><i class="EMPHASIS">Instead of:</i></span>
1394 <span class="emphasis"><i class=
1395 "EMPHASIS">Explanation:</i></span> - there is more room for
1396 comments on the individual variables - easier to add new
1397 variables without messing up the original ones - when searching
1398 on a variable to find its type, there is less clutter to
1399 "visually" eliminate
1402 <span class="emphasis"><i class="EMPHASIS">Exceptions:</i></span>
1403 when you want to declare a bunch of loop variables or other
1404 trivial variables; feel free to declare them on one line. You
1405 should, although, provide a good comment on their functions.
1408 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
1409 developer-discretion.
1414 <a name="S42">4.7.6. Use malloc/zalloc sparingly</a>
1417 <span class="emphasis"><i class=
1418 "EMPHASIS">Explanation:</i></span>
1421 Create a local struct (on the stack) if the variable will live
1422 and die within the context of one function call.
1425 Only "malloc" a struct (on the heap) if the variable's life will
1426 extend beyond the context of one function call.
1429 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1431 <table border="0" bgcolor="#E0E0E0" width="100%">
1434 <pre class="PROGRAMLISTING">
1435 If a function creates a struct and stores a pointer to it in a
1436 list, then it should definitely be allocated via `malloc'.
1444 <a name="S43">4.7.7. The Programmer Who Uses 'malloc' is
1445 Responsible for Ensuring 'free'</a>
1448 <span class="emphasis"><i class=
1449 "EMPHASIS">Explanation:</i></span>
1452 If you have to "malloc" an instance, you are responsible for
1453 insuring that the instance is `free'd, even if the deallocation
1454 event falls within some other programmer's code. You are also
1455 responsible for ensuring that deletion is timely (i.e. not too
1456 soon, not too late). This is known as "low-coupling" and is a
1457 "good thing (tm)". You may need to offer a free/unload/destructor
1458 type function to accommodate this.
1461 <span class="emphasis"><i class="EMPHASIS">Example:</i></span>
1463 <table border="0" bgcolor="#E0E0E0" width="100%">
1466 <pre class="PROGRAMLISTING">
1467 int load_re_filterfile(struct client_state *csp) { ... }
1468 static void unload_re_filterfile(void *f) { ... }
1474 <span class="emphasis"><i class="EMPHASIS">Exceptions:</i></span>
1477 The developer cannot be expected to provide `free'ing functions
1478 for C run-time library functions ... such as `strdup'.
1481 <span class="emphasis"><i class="EMPHASIS">Status:</i></span>
1482 developer-discretion. The "main" use of this standard is for
1483 allocating and freeing data structures (complex or nested).
1488 <a name="S44">4.7.8. Add loaders to the `file_list' structure and
1492 <span class="emphasis"><i class=
1493 "EMPHASIS">Explanation:</i></span>
1496 I have ordered all of the "blocker" file code to be in alpha
1497 order. It is easier to add/read new blockers when you expect a
1501 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> It
1502 may appear that the alpha order is broken in places by POPUP
1503 tests coming before PCRS tests. But since POPUPs can also be
1504 referred to as KILLPOPUPs, it is clear that it should come first.
1509 <a name="S45">4.7.9. "Uncertain" new code and/or changes to
1510 existing code, use XXX</a>
1513 <span class="emphasis"><i class=
1514 "EMPHASIS">Explanation:</i></span>
1517 If you have enough confidence in new code or confidence in your
1518 changes, but are not *quite* sure of the repercussions, add this:
1521 /* XXX: this code has a logic error on platform XYZ, * attempting
1522 to fix */ #ifdef PLATFORM ...changed code here... #endif
1528 /* XXX: I think the original author really meant this... */
1529 ...changed code here...
1535 /* XXX: new code that *may* break something else... */ ...new
1539 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If
1540 you make it clear that this may or may not be a "good thing
1541 (tm)", it will be easier to identify and include in the project
1542 (or conversely exclude from the project).
1548 <a name="S46">4.8. Addendum: Template for files and function
1552 <span class="emphasis"><i class="EMPHASIS">Example for file
1553 comments:</i></span>
1555 <table border="0" bgcolor="#E0E0E0" width="100%">
1558 <pre class="PROGRAMLISTING">
1559 const char FILENAME_rcs[] = "$I<!-- Break CVS Substitution -->d$";
1560 /*********************************************************************
1562 * File : $S<!-- Break CVS Substitution -->ource$
1564 * Purpose : (Fill me in with a good description!)
1566 * Copyright : Written by and Copyright (C) 2001-2009
1567 * the Privoxy team. https://www.privoxy.org/
1569 * This program is free software; you can redistribute it
1570 * and/or modify it under the terms of the GNU General
1571 * Public License as published by the Free Software
1572 * Foundation; either version 2 of the License, or (at
1573 * your option) any later version.
1575 * This program is distributed in the hope that it will
1576 * be useful, but WITHOUT ANY WARRANTY; without even the
1577 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1578 * PARTICULAR PURPOSE. See the GNU General Public
1579 * License for more details.
1581 * The GNU General Public License should be included with
1582 * this file. If not, you can view it at
1583 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
1584 * or write to the Free Software Foundation, Inc.,
1585 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
1588 *********************************************************************/
1593 ...necessary include files for us to do our work...
1595 const char FILENAME_h_rcs[] = FILENAME_H_VERSION;
1601 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> This
1602 declares the rcs variables that should be added to the
1603 "show-proxy-args" page. If this is a brand new creation by you, you
1604 are free to change the "Copyright" section to represent the rights
1605 you wish to maintain.
1608 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> The
1609 formfeed character that is present right after the comment flower
1610 box is handy for (X|GNU)Emacs users to skip the verbiage and get to
1611 the heart of the code (via `forward-page' and `backward-page').
1612 Please include it if you can.
1615 <span class="emphasis"><i class="EMPHASIS">Example for file header
1616 comments:</i></span>
1618 <table border="0" bgcolor="#E0E0E0" width="100%">
1621 <pre class="PROGRAMLISTING">
1624 #define FILENAME_H_VERSION "$I<!-- Break CVS Substitution -->d$"
1625 /*********************************************************************
1627 * File : $S<!-- Break CVS Substitution -->ource$
1629 * Purpose : (Fill me in with a good description!)
1631 * Copyright : Written by and Copyright (C) 2001-2009
1632 * the Privoxy team. https://www.privoxy.org/
1634 * This program is free software; you can redistribute it
1635 * and/or modify it under the terms of the GNU General
1636 * Public License as published by the Free Software
1637 * Foundation; either version 2 of the License, or (at
1638 * your option) any later version.
1640 * This program is distributed in the hope that it will
1641 * be useful, but WITHOUT ANY WARRANTY; without even the
1642 * implied warranty of MERCHANTABILITY or FITNESS FOR A
1643 * PARTICULAR PURPOSE. See the GNU General Public
1644 * License for more details.
1646 * The GNU General Public License should be included with
1647 * this file. If not, you can view it at
1648 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
1649 * or write to the Free Software Foundation, Inc.,
1650 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
1653 *********************************************************************/
1656 #include "project.h"
1662 ... function headers here ...
1665 /* Revision control strings from this header and associated .c file */
1666 extern const char FILENAME_rcs[];
1667 extern const char FILENAME_h_rcs[];
1674 #endif /* ndef _FILENAME_H */
1686 <span class="emphasis"><i class="EMPHASIS">Example for function
1687 comments:</i></span>
1689 <table border="0" bgcolor="#E0E0E0" width="100%">
1692 <pre class="PROGRAMLISTING">
1693 /*********************************************************************
1695 * Function : FUNCTION_NAME
1697 * Description : (Fill me in with a good description!)
1700 * 1 : param1 = pointer to an important thing
1701 * 2 : x = pointer to something else
1703 * Returns : 0 => Ok, everything else is an error.
1705 *********************************************************************/
1706 int FUNCTION_NAME(void *param1, const char *x)
1717 <span class="emphasis"><i class="EMPHASIS">Note:</i></span> If we
1718 all follow this practice, we should be able to parse our code to
1719 create a "self-documenting" web page.
1723 <div class="NAVFOOTER">
1724 <hr align="LEFT" width="100%">
1725 <table summary="Footer navigation table" width="100%" border="0"
1726 cellpadding="0" cellspacing="0">
1728 <td width="33%" align="left" valign="top">
1729 <a href="documentation.html" accesskey="P">Prev</a>
1731 <td width="34%" align="center" valign="top">
1732 <a href="index.html" accesskey="H">Home</a>
1734 <td width="33%" align="right" valign="top">
1735 <a href="testing.html" accesskey="N">Next</a>
1739 <td width="33%" align="left" valign="top">
1740 Documentation Guidelines
1742 <td width="34%" align="center" valign="top">
1745 <td width="33%" align="right" valign="top">