NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

main-standards(n) 4.99.30 manual "NaviServer Manual"

Name

main-standards - Engineering Standards Manual

Table Of Contents

Description

Contributed by George Nachman.

Based on the Tcl/Tk Engineering Manual by John K. Ousterhout

Introduction

This manual is based on the Tcl/Tk Engineering Manual by John K. Ousterhout that is available at http://www.activestate.com/ActiveState. Most of this document is a subset of what his manual specifies, with the goal of being more practical and up-to-date than the original. For example, it is assumed that only an ANSI compiler will be used, whereas the Ousterhout's manual describes conventions that will also work with non-ANSI compilers. The Tcl/Tk Engineering Manual is recommended reading, particularly with respect to the section on code documentation, which is not reproduced here.

Overall Structure

Each module will be named nsxxx, where xxx is a short name that describes the module. Each module will have its own directory, and contain at least the following files:

If a module exports symbols, then a header file by the name of nsxxx.h should also be in that directory.

Makefile Structure

Use this as a template for module makefiles:

 #
 # nsexample --
 #
 #      Example NaviServer module Makefile.
 #
 
 #
 # NaviServer's location
 #
 #  Since your module probably doesn't live inside the "naviserver"
 #  directory, you can tell make where to find NaviServer.
 #
 #NAVISERVER   =  /usr/local/ns
 NAVISERVER    =  ../naviserver
 
 #
 # File name of binary loadable module
 #
 MOD           =  nsexample.so
 
 #
 # Object files for modules (required with MOD)
 #
 MODOBJS       =  nsexample.o
 
 #     MODNAME     Unique name of module
 #     TCL         List of module shared Tcl files
 #     LIBNM       Root name of dynamic library (without lib prefix or extension)
 #     LIBOBJS     List of library object files (required with LIBNM)
 #     LIBHDRS     List of public header files to install (required with module LIBNM)
 #     PGM         Name of executable program
 #     PGMOBJS     List of program object files (required with PGM)
 #     HDRS        Header files which objects depend on
 #     MODLIBS     Extra module link libraries
 #     LIBLIBS     Extra dynamic library link libraries
 #     PGMLIBS     Extra program link libraries
 #     INSTALL     Extra install directive(s)
 #     CLEAN       Extra clean directive(s)
 #     DESTDIR     Root directory for install
 #
 #     At least one of PGM, MOD, or LIBNM must be defined.  For backwards compatibility,
 #     MODOBJS will default to OBJS and MODINST can be used for module install.
 
 
 include  $(NSHOME)/include/Makefile.module

Header file structure

Use this as a template for all header files:

 /*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://mozilla.org.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU General Public License (the "GPL"), in which case the
 * provisions of GPL are applicable instead of those above.  If you wish
 * to allow use of your version of this file only under the terms of the
 * GPL and not to allow others to use your version of this file under the
 * License, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the GPL.
 * If you do not delete the provisions above, a recipient may use your
 * version of this file under either the License or the GPL.
 */
 
 /*
 * file.h --
 *
 * 	Description of file.
 *
 */
 
 #ifndef FILE_H
 #define FILE_H
 
 
 /*
 * The following constants...
 */
 
 #define ...
 
 /*
 * The following structure defines...
 */
 
 typedef struct ...
 
 /*
 * Exported functions
 */
 
 extern ...
 
 #endif /* FILE_H */

Header files never contain static symbols.

Code File Structure

Each source code file should contain a related set of procedures. The most manageable size for files is usually in the range of 500-2000 lines. Closely related functions should be placed as close together as possible.

API functions (ns_*) come first; exported functions that are not API calls (Ns*) come after those; static functions come last. Logical groups of functions can be separated like this:

 /*
 *==========================================================================
 * This is where we torque the wingnut on the widget.
 *==========================================================================
 */

Use this as a template for all code files:

 /*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://mozilla.org/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU General Public License (the "GPL"), in which case the
 * provisions of GPL are applicable instead of those above.  If you wish
 * to allow use of your version of this file only under the terms of the
 * GPL and not to allow others to use your version of this file under the
 * License, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the GPL.
 * If you do not delete the provisions above, a recipient may use your
 * version of this file under either the License or the GPL.
 */
 
 /*
 * file.c --
 *
 * 	Description of file.
 */
 
 #include "file.h"
 
 /*
 * The following constants...
 */
 
 #define ...
 
 /*
 * The following structure defines...
 */
 
 typedef struct ...
 
 /*
 * Local functions defined in this file
 */
 
 static int FunctionName(int x);
 
 /*
 * Static variables defined in this file
 */
 
 static int nsNumFooBar;     /* Number of foobars allocated */
 ...
 
 /*
 *==========================================================================
 * API functions
 *==========================================================================
 */
 
 (API function definitions go here)
 
 /*
 *==========================================================================
 * Exported functions
 *==========================================================================
 */
 
 (Exported, non-api functions go here)
 
 /*
 *==========================================================================
 * Static functions
 *==========================================================================
 */
 
 (Static functions go here)

