X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=doc%2Fwebserver%2Fdeveloper-manual%2Fcoding.html;h=4dfb244a960922f4b1ea3e3a15fb666b50f8a3b2;hp=8fb12450275bc784184761f8dd06d5554cb16b7e;hb=473cfd051580edfa1e2a3f6beeb9a0d09a8253fd;hpb=56d03106907472899fa6e8933e81058744ce0fed diff --git a/doc/webserver/developer-manual/coding.html b/doc/webserver/developer-manual/coding.html index 8fb12450..4dfb244a 100644 --- a/doc/webserver/developer-manual/coding.html +++ b/doc/webserver/developer-manual/coding.html @@ -1,11 +1,11 @@ + Coding Guidelines
6.4.6. Make the last brace of a function stand +>4.4.6. Make the last brace of a function stand out

Example:

int function1( ... ) { ...code... - return( retCode ); + return( ret_code ); } /* -END- function1 */ @@ -1042,17 +1186,23 @@ int function2( ... ) >

Instead of:

int function1( ... ) { ...code... return( retCode ); } int +>int function1( ... ) { ...code... return( ret_code ); } int function2( ... ) { }

Note: Use 1 blank line before the closing brace and 2 lines afterward. This makes the end of function standout to the most casual viewer. Although function comments help @@ -1061,9 +1211,12 @@ CLASS="EMPHASIS" "do" loops, and long if {} statements too. After all whitespace is free!

Status: developer-discretion on the number of blank lines. Enforced is the end of function comments.

6.4.7. Use 3 character indentions4.4.7. Use 3 character indentions

Explanation:

If some use 8 character TABs and some use 3 character TABs, @@ -1086,9 +1242,12 @@ CLASS="EMPHASIS" only. If you like to use TABs, pass your code through a filter such as "expand -t3" before checking in your code.

Example:

6.5. Initializing4.5. Initializing
6.5.1. Initialize all variables4.5.1. Initialize all variables

Explanation:

Do not assume that the variables declared will not be used @@ -1150,9 +1312,12 @@ CLASS="EMPHASIS" the code. Remove the chance of accidentally using an unassigned variable.

Example:

short anShort = 0;
-float aFloat  = 0;
+>short a_short = 0;
+float a_float  = 0;
 struct *ptr = NULL;

Note: It is much easier to debug a SIGSEGV if the message says you are trying to access memory address 00000000 - and not 129FA012; or arrayPtr[20] causes a SIGSEV vs. - arrayPtr[0].

Status: developer-discretion if and only if the variable is assigned a value "shortly after" declaration.

6.6. Functions4.6. Functions
6.6.1. Name functions that return a boolean as a +>4.6.1. Name functions that return a boolean as a question.

Explanation:

Value should be phrased as a question that would logically be answered as a true or false statement

Example:

ShouldWeBlockThis();
-ContainsAnImage();
-IsWebPageBlank();
should_we_block_this(); +contains_an_image(); +is_web_page_blank();
6.6.2. Always specify a return type for a +>4.6.2. Always specify a return type for a function.

Explanation:

The default return for a function is an int. To avoid @@ -1255,21 +1435,27 @@ CLASS="SECT3" CLASS="SECT3" >6.6.3. Minimize function calls when iterating by +>4.6.3. Minimize function calls when iterating by using variables

Explanation:

It is easy to write the following code, and a clear argument can be made that the code is easy to understand:

Example:

for ( size_t cnt = 0; cnt < blockListLength(); cnt ++ )
+>for ( size_t cnt = 0; cnt < block_list_length(); cnt++ )
 {
    ....
 }

Note: Unfortunately, this makes a function call for each and every iteration. This increases the overhead in the program, because the compiler has to look up the function each time, call it, and return a value. Depending on what occurs in - the blockListLength() call, it might even be creating and + the block_list_length() call, it might even be creating and destroying structures with each iteration, even though in each case it is comparing "cnt" to the same value, over and over. - Remember too - even a call to blockListLength() is a function + Remember too - even a call to block_list_length() is a function call, with the same overhead.

Instead of using a function call during the iterations, assign the value to a variable, and evaluate using the variable.

Example:

size_t len = blockListLength();
+>size_t len = block_list_length();
 
-for ( size_t cnt = 0; cnt < len; cnt ++ )
+for ( size_t cnt = 0; cnt < len; cnt++ )
 {
    ....
 }

Exceptions: if the value of blockListLength() *may* - change or could *potentially* change, then you must code the +> if the value of block_list_length() + *may* change or could *potentially* change, then you must code the function call in the for/while loop.

6.6.4. Pass and Return by Const Reference4.6.4. Pass and Return by Const Reference

Explanation:

