Difference between revisions of "MansOS Coding Standard"

From DiLab
Jump to: navigation, search
m
Line 1: Line 1:
== Indenting ==
+
Be advised to use the following coding style when writing or editing '''MansOS''' source code.
Indent - 4 space
+
 
Tab char - 8 space
+
== 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 [http://en.wikipedia.org/wiki/Camelcase 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 <code>#define</code> 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;
 +
 
 +
 
 +
=== 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
 +
 
 +
<code>
 +
  typedef
 +
  struct
 +
  {
 +
    int a;
 +
    int b;
 +
    int c;
 +
  } MyStruct_t
 +
</code>
 +
 
 +
 
 +
Enums example:
  
== if,for,while ==
 
 
<code>
 
<code>
   if (...) {
+
   typedef enum
    foo();
+
  {
   }
+
    ShapeRectangle,
 +
    ShapeCircle = 17,
 +
    ShapeLast              // Use "Last" as the last enum available, as needed
 +
   } ShapeType_e;
 
</code>
 
</code>
  
== variables ==
 
variable names and function names should be written in camelcase(http://en.wikipedia.org/wiki/Camelcase) - writeByte, not write_byte, wrt_byte or WrItEbYtE.
 
  
int writeByte;
 
void printByte(void *buf);
 
  
=== pointer variables ===
+
== Code control structures ==
with "_p" appended
+
 
 +
=== if, else, for, while, do ===
 +
 
 +
Always use braces after <code>if(), else, for(), while(), do</code>, 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.
 +
 
 +
<code>
 +
  if ( a == 17 ) b++;
 +
 
 +
  if (...) {
 +
      foo();
 +
  }
  
int writeByte;
+
  if (...)
int writeByte_p; <-- like this
+
  {
 +
      foo();
 +
      bar();
 +
  }
 +
</code>
  
  
=== constants and defines  ===
+
=== Spacing in expressions ===
in all uppercase
 
#define MAX_LENGTH 15
 
#define HEIGHT 7
 
  
=== enums, structs and all other types ===
+
Use space to separate tokens in expressions and braces everywhere in the code
variable names with "_t" appended
+
However, you may use no space between unary operation and operand, for example, i++;
  
typedef
+
  x = y + 16 - z++;
struct
 
{
 
int a;
 
int b;
 
int c;
 
} MyStruct_t
 
  
typedef enum {RECTANGLE, CIRCLE} shapeType_t;
+
  if( a == 17 ) {
 +
    b = foo( c, d );
 +
  }

Revision as of 18:30, 2 April 2008

Be advised to use the following coding style when writing or editing MansOS source code.

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;


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 
 {
   ShapeRectangle, 
   ShapeCircle = 17,
   ShapeLast              // Use "Last" as the last enum available, as needed
 } ShapeType_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 ( a == 17 ) b++;
 if (...) {
     foo();
 }
 if (...)
 {
     foo();
     bar();
 }


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( a == 17 ) {
   b = foo( c, d );
 }