Source files should never contain extern statements; those belong in header files (called file.h in the above template).

Function definitions

Function definitions should follow this template:

   /*
    *----------------------------------------------------------------------
    * FunctionName --
    *
    * 	Description of function.
    *
    * Results:
    *	This function returns ...
    *
    * Side effects:
    *	A new thread will be created.
    *
    *----------------------------------------------------------------------
    */
 
   static int
   FunctionName(int x)
   {
 	...
   }

All functions definitions begin on a new page (which is to say they should be preceded by a control-L character). All functions must be typed: use void if the function returns no result. The second line gives the function's name and argument list. If there are many arguments, they should spill onto additional lines as such:

   static int
   FunctionThatTakesLotsOfParameters(int a, int b, int c, int d, int e,
                                     int f, int g)
   {
 	...
   }

The same rule applies to prototypes.

Parameter order

Function parameters may be divided into three categories. In parameters only pass information into the function (either directly or by pointing to information that the function reads). Out parameters point to things in the caller's memory that the function modifies. In-out parameters do both. Below is a set of rules for deciding on the order of parameters to a function:

  1. Parameters should normally appear in the order in, in/out, out, except where overridden by the rules below.

  2. If there is a group of functions, all of which operate on structures of a particular type--such as a hash table--the token for the structure should be the first argument to each of the functions.

  3. When two parameters are the address of a callback function and a context value (or ClientData value) to pass to that function, the function address should appear in the argument list immediately before the context/ClientData.

  4. If a callback function takes a context/ClientData argument (and all callbacks should), the context/ClientData argument should be the first argument to the procedure. Typically the context/ClientData is a pointer to the structure managed by the callback, so this is really the same as rule 2.

  5. In/out parameters should not be used without a very good reason.

Naming Conventions

  1. Be consistent. Use the same name to refer to the same thing everywhere. For example, in the Tcl implementation the name interp is used consistently for pointers to the user-visible Tcl_Interp structure.

  2. Make sure a function name describes what the function actually does. Will the name make sense out of context?

  3. Sometimes it is appropriate to use one-letter variables, such as a for-loop control variable called i. For anything more complex, a short descriptive name should be used.

Basic Syntax Rules

  1. Variable names always start with a lowercase letter. Function and type names always start with an uppercase letter.

  2. In multi-word names, the first letter of each word after the first is in uppercase (usually referred to as camel case). Example:

       int nsThreadTimeout;
    
  3. Any name that refers to a pointer ends in Ptr. If it is a pointer to a pointer, then it ends in PtrPtr. Exceptions to this rule include opaque handles for structures (such as ns_ModLogHandle) and char * variables that refer to null-terminated strings. Also, static buffers should not have the Ptr suffix, as in this case:

       char buf[32];
    

    It is recommended that the names of variables of type Tcl_Objs end with Obj.

  4. Variables that hold address of procedures should have names ending in Proc, as should typedefs for such variables.

       typedef int (Ns_ModuleInitProc) (const char *server, const char *module);
    
  5. #define macros and constants should be in all uppercase. Underscores separate multiple words (as in NS_TRUE).

  6. Tcl commands are always in all-lowercase.

Function names contain meaning

Public exported functions that are part of the API should begin with Ns_, as in:

   extern int Ns_ConnPort(const Ns_Conn *conn)

Functions that are to be used by other files in a module, but are not meant to be called from outside the module, should begin with Ns, as in:

   extern void NsDbInit(void);

Global variables that do not have static scope begin with ns, as in:

   ns_Cache *nsAdpCachePtr = NULL;

C implementations of Tcl commands should be ending with Cmd, as in:

   int NsTclSetObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv);

Low-level coding conventions

If you use Emacs, the following lisp (which you can put in your .emacs file) will make C-mode do much of the formatting for you (its default behavior is almost correct--this just makes indents be four spaces):

   (add-hook 'c-mode-hook
   	(function (lambda ()
   		(setq c-basic-offset 4)
   		(setq c-indent-level 4))))

Try to follow these rules:

Idioms, Canonical Forms, and Recommended Practices

The following conventions are frequently used in NaviServer. They are the recommended way of implementing a behavior.