FlexSEA Wiki

A WEARABLE ROBOTICS TOOLKIT

User Tools

Site Tools


styleguide

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
styleguide [2019/09/11 19:07]
jfduval
styleguide [2020/07/10 16:47] (current)
jfduval
Line 1: Line 1:
 ====== Dephy C/C++ Programming Style Guide ====== ====== Dephy C/C++ Programming Style Guide ======
  
-__**General:**__+Programming in Python? [[styleguidepython|Dephy Python Programming Style Guide]].  
 + 
 +Looking for a way to review your code? A checklist for this style guide lives [[internal:code_checklist|here.]] 
 + 
 +===== General ===== 
  
   * Use <color #​22b14c>​tabulations</​color>,​ not <color #​ed1c24>​spaces</​color>​. If you have to specify the tab size, use 4.   * Use <color #​22b14c>​tabulations</​color>,​ not <color #​ed1c24>​spaces</​color>​. If you have to specify the tab size, use 4.
   * All files need to end with an empty line.   * All files need to end with an empty line.
-  * Do not use ''/​* */'' ​for comments that are on a single line, use <​nowiki>​ // </​nowiki>​ +  * Do not use ''/​* */'' ​unless absolutely necessary, such as commenting out large chunks of code. Instead, use <​nowiki>//</​nowiki>​ 
-  * As a general rule, make sure that your code looks clean and similar to the existing code (when existing code is provided)+  * As a general rule, make sure that your code looks clean and similar to the existing code (when existing code is provided). More examples below.
   * In your comments, start sentences with capital letters. Do not put a space between the <​nowiki>//</​nowiki>​ and the text.   * In your comments, start sentences with capital letters. Do not put a space between the <​nowiki>//</​nowiki>​ and the text.
     * Good: <color #​22b14c><​nowiki>//​That'​s a good comment</​nowiki></​color>​     * Good: <color #​22b14c><​nowiki>//​That'​s a good comment</​nowiki></​color>​
Line 14: Line 19:
   * When you use a preprocessor statement such as <​nowiki>#​ifdef DEF_X</​nowiki>,​ always add a closing comment such as <​nowiki>#​endif //​DEF_X</​nowiki>​   * When you use a preprocessor statement such as <​nowiki>#​ifdef DEF_X</​nowiki>,​ always add a closing comment such as <​nowiki>#​endif //​DEF_X</​nowiki>​
   * Use the keyword '​ToDo'​ to flag stuff that should be fixed.   * Use the keyword '​ToDo'​ to flag stuff that should be fixed.
-  * Variable ​names should start with a lower case. Use upper case to separate words. ​+  * Function and variable ​names should start with a lower case. Use upper case to separate words. ​
     * Good: <color #​22b14c>​int myNewVariable = 0;</​color>​     * Good: <color #​22b14c>​int myNewVariable = 0;</​color>​
     * Bad: <color #​ed1c24>​int MyNewVariable = 0;</​color>​     * Bad: <color #​ed1c24>​int MyNewVariable = 0;</​color>​
Line 27: Line 32:
   * When in doubt, add more parentheses than needed. It usually makes the code easier to read, and it removes doubts about operator priority.   * When in doubt, add more parentheses than needed. It usually makes the code easier to read, and it removes doubts about operator priority.
  
-__**Functions - Brackets:**__+===== Functions - Brackets ​=====
  
 <color #​22b14c>​Good:</​color>​ <color #​22b14c>​Good:</​color>​
Line 33: Line 38:
 <​code>​ <​code>​
 //​Description of the function //​Description of the function
