Skip to content

Style guide for Arduino Uno. Created for my students, hopefully useful for everybody.

Notifications You must be signed in to change notification settings

Irikos/arduino-style-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

Style guide for Arduino (work in progress)

Created for students, hopefully useful for everybody.

Code is read more often than it is written. (Guido Van Rossum, creator of Python)

Arduino coding style guide, spurred from the lack of a comprehensive guide available on the internet and from the need to actually convince students to write their projects using more readable code and better practices.

Remember that a guide is just that, a guide. While some of the recommendation here are fundamental good practices, and it is highly recommended that you respect them, others are open to personal preferences.

The current guide uses many sources for inspiration: from shamelessly copying (parts of) forum and blog posts with useful practices, to imitating the style of other coding guides. These have all been referenced in References or in Other style guides. If I have missed anyone it is purely by mistake. Submit a PR and I will gladly adopt it.

Lastly, a style guide is a living document. It must constantly adapt to newer norms, technologies and techniques. The basis of any guide is its community. If you find this guide useful, please share it with other friends. If you notice a mistake or have updates to contribute, please submit a PR so that it can be adopted. Check Contributors to see a list of all the people that helped out.

Table of contents

  1. Consistency
  2. Naming conventions
  3. References
  4. Other style guides
  5. Contributors

Consistency

Consistency is more important than which convention is used.

Guide or not, remember that consistency in code is of utmost importance. So, despite this guide (or other), use the same style in your code. If joining an already existing project, adapt to the coding style used there, and be consistent. Where possible, refactor early when you notice inconsistency in a project, but be 100% sure that you won't ruin any compatibilities.

⬆ back to top

Naming conventions

  • Use descriptive, meaningful names for variables and functions. Avoid using short, generic names like "temp" or "count".

    Why? Generic short names do not accurately reflect the purpose of the variable of function.

    # Good:
    int temperature = 0;
    int counter = 0;
    ...
    void readTemperature() {
        // do stuff
    }
    
    # Bad:
    int temp = 0;
    int count = 0;
    ...
    void readTemp() {
        // do stuff
    }
    

⬆ back to top

Project-structure

Your code should explain itself, or use comments to do the same. Unless absolutely necessary, do not obfuscate your code for little gain in performance.

  • Start the project with a general comment that explain what the file (project) does

    Why? So the reader gets a general understanding of the program early on

  • Contiue with importing the necessary libraries

    Why? Hopefully self-explanatory, but the imported libraries should one of the first lines of code

  • Define constants and global variables

    Why? So they are easy to find and see. Do not declare global variables in other random places in the program.

  • Put your setup() and your loop() at the beginning of the program.

    Why? They help beginners to get an overview of the program, since all other functions are called from those two.

  • Continue with your own functions, in a program-specific logic order.

    Why? It makes the program easier to follow and debug.

    # potentially good
    /*
        Sketch title
    
        Describe what it does in layman's terms.  Refer to the components
        attached to the various pins.
    
        The circuit:
        * list the components attached to each input
        * list the components attached to each output
    
        Created day month year
        By author's name
        Modified day month year
        By author's name
    
        http://github.com/<user>/<repo>/<file> (or other relevant link, if available)
    */
    
    const byte temperatureSensorPin = A0;
    
    int temperature = 0;
    
    void setup() {
    
    }
    
    void loop() {
    
    }
    
    // user defined
    void writeNumber(byte number) {
        // do stuff
    }
    

⬆ back to top

Comments

  • Comment constants and variables whose usage are not obvious from the name
  • Comment every code block. Do it before the block, such that the reader knows what's coming.
  • Comment the foor loop
  • Do not write comments that are obvious from the code block name
    # bad
    // write the number
    void writeNumber(byte number) {
        // do stuff
    }

    # potentially good
    /* 
    Displays the number on the 7 segment display. 
    Number can be between 0 and 9.
    */
    void writeNumber(byte number, byte decimalPoint) {
        // do stuff
    }

Working with Serial

Reading too much from Serial.available()

If Serial.available () returns non-zero, you know have at least one byte. Trying to read two bytes is wrong, unless you specifically test for at least two.

// Bad
if (Serial.available()) {
    byte a = Serial.read();
    byte b = Serial.read();
}

// Potentially good
while (Serial.available()) {
    byte incomingByte = Serial.read();
    // do stuff with it
}

Working with pins

Trying to work "on a set"

While the first code will compile, it will only set the first pin to HIGH

// Bad
digitalWrite((9, 10, 11), HIGH);

// Good
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
// (you should have a constant for each pin, though)

⬆ back to top

References

References go here

⬆ back to top

Other style guides

Other coding guides, used for inspiration or just plain useful

⬆ back to top

Contributors

Contributors list

⬆ back to top

About

Style guide for Arduino Uno. Created for my students, hopefully useful for everybody.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published