Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Overview

This document covers coding guidelines to use when writing C code.  It is very much a WORK IN PROGRESS.

Copyright notices

All new source files must begin with the same copyright notice as recommended in the Coding Style and Guidelines

 

The ForgeRock C coding style

Summary

The ForgeRock C code style rules are based on the Java conventions:

  • source files MUST contain a valid copyright notice (see above)
  • source files MUST NOT contain tab characters - code MUST be formatted using 4 character indents
  • source files MUST NOT contain lines longer than 120 characters
  • curly braces MUST ALWAYS be used with if, while, for and do/while and the opening brace must appear on the SAME LINE as the keyword
  • checking against NULL should ALWAYS be explicit rather than implied
  • never declare more than one variable at a time
  • when declaring pointers, the * should be moved towards the type rather than the variable
  • macros must ALWAYS be defined in UPPERCASE
  • always include system header files before local header files
  • leave at least one blank line between declarations and code within a function, unless the function is particularly short
  • document each and every function, giving its purpose, parameters and return value (for non void functions)
  • ALWAYS put spaces around arithmetic operators (+, -, *, /, =, <, <=, >, >=)
  • NEVER put space around ->, * (pointer dereference), sizeof or a cast operation
  • ALWAYS separate arguments with a comma AND A SPACE

Details

Consider the following example for a few of the points listed above:

Bad
int my_func(int *p,int c) {
	unsigned int *index = p + c - 1, entry_index=0;
	if (index) *index=entry_index;
}

should be written as:

Good
/******************************************************************************************
 * The function my_func divides by the number you first thought of.
 * Parameters:
 *    p is a pointer to an area of memory containing a series of unsigned integers
 *    c is the number of unsigned integers in the block pointed to by "p"
 * Return:
 *    If successful, the function returns 1, if unsuccessful, the function returns 0
 ******************************************************************************************/
int my_func(int* p, int c) {
	unsigned int*  index = p + c - 1;
	unsigned int   entry_index = 0;

	if (index != NULL) {
    	*index = entry_index;
	}
}

 

 

 

  • No labels