-void my_function(void)+void myFunction(void)
 { {
      //My Code      //My Code
Line 41: Line 46:
 <color #​ed1c24>​Bad:</​color>​ <color #​ed1c24>​Bad:</​color>​
  
-<​code>​void ​my_function(void){+<​code>​void ​myFunction(void){
 //My Code //My Code
 }</​code>​ }</​code>​
  
-__**Functions - Arguments:**__+===== Functions - Arguments ​=====
  
-Always use the keyword '​void'​ when you do not have an argument.+C ONLY: Always use the keyword '​void'​ when you do not have an argument.
  
 <color #​22b14c>​Good:</​color>​ <color #​22b14c>​Good:</​color>​
  
-<​code>​void ​my_fct(void)</​code>​+<​code>​void ​myFct(void)</​code>​
  
 <color #​ed1c24>​Bad:</​color>​ <color #​ed1c24>​Bad:</​color>​
  
-<​code>​void ​my_fct()</​code>​+<​code>​void ​myFct()</​code>​
  
-__**Loops and Conditional statements - Brackets:**__+===== Loops and Conditional statements - Brackets ​===== 
  
 Always use brackets, even if you have a single line below the statement. Do not put a space before the parentheses. Example: Always use brackets, even if you have a single line below the statement. Do not put a space before the parentheses. Example:
Line 77: Line 82:
      ​value3 = 1;</​code>​      ​value3 = 1;</​code>​
  
-__**Conditional statements - Spaces:**__+===== Conditional statements - Spaces ​=====
  
 Do not include a space between if and ().  Do not include a space between if and (). 
Line 89: Line 94:
 <​code>​if (value1 == value2)</​code>​ <​code>​if (value1 == value2)</​code>​
  
-__**Operators and spaces**__+===== Operators and spaces ​=====
  
 <color #​22b14c>​Good:</​color>​ <color #​22b14c>​Good:</​color>​
Line 98: Line 103:
  
 <​code>​myVar = (i*20)/​(a+1)</​code>​ <​code>​myVar = (i*20)/​(a+1)</​code>​
 +
 +===== File Organization =====
 +
 +The idea is not to match the example below to the letter, but to follow the general structure and style. This makes it easier to read various files, move code across them, etc. General guidelines are:
 +  * Identify what the file does
 +  * Identify the major sections of your file
 +  * Use the same section identifier as other files
 +  * Whenever possible, make function static
 +
 +==== thisFile.c ====
 +
 +<code c>
 +//​Description of what this file is about
 +
 +//​****************************************************************************
 +// Include(s)
 +//​****************************************************************************
 +
 +#include "​main.h"​
 +#include "​thisFile.h"​
 +
 +//​****************************************************************************
 +// Variable(s)
 +//​****************************************************************************
 +
 +uint8_t myGlobalVar = 0;
 +
 +//​****************************************************************************
 +// Private Function Prototype(s):​
 +//​****************************************************************************
 +
 +static uint8_t computeNewValue(uint8_t dataPoint);
 +
 +//​****************************************************************************
 +// Public Function(s)
 +//​****************************************************************************
 +
 +//This function takes in X to produce Y
 +uint8_t thisFunction(uint8_t x)
 +{
 + uint8_t i = 0;
 + i = computeNewValue(rand());​
 + return i;
 +}
 +
 +//​****************************************************************************
 +// Private Function(s):​
 +//​****************************************************************************
 +
 +static uint8_t computeNewValue(uint8_t dataPoint)
 +{
 + return dataPoint + FUNCT_INCREMENT;​
 +}
 +
 +</​code>​
 +
 +==== thisFile.h ====
 +
 +<code c>
 +//​Description of what this file is about
 +
 +#ifndef INC_THIS_FILE_H
 +#define INC_THIS_FILE_H
 +
 +//​****************************************************************************
 +// Include(s)
 +//​****************************************************************************
 +
 +#include "​main.h"​
 +
 +//​****************************************************************************
 +// Definition(s)
 +//​****************************************************************************
 +
 +#define FUNCT_INCREMENT 5
 +
 +//​****************************************************************************
 +// Shared variable(s)
 +//​****************************************************************************
 +
 +extern uint8_t myGlobalVar;​
 +
 +//​****************************************************************************
 +// Public function prototype(s)
 +//​****************************************************************************
 +
 +//This function takes in X to produce Y
 +uint8_t thisFunction(uint8_t x);
 +
 +#endif //​INC_THIS_FILE_H
 +</​code>​
styleguide.1568228859.txt.gz · Last modified: 2019/09/11 19:07 by jfduval