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:
1
<code line>; // this is a comment; this code line does ‘xx’
Copied!
or
1
// this is a comment; this code line does \'xx\' :
2
<code line>;
Copied!
You can even add long comments; they will clear things up:
1
/* now we will perform ‘xx’ action
2
because we need to ‘yy’
3
in order to get ‘zz’ done
4
*/
5
<code>
Copied!
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
// 1 – configure module
2
// 2 – read sensor
3
// 3 – xxx
Copied!
Variables and constants
Variables should be lower case (upper case for 2nd, 3rd word). Use meaningful words and try to give a default value:
1
int counter1 = 0;
2
float newTemperature = 0.0;
Copied!
Constants should be upper case. Use meaningful words:
1
#define TIMEOUT_GPRS 1000
Copied!
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)
1
if (<condition>)
2
{
3
<code_line_1>;
4
}
5
else
6
{
7
<code_line_2>;
8
}
Copied!
(B)
1
if (<condition>) {<code_line_1>;}
2
else {<code_line_2>;}
Copied!
(C)
1
if (<condition>){
2
<code_line_1>;
3
}
4
else{
5
<code_line_2>;
6
}
Copied!
Always prefer explicit and sparse ways of code writing.
Copy link