httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Richards <p...@originat.demon.co.uk>
Subject Re: Style guide
Date Mon, 01 Jul 1996 22:54:00 GMT
In reply to Ben Laurie who said
> 
> As usual, please check that my records reflect your opinion, and again, again,
> please vote on 3a1 or 3ab.

3a1, 13a, 14a

After thinking about 13 for a while I think 13a makes sense. It makes
it clear from an immediate glance that this is a test for a NULL pointer
rather than checking that a variable or function return is zero. As I
argued for enum types, it saves having to look elsewhere in the source
to see what it is we're dealing with i.e a pointer or just a variable and
for enum whether its a enum type or a variable.

There's a whole load of things from the doc that were never disagreed
with and are now not being considered, I don't necessarily agree with 
all the examples below myself and have commented on them in
separate posts.

#include <sys/types.h>      /* Non-local includes in brackets. */
#include "pathnames.h"      /* Local includes in double quotes. */

/* 
 * Kernel include files come first; normally, you'll need <sys/types.h>
 * OR <sys/param.h>, but not both!  <sys/types.h> includes <sys/cdefs.h>,
 * and it's okay to depend on that.
 */
#include <sys/types.h>      /* Non-local includes in brackets. */
 
/* If it's a network program, put the network include files next. */
#include <net/if.h> 
#include <net/if_dl.h>
#include <net/route.h>
#include <netinet/in.h>
#include <protocols/rwhod.h>
    
/*
 * Then there's a blank line, followed by the /usr include files.
 * The /usr include files should be sorted!
 */ 
#include <stdio.h>

/* Then, there's a blank line, and the user include files. */
#include "pathnames.h"      /* Local includes in double quotes. */

/*
 * Macros are capitalized, parenthesized, and should avoid side-effects.
 * If they are an inline expansion of a function, the function is defined
 * all in lowercase, the macro has the same name all in uppercase. If the
 * macro needs more than a single line, use braces.  Right-justify the
 * backslashes, it makes it easier to read.
 */ 
#define MACRO(x, y) {                           \
    variable = (x) + (y);                       \
    (y) += 2;                           \
}    

/*
 * Major structures should be declared at the top of the file in which they
 * are used, or in separate header files, if they are used in multiple
 * source files.  Use of the structures should be by separate declarations
 * and should be "extern" if they are declared in a header file.
 */
struct foo {
    struct  foo *next;  /* List of active foo */
    struct  mumble amumble; /* Comment for mumble */
    int bar;
};
struct foo *foohead;        /* Head of global foo list */

/* Make the structure name match the typedef. */
typedef struct _bar {
    int level;
} BAR;

/*
 * All major routines should have a comment briefly describing what
 * they do.  The comment before the "main" routine should describe
 * what the program does.
 */

    /*
     * For consistency, getopt should be used to parse options.  Options
     * should be sorted in the getopt call and the switch statement, unless
     * parts of the switch cascade.  Elements in a switch statement that
     * cascade should have a FALLTHROUGH comment.  Numerical arguments
     * should be checked for accuracy.  Code that cannot be reached should
     * have a NOTREACHED comment.
     */

    /*
     * Space after keywords (while, for, return, switch).  No braces are
     * used for control statements with zero or only a single statement.
     *
     * Forever loops are done with for's, not while's.
     */

    /*
     * Parts of a for loop may be left empty.  Don't put declarations
     * inside blocks unless the routine is unusually complicated.
     */

    /* Second level indents are four spaces. */
    while (cnt < 20)
        z = a + really + long + statment + that + needs + two lines +
            gets + indented + four + spaces + on + the + second +
            and + subsequent + lines.

    exit(0);    /* Avoid obvious comments such as "Exit 0 on success." */
    return (eight);


	/*
     * Casts and sizeof's are not followed by a space.  NULL is any
     * pointer type, and doesn't need to be cast, so use NULL instead
     * of (struct foo *)0 or (struct foo *)NULL.  Also, test pointers
     * against NULL, i.e. use:
	 */

/* Variable numbers of arguments should look like this. */
#if __STDC__
#include <stdarg.h> 
#else   
#include <varargs.h> 
#endif

void
#if __STDC__
vaf(const char *fmt, ...)
#else
vaf(fmt, va_alist)
    char *fmt;
    va_dcl
#endif
{
    va_list ap;
#if __STDC__
    va_start(ap, fmt);
#else
    va_start(ap);
#endif
    STUFF;

    va_end(ap);     /* No return needed for void functions. */
}

static void
usage()
{   /* Insert an empty line if the function has no local variables. */






-- 
  Paul Richards, Originative Solutions Ltd.
  Internet: paul@netcraft.co.uk, http://www.netcraft.co.uk
  Phone: 0370 462071 (Mobile), +44 1225 447500 (work)

Mime
View raw message