Code Conventions
General Issues
- Avoid using code formatting (beautifier) tools
- Use spaces and never tab characters
- Indent code by 4 spaces (editors offer to set the TAB key to 4 spaces)
- Make use of spaces and white lines to separate code logically
- Comment all code exhaustively
CYBOL (XML)
- Write files that are valid according to the CYBOL DTD
CYBOI (C)
- Document each procedure (functionality, parameters) in javadoc style
- Use only procedures, no functions -- in other words: no return values besides void
- Hand over all parameters as void pointer being a reference to their value
- Make unchangeable parameters constant
- Only use arrays together with their size and element count;
never use null-terminated strings
- Put the opening brackets to the end of a line, not on an extra line
- Check pointers for null before accessing them
- Define all standard strings such as log messages as constant character array
- Use only if-else conditions, never case statements
- Use only while loops, never do-while or for loops
- Use only endless loops and place the loop condition inside the loop
- Example:
/**
* This is an example procedure.
* It does nothing.
*
* @param p0 the first parameter is an integer
* @param p1 the second parameter is a character
* @param p2 the third parameter is a pointer, for example a char pointer (string)
*/
void example_procedure(void* p0, void* p1, const void* p2) {
if (p2 != NULL_POINTER) {
// Cast void pointer parameter to its actual type.
char** s = (char**) p2;
// Use the string by dereferencing s.
// This value was handed over as constant parameter and cannot be changed.
fprintf(stdout, "The string value is: %s\n", *s);
if (p1 != NULL_POINTER) {
// Cast void pointer parameter to its actual type.
char* c = (char*) p1;
// Use the character by dereferencing c.
*c = 32;
if (p0 != NULL_POINTER) {
// Cast void pointer parameter to its actual type.
int* i = (int*) p0;
// Use the integer by dereferencing i.
*i = *i + 10;
// An example loop.
int j = 0;
while (1) {
if (j >= 10) {
break;
}
j++;
}
} else {
log_message((void*) &ERROR_LOG_LEVEL, (void*) &P0_IS_NULL_MESSAGE, (void*) &P0_IS_NULL_MESSAGE_COUNT);
}
} else {
log_message((void*) &ERROR_LOG_LEVEL, (void*) &P1_IS_NULL_MESSAGE, (void*) &P1_IS_NULL_MESSAGE_COUNT);
}
} else {
log_message((void*) &ERROR_LOG_LEVEL, (void*) &P2_IS_NULL_MESSAGE, (void*) &P2_IS_NULL_MESSAGE_COUNT);
}
}
|