MansOS Coding Standard
Be advised to use the following coding style when writing or editing MansOS source code.
Contents
General
- Write comments. Respect the reader!
- Keep the line length under 78 to help the viewer and those that want to print your code. Break the line in several as necessary.
Never ever exceed the line length of 120 symbols.
Code flow and indenting
Indent new code blocks by 4 spaces
Note that TAB character is always 8 spaces. However, set your editor to use spaces instead of the TAB character to avoid confusion when loading text files that may contain tab characters and was written using a different editor.
You may want to select TAB key to indent and Backspace key to un-indent in your text editor (SciTe, Emacs, ...)
Variable and function names
Variable names and function names shall start in lowercase and be written in CamelCase - writeByte, not write_byte, wrt_byte or WrItEbYtE.
int writeByte; void printByte(void *buf);
Pointer variables
Add trailing "_p" to show this is a pointer
int writeByte; int writeByte_p; <-- like this
Constants and defines
Use all-uppercase for #define
constants, with underscore between the words
#define MAX_LENGTH 15 #define HEIGHT 7
For constants use first letter in uppercase with the rest in camelcase Add trailing _c to show this is a constant.
const MaxBufferSize_c = 64;
When using #ifdef
or #if
constructs,
you should always use comments for #endif
.
The comment for #endif
should match the expression used in the corresponding
#if
or #ifdef
. The comment for #else
and #elif
should match the
inverse of the expression(s) used in the preceding #if
and/or #elif
statements.
#ifdef MSP430
/* msp430 stuff here */
#else /* !MSP430 */
/* non msp430 stuff here */
#endif /* MSP430 */
Enums, structs and all other types
Use the first letter in uppercase, the rest is CamelCase. Add trailing "_t" to show this is a type, or "_e" to show this is an enum
typedef struct
{
int a;
int b;
int c;
} MyStruct_t;
Enums example:
typedef enum
{
RECTANGLE,
CIRCLE = 17,
LAST // Use "Last" as the last enum available, as needed
} ShapeType_e;
Enumeration values all should be written in uppercase.
typedef enum { WHITE, BLACK } Color_e;
Code control structures
if, else, for, while, do
Always use braces after if(), else, for(), while(), do
, even when there is only one statement in the block.
There might be a few exceptions when if and the statement is on the same line and unmistakeably has one simple statement such as an assignment.
You may write the opening brace on the same or the next line. Use common sense. Generally to make a short if() with just one statement in the body I use it on the same line.
if ( 17 == a ) b++;
if (...) {
foo();
}
if (...)
{
foo();
bar();
}
When using comparisons in control structures, always try to use left-hand comparisons. Because these types of comparisons detects typo errors early. Typical errors arise in right-hand style when equality operator is required but assignment operator is used, in left-hand style these typos will result in compilation error and crash early.
daily usage of comparisons - right hand style
if ( a == 17 )
{
...
}
left-hand style
if ( 17 == a )
{
...
}
Spacing in expressions
Use space to separate tokens in expressions and braces everywhere in the code However, you may use no space between unary operation and operand, for example, i++;
x = y + 16 - z++;
if( 17 == a ) { b = foo( c, d ); }
Function and procedure heads
Start every function or procedure with a comment block describing it. The return type for functions may be on a separate line prior to the function name to make the function fit in a single line.
//----------------------------------------------------------------------------- // Prints formated text in a box with x,y,w,h parameters. //----------------------------------------------------------------------------- uint32_t printInBox(char* text, char *format, int16_t x, int16_t y, int16_t w, int16_t z) { ... }