Professional Documents
Culture Documents
<<Unique ID>>
Identifier
Approvals
Proprietary Information
This Document Contains AEC Proprietary
Information, and shall not be disclosed or used
for the benefit of others without the written permission of
the President & CEO or his designee.
Table of Contents
Topic Page #
Introduction ………………………………………………………... 3
References ………………………………………………………….. 40
2
INTRODUCTION
Objective
Applicability
This coding standard applies to all new C code that is part of Embedded
Software developed by Products Development Section/R&D. Whenever
existing code is modified (if the major portion of the file is being updated),
then update the source code to abide with the conventions outlined in this
document. This will ensure that old code will be upgraded over time.
Classification of Rules
3
1: GENERAL RULES
1.1. Braces
Example:
s1 interlock = OFF;
s2 if (stop)
s3 flag = ON;
s4 interlock = ON;
s5 if (interlock)
s6 open_doors();
s7 else
s8 apply_breaks();
s9 sound_alarm();
Q: There appear to be a problem with line #s4 & line #s9. Is the programmer
forgotten to put braces by mistake? OR it is just incorrect indentation?
4
1.2. Parentheses
Example:
5
1.3. Static
Example:
…
/* Declaring Static Variables */
static u8 m_demand_counter = 0;
….
/* Declaring Static Functions */
static void stop_process (void);
…..
….
….
….
}
6
1.4. Volatile
Example:
void main()
{
...
while (!ext_rcvd)
{
// Wait
}
...
}
Q: The above program is not working if “optimization” is turned on. Which part
of the code is being removed due to optimization and what is the solution do you
think?
7
1.5. Const
Example:
…
const double pi = 3.14159265358979323846;
…
u16 my_func (u8 const *s);
…
8
1.6 Magic Numbers
Example:
/* Don’t code like this */
case 0x01:
transmit_handler(0x10);
reset_machine();
if (timed_out)
{
transmit_handler(0x60);
}
else
{
transmit_handler(0x20);
}
break;
transmit_handler(BUSY);
reset_machine();
if (timed_out)
{
transmit_handler(TIMED_OUT);
}
else
{
transmit_handler(READY_RESET);
}
break;
9
1.7 Naming of Source/Header Files
Example:
Application Process.c
Application Process.h
Maximum Demand.c
Maximum Demand.h
appln_process.c
appln_process.h
max_demand.c
max_demand.h
10
1.8 Guards for Header Files
Example:
#ifndef __FILE1_H__
#define __FILE1_H__
/* the following lines will be excluded by the preprocessor if the file is included
more than once */
….
….
…
#endif //__FILE1_H__
11
1.9 Enumerator List Initialization
Example:
….
typedef enum { res_norma=29, res_not_finished, res_user_define = 30 }
Release_response_reason ;
….
12
1.10 Function-like Macros
Example:
s = square (i++);
…
13
1.11. goto, break, continue, auto & register
14
1.12 Line Width
Example:
/* Better Way*/
top = (load * stress - safety_margin - fudge_factor);
bottom = (length * width * depth - shrinkage);
ratio = top / bottom;
15
2: COMMENTS
Example:
….
u16 a = 12;
u16 b = 2;
….
/*a = 24; */
b = a//* divide by 4 */4;
16
2.2 Nested Comments
Example:
…
u16 a;
u16 size;
…
/* set local variables
a = 0;
/* set the variable size
to a known value */
size = 100;
17
3: Data Types & Variables
Example:
18
3.2 Variable Naming
Example:
u8 g_first_run = 2;
…
static u8 m_demand_counter = 0;
…
s8 entry_index = -1;
….
//#define MAX_COUNT 100
const u8 max_count = 100;
…
19
3.3 Variable Initialization
Example:
…
char ret_val; // Variable not initialized.
…
case A: ret_val = 3;
break;
Case B: ret_val = 6;
break;
Case C: #ifdef M2
ret_val = 4;
#endif
break;
default: ret_val = 0;
break;
….
return ret_val;
……
20
3.4 Hiding Variables
Example:
Q: The problem is that we've now used the same word for two different things. Is
location being updated is a global or a local?
21
4: Functions
Example:
22
4.2 Function Length
23
4.3 Function Complexity
24
4.4 Indentation Levels
Example:
/* Do not code like this; stop at 3 levels & re-design/factor */
if (x == y)
{
if (m== n)
{
if ( p == q)
{
if ( r == s)
{
switch (r)
{
case 1:
do_something();
switch (q)
{
case l:
….
…
break;
…..
…..
…..
25
4.5 Single Exit
Example:
/* Don’t code like this with more than one ‘return’ statement */
…
case A: …
return (3);
break;
Case B: ….
return (6);
break;
Case C: #ifdef M2
…
return (4);
#endif
break;
default: …
return (0);
break;
….
26
4.6 Avoid Function Calls in ISR
27
4.7 Unavoidable Function Calls in ISR
Example:
28
5: Expressions & Statements
Example:
u16 var_1;
s16 var_2;
var_1 = 10;
var_2 = -1;
29
5.2 Casting
Example:
u16 g_var1;
u8 var_2;
…..
if (…)
{
var_2 =g_ var_1;
}
30
5.3 Float Comparison
Example:
f32 var_1;
f32 var_2;
f32 var_3;
…..
var_1 = 9.64;
var_2 = 20.56;
var_3 = 30.2;
31
5.4 Comma Operator
Example:
32
5.5 Bit-wise Operations on Signed Integers
Example:
33
5.6 Array Index
Example:
34
5.7 If-Else
Example:
u8 x;
u8 y;
…
if (x = y) //Is this a typing mistake?
{
do_some_thing();
}
else
{
do_another_thing();
}
x = 2;
y = 0;
35
5.8 If-Else If
Example:
u8 x;
u8 y;
…
if (x = y)
{
do_some_thing();
}
else if (x == 2)
{
do_another_thing();
}
else
{
catch_error ();
}
36
5.9 Default in Switch
Example:
switch (g_var_1)
{
case READ:
read_process();
break;
case WRITE:
write_process();
break;
case EXECUTE:
exec_process();
break;
37
5.10 Break in Switch
Example:
switch (g_error)
{
case 1:
…
break;
case 2:
…
…
case 3:
…
break;
default:
catch_error();
break;
}
38
5.11 ++ and --
Example:
u8 b[4] = { 7, 21, 249, 100 };
…
x = b[i] + i++;
…
39
REFERENCES
40