Code style

In this section some recommendations are given in order to make a clear and easy readable code. Try to write beautiful code! You can take a look to all of our examples as a reference. We try to keep some programming rules.

However, each programmer has his own way to write code and could not like some of the given recommendations. The purpose of this section is just to give a guideline to keep things tidy.

Comment your code.

This will help your colleagues to understand what you try to do. Also, it will help the Libelium Technical Service if they needed to analyze the code. It could even help you if you read your own code months after you created it. The recommended styles are:

<code line>; // this is a comment; this code line does ‘xx’

or

// this is a comment; this code line does \'xx\' :
<code line>;

You can even add long comments; they will clear things up:

/*  now we will perform ‘xx’ action
    because we need to ‘yy’
    in order to get ‘zz’ done
*/
<code>

Use English for comments.. it is the most common language! If you need to share your codes with other developers or clients in foreign countries, it is better to start in English.

Number the parts of your code.

This is basic to keep code organized and improve the legibility. The recommended style is:

// 1 – configure module
// 2 – read sensor
// 3 – xxx

Variables and constants

Variables should be lower case (upper case for 2nd, 3rd word). Use meaningful words and try to give a default value:

int counter1 = 0;
float newTemperature = 0.0;

Constants should be upper case. Use meaningful words:

#define TIMEOUT_GPRS 1000

Indentation

Default indentation is 2 whitespaces (or use the key Tab). Use indentation to make code flow more visual, readable.

'if', 'while' and 'for' loops

"Sparse is better than dense". (A) is probably more readable than (B) or (C):

(A)

if (<condition>)
{
    <code_line_1>;
}
else
{
    <code_line_2>;
}

(B)

if (<condition>) {<code_line_1>;}
else {<code_line_2>;}

(C)

if (<condition>){
    <code_line_1>;
}
else{
    <code_line_2>;
}

Always prefer explicit and sparse ways of code writing.

Last updated