m730053b training
DESCRIPTION
programmingTRANSCRIPT
-
5/19/2018 M730053b Training
1/81SV P ED T DA 1 Khosrau Heidary C Coding Guidelines
Training on
C Coding Rules
M730053b
Welcome to the
Duration: approx. 4h (initial) / 2h (update)
Trainer: Khosrau Heidary
SV P ED T D
Tel.: !"#$##
e"%ail: &hosrau.heidary'sie%ens.o%
-
5/19/2018 M730053b Training
2/81SV P ED T DA Khosrau Heidary Coding Guidelines!
Background
Break
C Coding Guidelines - Overview
General Rules
Comments
ata and ata t!"es O"erators and #$"ressions
%tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance
)ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
3/81SV P ED T DA Khosrau Heidary Coding Guidelines"
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions
%tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance
)ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
4/81SV P ED T DA Khosrau Heidary Coding Guidelines#
while (*p>=1?1:0) f*=*p--;
while (*p>=1) f*=(*p)--;
/* calculate faculty of p */
Background +,
/* calculate faculty of p */
-
5/19/2018 M730053b Training
5/81SV P ED T DA Khosrau Heidary Coding Guidelines$
Background +.
ode should *e porta*le *et+een people
ode should *e orret and understanda*le
-
5/19/2018 M730053b Training
6/81
SV P ED T DA Khosrau Heidary Coding Guidelines%
Background +3
ode should *e porta*le *et+een
pro,ets
#C/, #C/.
-
5/19/2018 M730053b Training
7/81
SV P ED T DA Khosrau Heidary Coding Guidelines&
flag lv_status; flag *pt = !lv_status;
u" status;
u" *pt = !status;
Background +
'lag could (e
de'ined as )*T
-
5/19/2018 M730053b Training
8/81
SV P ED T DA Khosrau Heidary Coding Guidelines+
#ea u1$ *pt1;
fa u1$ *pt%;
u1$ *pt1;
u1$ *pt%;
Background +5
near,'ar not
de'ined
(y *S-
-
5/19/2018 M730053b Training
9/81
SV P ED T DA Khosrau Heidary Coding Guidelines.
&'efi#e _+, (u%).0000 &if _+, = .0000
u% = _+,;
&'efi#e _+, .0000 &if _+, = .0000
u% = (u%)_+,;
Background +1
Cast in /i'
not allo0ed
)y *S-
-
5/19/2018 M730053b Training
10/81
SV P ED T DA Khosrau Heidary Coding Guidelines1
Background +7
ode should *e porta*le *et+een
o%pilers
*c *h files
-
5/19/2018 M730053b Training
11/81
SV P ED T DA Khosrau Heidary Coding Guidelines11
s1$ = -%000; u1$ y = %; if (( / y) 2 0)
s1$ = -%000; u1$ y = %;
if (( / (s1$)y) 2 0)
Background +2
2 , y 3 1%&%+
on 1% (it systems
-3.000 ,000 00,, 0000 0000
$ is "romoted to u,1
4,000 00,, 0000 0000 33531
4 33531 . 6 ,1712
-
5/19/2018 M730053b Training
12/81
SV P ED T DA Khosrau Heidary Coding Guidelines1!
Background +
ode should *e porta*le *et+een
tar-ets
*c *h files
-
5/19/2018 M730053b Training
13/81
SV P ED T DA Khosrau Heidary Coding Guidelines1"
u" 'iv(u" val_1 u" val_%) 3 etu# = val_1 / val_%; 4
u" 'iv(u" val_1 u" val_%) 3 if (val_% > 0)
3 etu# = val_1 / val_%; 4 else 3 4 4
Background +,0
di4ision (y
5ossi(le
-
5/19/2018 M730053b Training
14/81
SV P ED T DA Khosrau Heidary Coding Guidelines1#
5o'_ah ----------- &'efi#e _678_989 %
5o'_
-
5/19/2018 M730053b Training
15/81
SV P ED T DA Khosrau Heidary Coding Guidelines1$
Background +,.
ode should *e sae and ro*ust
-
5/19/2018 M730053b Training
16/81
SV P ED T DA Khosrau Heidary Coding Guidelines1%
Background +,3
T*ere8ore9
(ctivities initiated b! %: (rc*itecture grou" 8or 3.-bit %:9
7Memor! (llocation conce"t
7(ggregate conce"t
7Con8iguration conce"t
7'artitioning
7Basic ata T!"es
7Coding Guidelines
7()%; %tandard
7;%O;#C 2 +1990< ,
7M;%R( guidelines 8or t*e C language in ve*icle based %:
7+ M00,< M00.0
%tandards 8or C-Coding9
-
5/19/2018 M730053b Training
17/81
SV P ED T DA Khosrau Heidary Coding Guidelines1&
Background +,
Roadma"9
=7=0,
;nitial version ,=3
ela(orated and re4ie0ed
(y Wor8ing Grou59
Giron Patricia :T;S*S6A
Team9
Giron Patricia :T;Seri8ication Code re4ie0
#$am"le )ad Good
toto = lamb; #define SET_TOTO(a) {toto=(a); }
)ad
*n the (ad e2am5le code is generated (y the header 'ile The good e2am5le
e25orts a macro Code 0ill only (e generated 0hen the macro 0ill (e called 0ithin a
c'ileRe8erence
Background
-
5/19/2018 M730053b Training
22/81
SV P ED T DA Khosrau Heidary Coding Guidelines!!
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions
%tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance
)ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
23/81
SV P ED T DA Khosrau Heidary Coding Guidelines!"
Comments
%ummar!%ummar!
. Explain eah P";int o%%ent
-
5/19/2018 M730053b Training
24/81
SV P ED T DA Khosrau Heidary Coding Guidelines!#
Comments +.
C=7 #ac* su""ression o8 'C-?int messages s*all be e$"lainedC=7 #ac* su""ression o8 'C-?int messages s*all be e$"lainedescri"tion To5 5riority is to a4oid su55ressing any PC;int 0arning A comment that su55resses
a PC;*T message must (e 0ell e25lained The sco5e o' the ;*T comment should
(e as small as 5ossi(le ;int 0arnings ha4e to (e su55ressed at the cause :eg atmacro or stu( de'inition< and not 0here they a55ear
#$am"le )ad Good
u16 c_xxx(u16 x) { u16 i = 0;
wile (i != x) i"";
etun = (x $ i);}
u16 c_xxx(u16 x) { u16 i = 0;
wile (i != x) i"";
etun = (x $ i); $%lint &e'*i alwa+, - 0 .- no di/ b+ eo %$
}
)ad
The (ad e2am5le generates the PC;int 0arning &.$ Concei4a(le di4ision (y
The good e2am5le su55resses this 0arning and descri(es the reason 0hy The
su55ression is only e''ecti4e 'or the 5articular code line
-
5/19/2018 M730053b Training
25/81
SV P ED T DA Khosrau Heidary Coding Guidelines!$
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions
%tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance
)ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
26/81
SV P ED T DA Khosrau Heidary Coding Guidelines!%
ata and ata T!"es
%ummar!%ummar!
D.53 ast i%%ediate 7alues (exept 0..#2$) expliitly
D.8 t use the enumtype to deine 7aria*les
-
5/19/2018 M730053b Training
27/81
SV P ED T DA Khosrau Heidary Coding Guidelines!&
ata and ata T!"es +.
=5 &lags s*all contain 0 and , onl!=5 &lags s*all contain 0 and , onl!escri"tion The data ty5e flagis de'ined to descri(e a (oolean in'ormation only Store only or 1
in a 4aria(le o' this ty5e *' more in'ormation needs to (e stored in a 4aria(le then
(it'ields or data ty5es :u+B F< shall (e used
#$am"le , )ad Goodfla l/_x;u16 m+_data;
l/_x = T23E;
l/_x = 0x4E;
l/_x = m+_data;
fla l/_x;u16 m+_data;
l/_x = 1;
l/_x = 1;
l/_x = (m+_data &= 0);
)ad
*n the (ad e2am5le you dont 8no0 i' T6E and =A;SE is eIual to 1 and *n the
good e2am5le the 4alues are de'ined e2actly The 4alue 2=E is assigned to a 'lag
This re5resents logical JT6EJ tooB (ut does not 0or8 on e4ery im5lementation The
right side o' an assignment to 'lags should al0ays (e a )oolean e25ression
-
5/19/2018 M730053b Training
28/81
SV P ED T DA Khosrau Heidary Coding Guidelines!+
ata and ata T!"es +3
=5 &lags s*all contain 0 and , onl!=5 &lags s*all contain 0 and , onl!
#$am"le . )ad Good
$% 5oce,,o 1* fla a, int %$t+edef int fla;
$% 5oce,,o 7* fla a, bit %$
t+edef bit fla;
$% won u,ae of fla* %$fla l/_,tatu, = 0x10;
if (l/_,tatu, 8 0x10)9
$% don:t u,e fla in ti, ca,e* %$u ,tatu, = 0x10;
if (,tatu, 8 0x10)
)ad
The (ad e2am5le 0ill not 0or8 i' 'lag is really only a (it
-
5/19/2018 M730053b Training
29/81
SV P ED T DA Khosrau Heidary Coding Guidelines!.
ata and ata T!"es +.
=1 o not use a "ointer to a 8lag=1 o not use a "ointer to a 8lagescri"tion Do not use a 5ointer to a 4aria(le o' data ty5e 'lag se a di''erent data ty5eB i' indirect
access is reIuired
#$am"le )ad Good
fla l/_,tatu,;
fla %t = 8l/_,tatu,;
u ,tatu,;
u %t = 8,tatu,;
)ad
The (ad e2am5le 0ill not 0or8 :or e4en get com5iled< i' 'lag is really a :non
addressa(le< (it
-
5/19/2018 M730053b Training
30/81
SV P ED T DA Khosrau Heidary Coding Guidelines"
ata and ata T!"es +
=7 o not initialiAe global and static variables wit* initial value 0=7 o not initialiAe global and static variables wit* initial value 0escri"tion Any glo(al or static 4aria(leB 0hich should (e initiali@ed 0ith during startu5B shall not
(e initiali@ed e25licitly
Background The initiali@ation o' these storage ty5es 0ith 0ill (e done at startu5 time (y the 6A>
test An e25licit initiali@ation 0ould result in additional startu5 time The 6A> test has
to (e con'igured accordingly
-
5/19/2018 M730053b Training
31/81
SV P ED T DA Khosrau Heidary Coding Guidelines"1
ata and ata T!"es +5
#$am"le )ad Good
u% v1 = 0;static u% v% = 0;u
-
5/19/2018 M730053b Training
32/81
SV P ED T DA Khosrau Heidary Coding Guidelines"!
ata and ata T!"es +1
R(M
v,
v.
u% v1 = 0;static u% v% = 0;u
-
5/19/2018 M730053b Training
33/81
SV P ED T DA Khosrau Heidary Coding Guidelines""
ata and ata T!"es +7
=, Cast immediate values +e$ce"t 0==3.717 e$"licitl!=, Cast immediate values +e$ce"t 0==3.717 e$"licitl!escri"tion Each immediate 4alue :e2ce5t "!&%&< has to (e casted to a 5rede'ined data ty5e
:u1%B F
-
5/19/2018 M730053b Training
34/81
SV P ED T DA Khosrau Heidary Coding Guidelines"#
ata and ata T!"es +2
=.0 'er8orm t!"e casts e$"licitl! i8 needed=.0 'er8orm t!"e casts e$"licitl! i8 neededescri"tion A4oid unnecessary e25licit ty5e casts Per'orm ty5e casts in the 'ollo0ing cases9
1 )oth o5erands o' a (inary o5erator :eg LB MB 33< ha4e to (e either unsigned or
signedB (ut not mi2ed *' not 5er'orm the reIuired ty5ecast
! The le'thand o5erand o' a (inary nonrelational o5erator has to (e casted i' its
si@e is smaller than the e25ected si@e o' the o5eration
" An e25licit ty5e cast is necessary to reduce the data si@e
# An e25licit ty5e cast is necessary 'or con4erting 'rom signed to unsigned or
(ac8
#$am"le ,
+not mi$ed
unsigned
signed o"er-
ands
)ad Good
,16 x = .
-
5/19/2018 M730053b Training
35/81
SV P ED T DA Khosrau Heidary Coding Guidelines"$
ata and ata T!"es +
=.0 'er8orm t!"e casts e$"licitl! i8 needed=.0 'er8orm t!"e casts e$"licitl! i8 needed
#$am"le .
+le8t-*ando"erand de-
termines siAe o8
o"eration
)ad Good
,16 /a1;,16 /a7;,
-
5/19/2018 M730053b Training
36/81
SV P ED T DA Khosrau Heidary Coding Guidelines"%
ata and ata T!"es +,0
=.0 'er8orm t!"e casts e$"licitl! i8 needed=.0 'er8orm t!"e casts e$"licitl! i8 needed
escri"tion A4oid unnecessary e25licit ty5e casts Per'orm ty5e casts in the 'ollo0ing cases9
1 )oth o5erands o' a (inary o5erator :eg LB MB 33< ha4e to (e either unsigned or
signedB (ut not mi2ed *' not 5er'orm the reIuired ty5ecast
! The le'thand o5erand o' a (inary nonrelational o5erator has to (e casted i' its
si@e is smaller than the e25ected si@e o' the o5eration
" An e25licit ty5e cast is necessary to reduce the data si@e
# An e25licit ty5e cast is necessary 'or con4erting 'rom signed to unsigned or
(ac8
-
5/19/2018 M730053b Training
37/81
SV P ED T DA Khosrau Heidary Coding Guidelines"&
ata and ata T!"es +,,
#$am"le 3
+reduce data
siAe
)ad Good
u16 /a1;u /a7;9
if (/a1 ! 76){
/a7 = /a1;}el,e{ /a7 = 0x44;}
u16 /a1;u /a7;9
if (/a1 ! 76){
/a7 = (u)/a1;}el,e{ /a7 = 0x44;}
*n the J(adJ e2am5le it is unclear 0hether data si@e is reduced intentionally or (y mista8e ;*T 0ill
issue a 0arning *n the JgoodJ e2am5le the intention is documented
-
5/19/2018 M730053b Training
38/81
SV P ED T DA Khosrau Heidary Coding Guidelines"+
ata and ata T!"es +,.
=.0 'er8orm t!"e casts e$"licitl! i8 needed=.0 'er8orm t!"e casts e$"licitl! i8 needed
#$am"le
+converting
unsigned to
signed
)ad Good
,16 a;u16 b;
9a = u16_min_u16_u16(bA 0x'444);
,16 a;u16 b;
9a = (,16)u16_min_u16_u16(bA 0x'444);
*n the J(adJ e2am5le it is unclear 0hether the con4ersion to signed is done intentionally or (y
mista8e ;*T 0ill issue a 0arning *n the JgoodJ e2am5le the intention is documented
escri"tion A4oid unnecessary e25licit ty5e casts Per'orm ty5e casts in the 'ollo0ing cases9
1 )oth o5erands o' a (inary o5erator :eg LB MB 33< ha4e to (e either unsigned or
signedB (ut not mi2ed *' not 5er'orm the reIuired ty5ecast
! The le'thand o5erand o' a (inary nonrelational o5erator has to (e casted i' its
si@e is smaller than the e25ected si@e o' the o5eration
" An e25licit ty5e cast is necessary to reduce the data si@e# An e25licit ty5e cast is necessary 'or con4erting 'rom signed to unsigned or
(ac8
-
5/19/2018 M730053b Training
39/81
SV P ED T DA Khosrau Heidary Coding Guidelines".
ata and ata T!"es +,3
=. onDt use t*e enumt!"e to de8ine variables=. onDt use t*e enumt!"e to de8ine variablesescri"tion Varia(les o' enumty5e are not usa(le due to high 6A>consum5tion So use #define
or enumeration sets together 0ith the smallest 5ossi(le data ty5e :mostly u+< 'or state
4aria(les *n the latter case use only tagless enums
#$am"le , +R(M
consum"tion
)ad Good
enum {>?_STBTE1A >?_STBTE7} ,tate; #define >?_STBTE1 0x00
#define >?_STBTE7 0x01u ,tate;
,tate = (u)>?_STBTE1;
The (ad e2am5le is good in terms o' codeB (ut the state 4alue 0ill use the integer data ty5eB 0hich
needs more 6A>
-
5/19/2018 M730053b Training
40/81
SV P ED T DA Khosrau Heidary Coding Guidelines#
ata and ata T!"es +,
=. onDt use t*e enum t!"e to de8ine state variables=. onDt use t*e enum t!"e to de8ine state variables
#$am"le .
+enumusage
)ad Good
enum STBTES {>?_STBTE1A>?_STBTE7};u ,tate;
,tate = >?_STBTE1;
enum {>?_STBTE1A >?_STBTE7};u ,tate;
,tate = (u)>?_STBTE1;
*n the (ad e2am5le the enumhas the tag STATES This de'ines a ne0 data ty5e An e25licit ty5e
cast 0ould (e necessary 0hen assigning the state CNSTATE1 to the 4aria(le state
-
5/19/2018 M730053b Training
41/81
SV P ED T DA Khosrau Heidary Coding Guidelines#1
ata and ata T!"es +,5
=. onDt use t*e enum t!"e to de8ine state variables=. onDt use t*e enum t!"e to de8ine state variables
#$am"le 3
+enumusage)ad Good
enum { >?_CDB = 0x?_TOTO =0xBB};
u /a;
/a = >?_CDB;
#define >?_CDB 0x?_TOTO 0xBBu /a;
/a = (u)>?_CDB;
*n the (ad e2am5le an enum mem(er is used to initiali@e a u+ 4aria(le Enumsare a s5ecial data
ty5e to contain enumerations only They ha4e nothing to do 0ith the integer data ty5es *n this case
use #define
*' you need to de'ine state 4alues its more use'ul to use enumty5e 'or de'inition o' data sets
together 0ith u+ ty5e :see e2am5le !
-
5/19/2018 M730053b Training
42/81
SV P ED T DA Khosrau Heidary Coding Guidelines#!
#$ercise +,
#$ercises on
7General Rules
7Comments
7ata and ata T!"es
#$ercises on
7General Rules7Comments
7ata and ata T!"es
-
5/19/2018 M730053b Training
43/81
SV P ED T DA Khosrau Heidary Coding Guidelines#"
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions
%tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
44/81
SV P ED T DA Khosrau Heidary Coding Guidelines##
O"erators and #$"ressions
%ummar!%ummar!
.# Do not use state%ents +hih depend on the order o e7aluation or on lo-ial shortuts
.4 6se the ?operator only in speial ases
-
5/19/2018 M730053b Training
45/81
SV P ED T DA Khosrau Heidary Coding Guidelines#$
O"erators and #$"ressions +.
O=3 o not use statements w*ic* de"end on t*e order o8 evaluation or on logical s*ortcuts O=3 o not use statements w*ic* de"end on t*e order o8 evaluation or on logical s*ortcutsescri"tion E4ery statement has to (e 0ritten in a 0ay that does not de5end on the order o'
e4aluation se only 'unctions in an e25ressionB 0hich dont modi'y glo(al resources
=unctionsB 0hich 5er'orm an actionB are not allo0ed to (e used in e25ressions 0ith a
shortcut
#$am"le , )ad Good
x = biF " i""; x = biF " i;
i"";
*n the J(adJ e2am5le it is not clearB 0hat the 4alue o' i isB 0hen accessing (Oi *t de5ends on the
order o' e4aluation There'ore in the good e2am5le i is incremented 0ithin a se5arate statement
-
5/19/2018 M730053b Training
46/81
SV P ED T DA Khosrau Heidary Coding Guidelines#%
O"erators and #$"ressions +3
O=3 o not use statements w*ic* de"end on t*e order o8 evaluation or on logical s*ortcuts O=3 o not use statements w*ic* de"end on t*e order o8 evaluation or on logical s*ortcuts#$am"le . )ad Good
u a10F = {0A1A7A
-
5/19/2018 M730053b Training
47/81
SV P ED T DA Khosrau Heidary Coding Guidelines#&
O"erators and #$"ressions +
O= /se t*e ?o"erator onl! in s"ecial casesO= /se t*e ?o"erator onl! in s"ecial casesescri"tion se the ? o5erator only i' the 5rogram (ecomes 'aster and,or smaller The used
e25ressions should (e less com5le2 in that case
*n standard cases and 'or more com5le2 e25ressionsB use the ifinstructioninstead
#$am"le , )ad Good
e=(d 88 (a - (b%c))) G a " c * b; if (d 88 ( a - (b % c))){
e = a " c;}el,e{ e = b;}
*n the (ad e2am5le the reada(ility is not acce5ta(le =urthermore the use o' Q has no ad4antageThe i'instruction should (e used in such cases
-
5/19/2018 M730053b Training
48/81
SV P ED T DA Khosrau Heidary Coding Guidelines#+
O"erators and #$"ressions +5
#$am"le . )ad Good
u16 tm_n_min;
if (l/_at) tm_n_min = c_n_min_at;el,e tm_n_min = c_n_min_mt;
if (l/_ena
88 (n - tm_n_min))
HHH
if (l/_ena 88 (n - (l/_at G c_n_min_at * c_n_min_mt))
HHH
*n J(adJ e2am5leB the im5lementation is Iuite ine''ecti4e *n this case it ma8es sense to use the Q
o5erator
O= /se t*e ?o"erator onl! in s"ecial casesO= /se t*e ?o"erator onl! in s"ecial cases
-
5/19/2018 M730053b Training
49/81
SV P ED T DA Khosrau Heidary Coding Guidelines#.
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions %tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
50/81
SV P ED T DA Khosrau Heidary Coding Guidelines$
%tatements and Control %tructures
%ummar!%ummar!
S.55 he& array indexes
S.58 Ter%inate eah ase in a switchstate%ent +ith a break
S.5$ Put the %ost deisi7e riterion o a ondition at the *e-innin-
-
5/19/2018 M730053b Training
51/81
SV P ED T DA Khosrau Heidary Coding Guidelines$1
%tatements and Control %tructures +.
%=,, C*eck arra! inde$es%=,, C*eck arra! inde$esescri"tion *nde2es on arrays must (e chec8ed during coding 5hase and re4ie0ing to ensure that
inde2 4alue 5oints do not e2ceed the array range *' it can not (e guaranteed (y any
other means :eg code re4ie0B static code chec8B integration re4ie0
-
5/19/2018 M730053b Training
52/81
SV P ED T DA Khosrau Heidary Coding Guidelines$!
%tatements and Control %tructures +3
%=,5 Terminate eac* case in a switchstatement wit* a break%=,5 Terminate eac* case in a switchstatement wit* a breakescri"tion Each case :including the de'ault case< shall (e terminated 0ith a (rea8 statement *' it
is 0anted to omit the (rea8 statement then it needs to (e documented
#$am"le )ad Good
,witc(,tate){
ca,e 0* ca,e 1*
do_,ometin_fo_ca,e_0_and_1(); beaK; ca,e 7* do_,ometin_fo_ca,e_7(); beaK; default*}
,witc (,tate) {
ca,e 0* $%lint .falltou%$ ca,e 1*
do_,ometin_fo_ca,e_0_and_1(); beaK; ca,e 7* do_,ometin_fo_ca,e_7(); beaK; default* beaK;
}
The good e2am5le terminates each case 0ith a (rea8 or documents that it 0as intentionally
omitted ote9 ;int understands the comment ,lint 'allthrough, a'ter the last statement o' the
5receding case Please note that the case(loc8 in "! im5lies (racesU so the ;*Tcomment must(e outside the (ody o' the case(loc8
-
5/19/2018 M730053b Training
53/81
SV P ED T DA Khosrau Heidary Coding Guidelines$"
%tatements and Control %tructures +
%=,1 'ut t*e most decisive criterion o8 a condition at t*e beginning%=,1 'ut t*e most decisive criterion o8 a condition at t*e beginning
#$am"le , )ad Good
fla x;
if (l/_iK 88 l/_,t) x = 1;if (l/_,t LL l/_iK) x = 1;
fla x;
if (l/_,t 88 l/_iK) x = 1;if (l/_iK LL l/_,t) x = 1;
AssumingB that it is more li8ely that l4Nig8 is set and l4Nst is not setB the order o' the criterions is
0rong in the (ad e2am5le
Put the least 5ro(a(le criterion 0ithin a condition and the most 5ro(a(le criterion
0ithin a condition at the (eginning o' the e25ression
Put the most 5ro(a(le case at the (eginning o' a s0itchstatement
escri"tion
#$am"le . )ad Good
fla f;
if((u_add_u_u(aA b) ! c) 88 f)9
fla f;
if (f 88 (u_add_u_u(aA b) ! c)) 9
*n the (ad e2am5le the 'unction 0ill (e called in any case
-
5/19/2018 M730053b Training
54/81
SV P ED T DA Khosrau Heidary Coding Guidelines$#
#$ercise +.
#$ercises on
7O"erators and #$"ressions
7%tatements and Control %tructures
#$ercises on
7O"erators and #$"ressions
7%tatements and Control %tructures
-
5/19/2018 M730053b Training
55/81
SV P ED T DA Khosrau Heidary Coding Guidelines$$
C Coding Guidelines - Overview
Break
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions %tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
56/81
SV P ED T DA Khosrau Heidary Coding Guidelines$%
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions %tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
57/81
SV P ED T DA Khosrau Heidary Coding Guidelines$&
&unctions
%ummar!%ummar!
-
5/19/2018 M730053b Training
58/81
SV P ED T DA Khosrau Heidary Coding Guidelines$+
&unctions +.
&=. (lwa!s declare 8unction "rotot!"es 8or e$"orted 8unctions&=. (lwa!s declare 8unction "rotot!"es 8or e$"orted 8unctionsescri"tion The de4elo5er o' an e25orted 'unction has to declare a 'unction 5rototy5e in the
res5ecti4e header 'ile When this 'unction is to (e used in another moduleB the header
has to (e included o e25licit e2terndeclaration shall (e done outside this header
The 5rototy5e must (e 8no0n 0hen calling the 'unction
)ote =or 5ri4ate 'unctions :'unctions that are only 4isi(le 0ithin the module< no e25licit
5rototy5e is needed in order to a4oid redundancy *nsteadB the de4elo5er can ta8ecare 'or the correct order o' the 'unctions de'initions to ensure that the 5rototy5e o' a
'unction is a4aila(le 0hen it is called
-
5/19/2018 M730053b Training
59/81
SV P ED T DA Khosrau Heidary Coding Guidelines$.
&unctions +3
moduleE,=c
void 8Ee$t+void F
static void 8E"riv+void F
8E"riv+ H
void 8Eclient+void F
8Ee$t+ H
moduleE.=ce$tern void 8Ee$t+voidH
e$tern void 8Ee$t+voidHmoduleE,=*
static void 8E"riv+voidH
Iinclude JmoduleE,=*K
Take
care
o8correctorderL
-
5/19/2018 M730053b Training
60/81
SV P ED T DA Khosrau Heidary Coding Guidelines%
&unctions +
&=1 /se inline 8unctions w*ere reuired&=1 /se inline 8unctions w*ere reuiredescri"tion *nline 'unctions are a use'ul means to im5lement 'unctionli8e structures 0ithout the
o4erhead o' 'unction calls They shall (e used 0here4er this is suita(le
#$am"le )ad Good
,
-
5/19/2018 M730053b Training
61/81
SV P ED T DA Khosrau Heidary Coding Guidelines%1
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions %tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
-
5/19/2018 M730053b Training
62/81
SV P ED T DA Khosrau Heidary Coding Guidelines%!
're"rocessor irectives
%ummar!%ummar!
P.4 Enlose %aros9 +hih don>t ha7e a return 7alue9 +hithin {}
P.8 @aros %ust not end +ith a ABC
' i ti +.
-
5/19/2018 M730053b Training
63/81
SV P ED T DA Khosrau Heidary Coding Guidelines%"
're"rocessor irectives +.
'= #nclose macros< w*ic* donDt *ave a return value< wit*in {}'= #nclose macros< w*ic* donDt *ave a return value< wit*in {}
#$am"le )ad Good
#define MN_MB?2O() l/_,tate_1=1; l/_,tate_7=0
if (l/_,tate_0) MN_MB?2O();
i, exanded to*if (l/_,tate_0) l/_,tate_1 = 1;
l/_,tate_7 = 0;
#define MN_MB?2O() {l/_,tate_1 = 1; l/_,tate_7 = 0;}
if (l/_,tate_0) MN_MB?2O();
i, exanded to*if (l/_,tate_0) {l/_,tate_1 = 1;
l/_,tate_7 = 0;};
*n (oth e2am5les rule A& is 4iolated *n the (ad e2am5le only the 'irst statement :l4NstateN1 3 1< is5art o' the i'structureB the second statement :l4NstateN! 3 < is e2ecuted unconditionally )e a0are
that the JgoodJ e2am5le is still not con'orming to the coding guidelinesB ne4ertheless the 5ro(lem
does not occur anymore
Background This is necessaryB i' the macro is called 0ithin a (ranch The second statement 0ould
(e unconditional
' i ti +.
-
5/19/2018 M730053b Training
64/81
SV P ED T DA Khosrau Heidary Coding Guidelines%#
're"rocessor irectives +.
'=5 Macros must not end wit* a ;'=5 Macros must not end wit* a ;
#$am"le )ad Good
#define >?_4OO 10;HHHx = >?_4OO P ?_4OO 10HHHx = >?_4OO P
-
5/19/2018 M730053b Training
65/81
SV P ED T DA Khosrau Heidary Coding Guidelines%$
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions %tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
&orm and (""earance
-
5/19/2018 M730053b Training
66/81
SV P ED T DA Khosrau Heidary Coding Guidelines%%
&orm and (""earance
%ummar!%ummar!
.5 6se a proper representation o expressions
.2 6se o%%on or%attin- rules9 +here applia*le
.# ;i%it identiier len-ths to #5 si-ns.4 Do not use identiiers +ith leadin- undersores
.8 Keep the o%plexity o expressions s%all
.$ Preer dei%al representation or inte-er onstants. Do not use hexadei%al
representation or ne-ati7e onstants. Do not use otal representation.
. The state%ents or%in- the *ody o an i9 else i9 else9 +hile9 do +hile or or
state%ent %ust al+ays *e enlosed in *raes
&orm and (""earance +.
-
5/19/2018 M730053b Training
67/81
SV P ED T DA Khosrau Heidary Coding Guidelines%&
&orm and (""earance +.
(=1 're8er decimal re"resentation 8or integer constants= o not use *e$adecimal re"resentation 8or negative constants= o not use octal re"resentation=
(=1 're8er decimal re"resentation 8or integer constants= o not use *e$adecimal re"resentation 8or negative constants= o not use octal re"resentation=
#$am"le , )ad Good
foo = 0xc';
ba = 017;
foo = 1;
ba = 17;
bla = 0xff;bla = 0;
The (ad e2am5le assigns an octal constant 1! :decimal 1< to (ar 0hich is 5ro(a(ly not 0hat 0as
intended
escri"tion Any integer constants must (e gi4en in decimalsB not in he2adecimalsB e2ce5t i' the
he2adecimal re5resentation im5ro4es the reada(ility and the 4alue is 5ositi4e :eg
2'''' or 2+
-
5/19/2018 M730053b Training
68/81
SV P ED T DA Khosrau Heidary Coding Guidelines%+
&orm and (""earance +3
(=1 're8er decimal re"resentation 8or integer constants= o not use *e$adecimal re"resentation 8or negative constants= o not use octal re"resentation=
(=1 're8er decimal re"resentation 8or integer constants= o not use *e$adecimal re"resentation 8or negative constants= o not use octal re"resentation=
#$am"le .
+"roblems wit*
negative *e$a-decimal con-
stants
)ad
,
-
5/19/2018 M730053b Training
69/81
SV P ED T DA Khosrau Heidary Coding Guidelines%.
C Coding Guidelines - Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions %tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
)ames"aces
-
5/19/2018 M730053b Training
70/81
SV P ED T DA Khosrau Heidary Coding Guidelines&
)ames"aces
%ummar!%ummar!
.5 6se an o+n na%e spae or not speiied ite%s
.2 Do not deine the sa%e identiier in inner and outer sope
.# Do not use the sa%e identiiers in dierent na%e spaes
.4 The delaration %ust exatly %ath the deinition
.8 6se delarations +ith external lin&a-e only on ile le7el
.$ Deine an identiier +ith external lin&a-e exatly one
)ames"aces +.
-
5/19/2018 M730053b Training
71/81
SV P ED T DA Khosrau Heidary Coding Guidelines&1
)ames"aces +.
)=, /se an own name s"ace 8or not s"eci8ied items)=, /se an own name s"ace 8or not s"eci8ied itemsescri"tion =or internal data se5arate names5aces shall (e usedB i' these data are not en
ca5sulated (y standardCmechanism9 *nsert the name o' the 5roducing instance to
any SWinternal data 0hich is not enca5sulated (y standardCmechanism An
*nstance may (e an aggregateB an element'ileB a C'unction or any other sco5eB the
data is (elonging to The naming rule 'or this instance may (e instances5eci'icPre'i2
sym(ols 'or meanings 0ith an en4ironmental descri5tion li8e9
Men4ironmental descri5tionRNNMmeaningR
Backgroud )y the distri(uted de4elo5ment o' SW the ris8 o' con'licting sym(ols gro0s SW 5artsrunning correctly aloneB crash 0hen com5iled together Some 8ey0ords li8e -EB
WA*TB -B -==B -KB E66-6 0ere used multi5ly 'or di''erent 5ur5oses
)ames"aces +3
-
5/19/2018 M730053b Training
72/81
SV P ED T DA Khosrau Heidary Coding Guidelines&!
)ames"aces +3
)=, /se an own name s"ace 8or not s"eci8ied items)=, /se an own name s"ace 8or not s"eci8ied items
#$am"le )ad Good
mod_aHc
.......fla l/_old_,tate; $%lobal data %$
mod_bHc.......fla l/_old_,tate; $%lobal data %$
mod_aHc
.......,tatic fla l/_old_,tate;
mod_bHc.......,tatic fla l/_old_,tate;__________________________________O2*
HHHfla l/_mod_a_old_,tate;
.......HHHfla l/_mod_b_old_,tate;
To store STATEn1 the local data ;VN-;DNSTATE is used con'licting in t0o modules To a4oid
con'licts the name s5ace can (e se5arated (y standardCmechanism :static< or i' not 5ossi(le (y
creating o0n names5aces manually
#$ercise +3
-
5/19/2018 M730053b Training
73/81
SV P ED T DA Khosrau Heidary Coding Guidelines&"
#$ercise +3
#$ercises on
7&unctions
7're"rocessor irectives
7&orm and (""earance
7)ames"aces
#$ercises on
7&unctions
7're"rocessor irectives
7&orm and (""earance
7)ames"aces
C Coding Guidelines - Overview
-
5/19/2018 M730053b Training
74/81
SV P ED T DA Khosrau Heidary Coding Guidelines
C Coding Guidelines Overview
Background
General Rules
Comments
ata and ata t!"es
O"erators and #$"ressions %tatements and Control %tructures
&unctions
're"rocessor irectives
&orm and (""earance )ames"aces
Tec*nical %olution
Tec*nical %olution M% :OR
-
5/19/2018 M730053b Training
75/81
SV P ED T DA Khosrau Heidary Coding Guidelines&$
Tec*nical %olution M% :OR
Enter here a certain e25ression to go toY
Possi(le entries9
6uleB eg XD1.X or Xd1.X Page num(erB eg X"$X
6ule category :T-C
-
5/19/2018 M730053b Training
76/81
SV P ED T DA Khosrau Heidary Coding Guidelines&%
+
Tec*nical %olution M% :OR +3
-
5/19/2018 M730053b Training
77/81
SV P ED T DA Khosrau Heidary Coding Guidelines&&
Creates a >S W-6D document containing
the result :Heading )ody< o' the selection
This selection 'ilters all 6ules o' Class A
that are not function criticaland
not checked by C!"int
Clic8 a rule
to Zum5 to
that rule
0ithin the
document
()
+
Tec*nical %olution %> ;ntranet
-
5/19/2018 M730053b Training
78/81
SV P ED T DA Khosrau Heidary Coding Guidelines&+
*tt"s9intranet=sba=siemensvdo=com'owertrainOrganiAation#lectronicsrivOrganiAation%o8twar
eevelo"guid"roc3.bitc-codingstart=as"
Related Rules
-
5/19/2018 M730053b Training
79/81
SV P ED T DA Khosrau Heidary Coding Guidelines&.
ocuments9
7&unction inlining9
Conce"t to allow inlining o8 librar! 8unctions on c-level
7?ibrar! and ObNect building
Rules *ow to build libraries and obNects7O#M %: integration
Rules *ow to integrate O#M %:
&eedbacks Comments
-
5/19/2018 M730053b Training
80/81
SV P ED T DA Khosrau Heidary Coding Guidelines+
(n! 8eedbacks< comments or im"rovements
Create message in
;M#% -K TCG
T*e #nd
-
5/19/2018 M730053b Training
81/81
SV P ED T DA Khosrau Heidary Coding Guidelines+1
T*ank !ou 8or !our attentionL