X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=doc%2Fwebserver%2Fdeveloper-manual%2Fcoding.html;h=6898541fead2e43ea066cd8a4388ead2f20991a1;hp=28b00173c93e5d9693596ad8b38a06cb36458425;hb=3db7a58b2bbed7b6356b2a0600e93ec4f2846499;hpb=0212c18282eaa5f73843cbbec12c9137ea596e1c diff --git a/doc/webserver/developer-manual/coding.html b/doc/webserver/developer-manual/coding.html index 28b00173..6898541f 100644 --- a/doc/webserver/developer-manual/coding.html +++ b/doc/webserver/developer-manual/coding.html @@ -1,11 +1,11 @@ + Coding Guidelines

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.

4.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:

4.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.

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();

Explanation:

The default return for a function is an int. To avoid @@ -1259,17 +1439,23 @@ NAME="S28" 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.

4.6.4. Pass and Return by Const Reference

Explanation:

This allows a developer to define a const pointer and call @@ -1369,9 +1567,12 @@ NAME="S30" >4.6.5. Pass and Return by Value

Explanation:

Most structures cannot fit onto a normal stack entry (i.e. @@ -1391,9 +1592,12 @@ NAME="S31" >4.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.

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:

4.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:

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.

4.7.1. Turn on warnings

Explanation

Compiler warnings are meant to help you find bugs. You @@ -1625,9 +1865,12 @@ NAME="S37" 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.

Explanation:

In general, you will want to have a 'break' statement within @@ -1729,17 +1987,23 @@ NAME="S39" '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 @@ -1755,17 +2019,19 @@ NAME="S40" >4.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.

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.

4.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:

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).

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 @@ -1962,12 +2270,15 @@ CLASS="SECT3" >4.7.10. "Uncertain" new code and/or changes to - existing code, use FIXME

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 @@ -2007,9 +2321,12 @@ NAME="S46" comment blocks:

Example for file comments:

const char FILENAME_rcs[] = "$Id: developer-manual.sgml,v 1.40 2002/05/04 00:43:43 hal9 Exp $";
+>const char FILENAME_rcs[] = "$Id$";
 /*********************************************************************
  *
  * File        :  $Source$
  *
  * Purpose     :  (Fill me in with a good description!)
  *
- * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
- *                Privoxy team. http://www.privoxy.org/
- *
- *                Based on the Internet Junkbuster originally written
- *                by and Copyright (C) 1997 Anonymous Coders and
- *                Junkbusters Corporation.  http://www.junkbusters.com
+ * Copyright   :  Written by and Copyright (C) 2001-2009
+ *                the Privoxy team. http://www.privoxy.org/
  *
  *                This program is free software; you can redistribute it
  *                and/or modify it under the terms of the GNU General
@@ -2047,12 +2360,10 @@ 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.
- *
- * Revisions   :
- *    $Log$
+ *                http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *                or write to the Free Software Foundation, Inc., 
+ *                51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 ,
+ *                USA
  *
  *********************************************************************/
 
@@ -2066,26 +2377,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.40 2002/05/04 00:43:43 hal9 Exp $" +#define FILENAME_H_VERSION "$Id$" /********************************************************************* * * File : $Source$ * * Purpose : (Fill me in with a good description!) * - * Copyright : Written by and Copyright (C) 2001 the SourceForge - * Privoxy team. http://www.privoxy.org/ - * - * Based on the Internet Junkbuster originally written - * by and Copyright (C) 1997 Anonymous Coders and - * Junkbusters Corporation. http://www.junkbusters.com + * Copyright : Written by and Copyright (C) 2001-2009 + * the Privoxy team. http://www.privoxy.org/ * * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General @@ -2125,12 +2441,10 @@ 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. - * - * Revisions : - * $Log$ + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 , + * USA * *********************************************************************/ @@ -2164,9 +2478,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 \ No newline at end of file +>
HomeNext