This allows a developer to define a const pointer and call @@ -1366,12 +1564,15 @@ CLASS="SECT3" CLASS="SECT3" >6.6.5. Pass and Return by Value4.6.5. Pass and Return by Value

Explanation:

Most structures cannot fit onto a normal stack entry (i.e. @@ -1388,12 +1589,15 @@ CLASS="SECT3" CLASS="SECT3" >6.6.6. Names of include files4.6.6. Names of include files

Explanation:

Your include statements should contain the file name without @@ -1403,9 +1607,12 @@ CLASS="EMPHASIS" partial path to distinguish their header files from system or other header files.

Example:

Exception:

Note: Please! do not add "-I." to the Makefile without a _very_ good reason. This duplicates the #include "file.h" behavior.

6.6.7. Provide multiple inclusion +>4.6.7. Provide multiple inclusion protection

Explanation:

Prevents compiler and linker errors resulting from @@ -1471,9 +1687,12 @@ CLASS="EMPHASIS" with your file name, with "." Changed to "_", and make it uppercase.

Example:

6.6.8. Use `extern "C"` when appropriate4.6.8. Use `extern "C"` when appropriate

Explanation:

If our headers are included from C++, they must declare our functions as `extern "C"`. This has no cost in C, but increases the potential re-usability of our code.

Example:

6.6.9. Where Possible, Use Forward Struct +>4.6.9. Where Possible, Use Forward Struct Declaration Instead of Includes

Explanation:

Useful in headers that include pointers to other struct's. Modifications to excess header files may cause needless compiles.

Example:

Note: If you declare "file_list xyz;" (without the pointer), then including the proper header file is necessary. If you only want to prototype a pointer, however, the header file is unnecessary.

Status: Use with discretion.

6.7. General Coding Practices4.7. General Coding Practices
6.7.1. Turn on warnings4.7.1. Turn on warnings

Explanation

Compiler warnings are meant to help you find bugs. You @@ -1621,13 +1861,16 @@ CLASS="SECT3" CLASS="SECT3" >6.7.2. Provide a default case for all switch +>4.7.2. Provide a default case for all switch statements

Explanation:

What you think is guaranteed is never really guaranteed. The @@ -1635,9 +1878,12 @@ CLASS="EMPHASIS" someday will be passed. So, to protect yourself from the unknown, always have a default step in a switch statement.

Example:

Note: If you already have a default condition, you are obviously exempt from this point. Of note, most of the WIN32 code calls `DefWindowProc' after the switch statement. This API call *should* be included in a default statement.

Another Note: This is not so much a readability issue as a robust programming issue. The "anomaly code goes here" may be no more than a print to the STDERR stream (as in - load_config). Or it may really be an ABEND condition.

Status: Programmer discretion is advised.

6.7.3. Try to avoid falling through cases in a +>4.7.3. Try to avoid falling through cases in a switch statement.

Explanation:

In general, you will want to have a 'break' statement within @@ -1725,21 +1983,27 @@ CLASS="SECT3" CLASS="SECT3" >6.7.4. Use 'long' or 'short' Instead of +>4.7.4. Use 'long' or 'short' Instead of 'int'

Explanation:

On 32-bit platforms, int usually has the range of long. On 16-bit platforms, int has the range of short.

Status: open-to-debate. In the case of most FSF projects (including X/GNU-Emacs), there are typedefs to int4, int8, int16, (or equivalence ... I forget the exact typedefs @@ -1752,20 +2016,22 @@ CLASS="SECT3" CLASS="SECT3" >6.7.5. Don't mix size_t and other types4.7.5. Don't mix size_t and other types

Explanation:

The type of size_t varies across platforms. Do not make assumptions about whether it is signed or unsigned, or about how long it is. Do not compare a size_t against another variable of a different type (or even against a constant) - without casting one of the values. Try to avoid using size_t if - you can.

6.7.6. Declare each variable and struct on its +>4.7.6. Declare each variable and struct on its own line.

Explanation:

It can be tempting to declare a series of variables all on one line. Don't.

Example:

Instead of:

long a, b, c;

Explanation: - there is more room for comments on the individual variables - easier to add new variables without messing up the original ones - when searching on a variable to find its type, there is less clutter to "visually" eliminate

Exceptions: when you want to declare a bunch of loop variables or other trivial variables; feel free to declare them - on 1 line. You should, although, provide a good comment on + on one line. You should, although, provide a good comment on their functions.

Status: developer-discretion.

6.7.7. Use malloc/zalloc sparingly4.7.7. Use malloc/zalloc sparingly

Explanation:

Create a local struct (on the stack) if the variable will @@ -1853,9 +2140,12 @@ CLASS="EMPHASIS" >Only "malloc" a struct (on the heap) if the variable's life will extend beyond the context of one function call.

Example:

6.7.8. The Programmer Who Uses 'malloc' is +>4.7.8. The Programmer Who Uses 'malloc' is Responsible for Ensuring 'free'

Explanation:

If you have to "malloc" an instance, you are responsible for @@ -1892,11 +2185,14 @@ CLASS="EMPHASIS" responsible for ensuring that deletion is timely (i.e. not too soon, not too late). This is known as "low-coupling" and is a "good thing (tm)". You may need to offer a - free/unload/destuctor type function to accommodate this.

Example:

Exceptions:

The developer cannot be expected to provide `free'ing functions for C run-time library functions ... such as `strdup'.

Status: developer-discretion. The "main" use of this standard is for allocating and freeing data structures (complex or nested).

6.7.9. Add loaders to the `file_list' structure +>4.7.9. Add loaders to the `file_list' structure and in order

Explanation:

I have ordered all of the "blocker" file code to be in alpha order. It is easier to add/read new blockers when you expect a certain order.

Note: It may appear that the alpha order is broken in places by POPUP tests coming before PCRS tests. But since POPUPs can also be referred to as KILLPOPUPs, it is clear that @@ -1961,13 +2269,16 @@ CLASS="SECT3" CLASS="SECT3" >6.7.10. "Uncertain" new code and/or changes to - existing code, use FIXME4.7.10. "Uncertain" new code and/or changes to + existing code, use FIXME or XXX

Explanation:

If you have enough confidence in new code or confidence in @@ -1988,9 +2299,12 @@ CLASS="EMPHASIS" >/* FIXME: new code that *may* break something else... */ ...new code here...

Note: If you make it clear that this may or may not be a "good thing (tm)", it will be easier to identify and include in the project (or conversely exclude from the @@ -2003,13 +2317,16 @@ CLASS="SECT2" CLASS="SECT2" >6.8. Addendum: Template for files and function +>4.8. Addendum: Template for files and function comment blocks:

Example for file comments:

const char FILENAME_rcs[] = "$Id: developer-manual.sgml,v 1.35 2002/04/17 15:16:15 oes Exp $";
+>const char FILENAME_rcs[] = "$Id: developer-manual.sgml,v 2.20 2008/06/14 13:21:24 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source$
  *
  * Purpose     :  (Fill me in with a good description!)
  *
- * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
+ * Copyright   :  Written by and Copyright (C) 2001-2007 the SourceForge
  *                Privoxy team. http://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
@@ -2048,8 +2365,9 @@ CLASS="PROGRAMLISTING"
  *                The GNU General Public License should be included with
  *                this file.  If not, you can view it at
  *                http://www.gnu.org/copyleft/gpl.html
- *                or write to the Free Software Foundation, Inc., 59
- *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *                or write to the Free Software Foundation, Inc., 
+ *                51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
+ *                USA
  *
  * Revisions   :
  *    $Log$
@@ -2066,26 +2384,35 @@ const char FILENAME_h_rcs[] = FILENAME_H_VERSION;

Note: This declares the rcs variables that should be added to the "show-proxy-args" page. If this is a brand new creation by you, you are free to change the "Copyright" section to represent the rights you wish to maintain.

Note: The formfeed character that is present right after the comment flower box is handy for (X|GNU)Emacs users to skip the verbiage and get to the heart of the code (via `forward-page' and `backward-page'). Please include it if you can.

Example for file header comments:

#ifndef _FILENAME_H #define _FILENAME_H -#define FILENAME_H_VERSION "$Id: developer-manual.sgml,v 1.35 2002/04/17 15:16:15 oes Exp $" +#define FILENAME_H_VERSION "$Id: developer-manual.sgml,v 2.20 2008/06/14 13:21:24 fabiankeil Exp $" /********************************************************************* * * File : $Source$ * * Purpose : (Fill me in with a good description!) * - * Copyright : Written by and Copyright (C) 2001 the SourceForge + * Copyright : Written by and Copyright (C) 2001-2007 the SourceForge * Privoxy team. http://www.privoxy.org/ * * Based on the Internet Junkbuster originally written @@ -2126,8 +2453,9 @@ CLASS="PROGRAMLISTING" * The GNU General Public License should be included with * this file. If not, you can view it at * http://www.gnu.org/copyleft/gpl.html - * or write to the Free Software Foundation, Inc., 59 - * Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * or write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 , + * USA * * Revisions : * $Log$ @@ -2164,9 +2492,12 @@ extern const char FILENAME_h_rcs[]; >

Example for function comments:

Note: If we all follow this practice, we should be able to parse our code to create a "self-documenting" web page.


Prev
HomeNext