Coding Standards

From Embedded Systems Learning Academy
Jump to: navigation, search
  1. 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: , use case moveForward:
  2. Do not have a large code section without sub-routines
    Anything longer than viewable screen(monitor) length should be split in functions
  3. Use meaningful variable names
    Do not use i,j,k unless they are for loop counters
  4. 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"
  5. 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

  1. Test each thought before moving on
    Guarantee functionality before moving on to next task
    Write Unit Tests for each sub-routine.