ifae/guidelines/c

C Coding Guidelines

Notes:

Prefer C17 version when possible. Use -Wall compiler option when possible

Naming

  1. Struct, enums and global functions name must be PascalCase
  2. Static functions local to a translation unit should be camelCase
  3. Non static functions part of a translation unit should be prefixed in snake_case (eg: with the name of the file) followed with the function name in camelCase. For example “dma_enqueueTask”
  4. Variables identifiers should be snake_case
  5. Constants variables must start with “k_” and use snake_case (like k_buffer_size)
  6. Global non-constant variables must start with “g_” and use snake case (like g_buffer_size)
  7. Macros, if used, must use SNAKE_CASE (all uppercase)
  8. File names .h and .c should be snake_case
  9. Both header and implementation file names should be the same (like cmd.h and cmd.c)

Variables

  1. Use Static keyword when a variable or function is to be visible only within a single source file.
    • It is recommended to keep variables within a source file static if they are not meant to be visible by other files.
    • However, it is not recommended to define a static variable within a function, this can make your code less readable and more susceptible to bugs.
  2. When a variable is used by interrupt routine or is a memory-mapped pointer, it must be declared as volatile.
    • Tick counters are incremented in an Interrupt routine, thus volatile
    • Memory-mapped pointers values are volatile since a register can be changed by hardware.
  3. Typedef your application variable whenever possible
    • It is generally recommended to typedef all variables in your application to a user typedef.
    • Using the raw c types in application is not recommended as changing the type later is way more complex than using typedef.
  4. For constant pointer parameter, use the const keyword to operate on the pointer only, as follows:
void updateWifiNameTo4G (WifiName_t* const pWifi)

Misc code

  1. Do not put a default case when an Enum is used as the switch case variable
    • This allows compiler to catch missing or unhandled cases, and throw warning
  2. If multiple cases have the same handling, use fall through.
    • Fall through is enabled by writing cases in series with just a single break at the end and a common body for all.
  3. Avoid using magic numbers for loop counter
    • Use define constants instead.
    • When the constant is changed, all instances will be updated unlike magic numbers where you need to change them individually.

Files

  1. Both files (.h and .c) should be in the same folder whenever possible

Includes

  1. First include STL headers. Then third party libraries and finally header files of the cpp object.

  2. Includes in a file should be sorted alphabetically

  3. Avoid non needed includes, it makes modules more dependant and compilation slower.

  4. #pragma once directive: To avoid multiple including of a file use #pragma once instead of the old fashion #ifdef #endif