Difference between revisions of "Coding Standards"
From Embedded Systems Learning Academy
(No difference)
|
Latest revision as of 14:38, 6 May 2012
- Do not use magic numbers.
- Instead of:
sendSpi(12)
- Use:
int readOpCode=12; sendSpi(readOpCode);
- Leave it to the compiler to optimize
- Make case statements descriptive; don't use hex value
- For instance, create an enumeration:
typedef enum {moveForward = 0x80}myStateType;
- Instead of
case 0x80:
, usecase moveForward:
- For instance, create an enumeration:
- Instead of:
- Do not have a large code section without sub-routines
- Anything longer than viewable screen(monitor) length should be split in functions
- Use meaningful variable names
- Do not use i,j,k unless they are for loop counters
- Variable names' Standards
- Global Variables should be ALL_CAPS (Use underscore to separate a word)
- Class variables should be preceded with an m: ie: mMyClassVar
- Pointers should be preceded with a p: ie: pDataBuffer
- C Private variables (only declared in *.c files) should be also preceded with an "m"
- Refactor ALL duplicate code
- Any code that needs to be changed in more than 1 place needs to be refactored.
- This is wrong:
-
char arr[128];
for(int i=0; i < 128; i++)
{ arr[i] = getchar(); } - Here, if you change value of 128, you will need to change it at more than 1 place.
-
- This is okay:
-
const int size = 128;
char arr[size];
for(int i=0; i < size; i++)
{ arr[i] = getchar(); }
-
Other Thoughts
- Test each thought before moving on
- Guarantee functionality before moving on to next task
- Write Unit Tests for each sub-routine.