-
Notifications
You must be signed in to change notification settings - Fork 2
/
C++ Coding Standards.html
5594 lines (4324 loc) · 178 KB
/
C++ Coding Standards.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<HTML>
<HEAD>
<TITLE> C++ Coding Standard </TITLE>
</HEAD>
<BODY BGCOLOR=#ffffff>
<TABLE>
<TR><TD VALIGN=TOP>
<CENTER>
<H1>C++ Coding Standard </H1>
<H4>Last Modified: 2008-03-01</H4>
<A HREF="mailto:tmh@possibility.com">tmh@possibility.com</A> / <A HREF="http://www.possibility.com/Tmh">http://www.possibility.com/Tmh</A>
<P>
<TABLE WIDTH="65%">
<TR>
<TD>
<B>Using this Standard</B>. If you want to make a local copy
of this standard and use it as your own you are perfectly free
to do so. That's why I made it! If you find any errors or make any
improvements please email me the changes so I can merge them in.
I also have a programming blog at <A HREF="http://radio.weblogs.com/0103955/categories/stupidHumanProgramming/">
http://radio.weblogs.com/0103955/categories/stupidHumanProgramming/</A>
that is accidently interesting at times, as is my wiki at <A HREF="http://www.possibility.com/epowiki/Wiki.jsp">
http://www.possibility.com/epowiki/Wiki.jsp</A>. <BR>
</TD>
</TR>
</TABLE>
</CENTER>
<P>
<H1> Contents </H1>
To make comments on this page please see the new <A HREF="#disqus">Disqus comment section</A>.
Pferor was also nice enough to make a copy of this document available in <A HREF="c++_coding_standards.pdf">pdf format</A>.
<OL>
<LI> <A HREF="#intro"> <B>Introduction</B> </A>
<UL>
<LI> <A HREF="#important"> Standardization is Important </A>
<LI> <A HREF="#enforce"> Standards Enforcement </A>
<LI> <A HREF="#accept"> Accepting an Idea </A>
<LI> <A HREF="#phases"> 6 Phases of a Project <I>(joke)</I> </A>
<LI> <A HREF="#flow"> Flow Chart of Project Decision Making <I>(joke)</I> </A>
<LI> <A HREF="#leader"> On Leadership </A>
</UL>
<LI> <A HREF="#resources"> <B>Resources- Take a Look!</B> </A>
<UL>
<LI> <A HREF="#general"> General </A>
<LI> <A HREF="#book"> Book Recommendations </A>
</UL>
<LI> <A HREF="#names"> <B>Names</B> </A>
<UL>
<LI> <A HREF="#descriptive"> Make Names Fit </A>
<LI> <A HREF="#units"> Include Units in Names </A>
<LI> <A HREF="#noabbrev"> No All Upper Case Abbreviations </A>
<LI> <A HREF="#classnames"> Class Names </A>
<LI> <A HREF="#classlnames"> Class Library Names </A>
<LI> <A HREF="#methodsnames"> Method Names </A>
<LI> <A HREF="#attrnames"> Class Attribute Names </A>
<LI> <A HREF="#margnames"> Method Argument Names </A>
<LI> <A HREF="#stacknames"> Variable Names on the Stack </A>
<LI> <A HREF="#pnames"> Pointer Variables </A>
<LI> <A HREF="#rnames"> Reference Variables and Functions
Returning References</A>
<LI> <A HREF="#gnames"> Global Variables </A>
<LI> <A HREF="#gconstants"> Global Constants </A>
<LI> <A HREF="#snames"> Static Variables </A>
<LI> <A HREF="#tnames"> Type Names </A>
<LI> <A HREF="#enames"> Enum Names </A>
<LI> <A HREF="#mnames"> #define and Macro Names </A>
<LI> <A HREF="#cnames"> C Function Names </A>
<LI> <A HREF="#fext"> C++ File Extensions </A>
</UL>
<LI> <A HREF="#doc"><B>Documentation</B></A>
<UL>
<LI> <A HREF="#cstas"> Comments Should Tell a Story </A>
<LI> <A HREF="#cdd"> Document Decisions </A>
<LI> <A HREF="#cuh"> Use Extractable Headers </A>
<LI> <A HREF="#caq"> Comment All Questions a Programmer May Have When Looking at Your Code </A>
<LI> <A HREF="#mycd"> Make Your Code Discoverable by Browsing </A>
<LI> <A HREF="#wcyc"> Write Comments as You Code </A>
<LI> <A HREF="#mge"> Make Gotchas Explicit </A>
<LI> <A HREF="#two"> Interface and Implementation Documentation </A>
<LI> <A HREF="#dirdoc"> Directory Documentation </A>
<LI> <A HREF="#idoc"> Include Statement Documentation </A>
<LI> <A HREF="#blockdoc"> Block Comments </A>
</UL>
<LI> <A HREF="#comman"><B>Complexity Management</B></A>
<UL>
<LI> <A HREF="#layering"> Layering </A>
<LI> <A HREF="#abstract"> Minimize Dependencies with Abstract
Base Classes </A>
<LI> <A HREF="#liskov"> Liskov's Substitution Prinicple </A>
<LI> <A HREF="#open"> Open/Closed Principle </A>
<LI> <A HREF="#register"> Register/Dispatch Idiom </A>
<LI> <A HREF="#delegation"> Delegation </A>
<LI> <A HREF="#demeter"> Follow the Law of Demeter </A>
<LI> <A HREF="#contract"> Design by Contract </A>
</UL>
<LI> <A HREF="#classes"><B>Classes</B></A>
<UL>
<LI> <A HREF="#cflayout"> Naming Class Files </A>
<LI> <A HREF="#classlayout"> Class Layout </A>
<LI> <A HREF="#pubpropri"> What should go in public/protected/private? </A>
<LI> <A HREF="#cfile"> Prototype Source File </A>
<LI> <A HREF="#guards"> Use Header File Guards </A>
<LI> <A HREF="#req"> Required Methods for a Class </A>
<LI> <A HREF="#mlayout"> Method Layout </A>
<LI> <A HREF="#multimethods"> Formating Methods with Multiple Arguments </A>
<LI> <A HREF="#accessor"> Different Accessor Styles </A>
<LI> <A HREF="#init"> Init Idiom for Initializing Objects </A>
<LI> <A HREF="#initvar"> Initialize all Variables </A>
<LI> <A HREF="#mininlines"> Minimize Inlines </A>
<LI> <A HREF="#work"> Think About What Work to do in Constructors </A>
<LI> <A HREF="#nooper"> Don't Over Use Operators </A>
<LI> <A HREF="#thinfat"> Thin vs. Thick Class Interfaces </A>
<LI> <A HREF="#shortmethods"> Short Methods </A>
<LI> <A HREF="#msv"> In a Source file Indicate if a Method is Static or Virtual </A>
</UL>
<LI> <A HREF="#process"><B>Process</B></A>
<UL>
<LI> <A HREF="#design"> Use a Design Notation and Process </A>
<LI> <A HREF="#usecases"> Using Use Cases </A>
<LI> <A HREF="#stories"> Using Stories </A>
<LI> <A HREF="#uml"> Unified Modeling Language </A>
<LI> <A HREF="#codereview"> Code Reviews </A>
<LI> <A HREF="#getsome"> Create a Source Code Control System Early and Not Often </A>
<LI> <A HREF="#bugs"> Create a Bug Tracking System Early and Not Often </A>
<LI> <A HREF="#wiki"> Create a Wiki System Early and Not Often </A>
<LI> <A HREF="#rcs"> RCS Keyword, Change Log, and History Policy</A>
<LI> <A HREF="#resp"> Honor Responsibilities </A>
<LI> <A HREF="#autocheck"> Process Automation </A>
<LI> <A HREF="#tools"> Tools Agreement </A>
<LI> <A HREF="#sched"> Non-Blocking Scheduling </A>
<LI> <A HREF="#personas"> Using Personas</A>
<LI> <A HREF="#cobld"> Use a Continuous Build System</A>
<LI> <A HREF="#domstlye"> Code in the Dominant Style</A>
<LI> <A HREF="#runut"> Run Unit Tests Before Every Check-in</A>
</UL>
<LI> <A HREF="#formatting"><B>Formatting</B></A>
<UL>
<LI> <A HREF="#brace"> Brace <I>{}</I> Policy </A>
<LI> <A HREF="#indent"> Indentation/Tabs/Space Policy </A>
<LI> <A HREF="#parens"> Parens <I>()</I> with Key Words and
Functions Policy </A>
<LI> <A HREF="#linelen"> A Line Should Not Exceed 78 Characters </A>
<LI> <A HREF="#ifthen"> <I>If Then Else</I> Formatting </A>
<LI> <A HREF="#switch"> <I>switch</I> Formatting </A>
<LI> <A HREF="#goto"> Use of <I>goto,continue,break</I> and <I>?:</I> </A>
<LI> <A HREF="#one"> One Statement Per Line </A>
<LI> <A HREF="#aligndecls"> Alignment of Declaration Blocks </A>
<LI> <A HREF="#docnull"> Document Null Statements </A>
<LI> <A HREF="#incvs"> Include <I>static</I> and <I>virtual</I> Key Words in Source File </A>
</UL>
<LI> <A HREF="#ex"><B>Exceptions</B></A>
<UL>
<LI> <A HREF="#coeeal"> Create One Exception for Each Library </A>
<LI> <A HREF="#sbea"> Selecting Between Exceptions And Asserts </A>
<LI> <A HREF="#destexcept"> Be Careful Throwing Exceptions in Destructors </A>
<LI> <A HREF="#wwluas"> Working With Libaries Using All Sorts of Different Policies </A>
</UL>
<LI> <A HREF="#templates"><B>Templates</B></A>
<LI> <A HREF="#namespaces"><B>Namespaces</B></A>
<UL>
<LI> <A HREF="#cun"> Create Unique Name Space Names </A>
<LI> <A HREF="#dgdu"> Don't Globally Define <I>using</I> </A>
<LI> <A HREF="#csn"> Create Shortcut Names </A>
</UL>
<LI> <A HREF="#misc"><B>Miscellaneous</B></A>
<UL>
<LI> <A HREF="#const"> Be Const Correct </A>
<LI> <A HREF="#constplace"> Placement of the Const Qualifier</A>
<LI> <A HREF="#streams"> Use Streams </A>
<LI> <A HREF="#nomagic"> No Magic Numbers
<LI> <A HREF="#errorret"> Error Return Check Policy
<LI> <A HREF="#useenums"> To Use Enums or Not to Use Enums </A>
<LI> <A HREF="#macros"> Macros </A>
<LI> <A HREF="#nztest"> Do Not Default If Test to Non-Zero </A>
<LI> <A HREF="#boolean"> The Bull of Boolean Types </A>
<LI> <A HREF="#eassign"> Usually Avoid Embedded Assignments </A>
<LI> <A HREF="#reuse"> Reusing Your Hard Work and the Hard Work of Others</A>
<LI> <A HREF="#if0"> Commenting Out Large Code Blocks </A>
<LI> <A HREF="#ifdef"> Use #if Not #ifdef </A>
<LI> <A HREF="#cacf"> Creating a C Function in C++ </A>
<LI> <A HREF="#callc"> Mixing C and C++ </A>
<LI> <A HREF="#nodef"> No Data Definitions in Header Files </A>
<LI> <A HREF="#reentrant"> Make Functions Reentrant </A>
<LI> <A HREF="#raii"> Use the Resource Acquisition is Initialization (RAII) Idiom </A>
<LI> <A HREF="#rtws"> Remove Trailing Whitespace </A>
</UL>
<LI> <A HREF="#port"><B>Portability</B></A>
<UL>
<LI> <A HREF="#usetypedefs"> Use Typedefs for Types </A>
<LI> <A HREF="#alignment"> Alignment of Class Members </A>
<LI> <A HREF="#cdexcept"> Compiler Dependent Exceptions </A>
<LI> <A HREF="#cdrtti"> Compiler Dependent RTTI </A>
</UL>
<LI> <A HREF="#popm">Popular Myths</A>
<UL>
<LI> <A HREF="#promiss"> Promise of OO </A>
<LI> <A HREF="#embedded"> You can't use OO and C++ on Embedded Systems </A>
</UL>
</OL>
<P> <HR> <A NAME="intro"></A>
<H1> Introduction </H1>
<A NAME="important"></A>
<H2> Standardization is Important </H2>
It helps if the standard annoys everyone in some way so everyone
feels they are on the same playing field. The proposal here
has evolved over many projects, many companies, and literally
a total of many weeks spent arguing. It is no particular person's style
and is certainly open to local amendments.
<H3> Good Points </H3>
When a project tries to adhere to common standards a few
good things happen:
<UL>
<LI> Programmers can go into any code and figure out what's going on.
<LI> New people can get up to speed quickly.
<LI> People new to C++ are spared the need to develop a personal
style and defend it to the death.
<LI> People new to C++ are spared making the same mistakes over
and over again.
<LI> People make fewer mistakes in consistent environments.
<LI> Programmers have a common enemy :-)
</UL>
<H3> Bad Points </H3>
Now the bad:
<UL>
<LI> The standard is usually stupid because it was made by someone
who doesn't understand C++.
<LI> The standard is usually stupid because it's not what I do.
<LI> Standards reduce creativity.
<LI> Standards are unnecessary as long as people are consistent.
<LI> Standards enforce too much structure.
<LI> People ignore standards anyway.
<LI> Standards can be used as a reason for NIH (not invented here)
because the new/borrowed code won't follow the standard.
</UL>
<H3> Discussion </H3>
The experience of many projects leads to the conclusion that using
coding standards makes the project go smoother. Are standards
necessary for success? Of course not. But they help, and we need all the
help we can get! Be honest, most arguments against a particular
standard come from the ego. Few decisions in a reasonable standard really
can be said to be technically deficient, just matters of taste.
So be flexible, control the ego a bit, and remember any project
is fundamentally a team effort.
<P>
<P><A NAME="enforce"></A>
<H2> Standards Enforcement </H2>
First, any serious concerns about the standard should be brought
up and worked out within the group. Maybe the standard is not
quite appropriate for your situation. It may have overlooked
important issues or maybe someone in power vehemently
disagrees with certain issues :-)
<P>
In any case, once finalized hopefully people will play the adult and
understand that this standard is reasonable, and has been found reasonable
by many other programmers, and therefore is worthy of being followed
even with personal reservations.
<P>
Failing willing cooperation it can be made a requirement that
this standard must be followed to pass a code inspection.
<P>
Failing that the only solution is a massive tickling party on the
offending party.
<P>
<A NAME="accept"></A>
<H2> Accepting an Idea </H2>
<OL>
<LI> It's impossible.
<LI> Maybe it's possible, but it's weak and uninteresting.
<LI> It is true and I told you so.
<LI> I thought of it first.
<LI> How could it be otherwise.
</OL>
If you come to objects with a negative preconception please
keep an open mind. You may still conclude objects are bunk,
but there's a road you must follow to accept something different.
Allow yourself to travel it for a while.
<A NAME="phases"></A>
<H2> 6 Phases of a Project </H2>
<OL>
<LI> Enthusiasm
<LI> Disillusionment
<LI> Panic
<LI> A Search for the Guilty
<LI> The Punishment of the Innocent
<LI> Praise and Honor for the Non-Participants
</OL><P>
<A NAME="flow"></A>
<H2> Flow Chart for Project Decision Making </H2>
<PRE>
+---------+
| START |
+---------+
|
V
YES +------------+ NO
+---------------| DOES THE |---------------+
| | DAMN THING | |
V | WORK? | V
+------------+ +------------+ +--------------+ NO
| DON'T FUCK | | DID YOU FUCK |-----+
| WITH IT | | WITH IT? | |
+------------+ +--------------+ |
| | |
| | YES |
| V |
| +------+ +-------------+ +---------------+ |
| | HIDE | NO | DOES ANYONE |<------| YOU DUMBSHIT! | |
| | IT |<----| KNOW? | +---------------+ |
| +------+ +-------------+ |
| | | |
| | V |
| | +-------------+ +-------------+ |
| | | YOU POOR | YES | WILL YOU | |
| | | BASTARD |<------| CATCH HELL? |<-----+
| | +-------------+ +-------------+
| | | |
| | | | NO
| | V V
| V +-------------+ +------------+
+-------------->| STOP |<------| SHITCAN IT |
+-------------+ +------------+
</PRE>
<A NAME="leader">
<H2> Leadership </H2>
I wish i had said this, but it was said by asd@asd.com in comp.software-eng.
<P>
<B>Leaders:</B>
<OL>
<LI> lead by example
<LI> don't ask anything of anyone they wouldn't do themselves
<LI> are called on to make difficult and unpopular decisions
<LI> keep the team focused
<LI> reward/support their team in whatever they do
<LI> keep/clear unnecessary crap out of the way of the team
</OL>
Consensus is great. If it lasts for the project lifecycle,
consider yourself blessed. I've been on a couple projects
where two engineers just blantantly <B>disagreed</B>!
They were always:
<P>
Programmer #1 says " x = 1" <BR>
Programmer #2 says " x != 1"
<P>
That's when a Project Leader is required. Unless you
want to flip a coin.
<P>
Oh yah - one more thing. Project leaders: TAKE the blame
when things go wrong and SHARE the credit when things
go right.
<P>
Ain't easy - but it's the way I try to run my life.
<P>
<P><HR><A NAME="resources"></A>
<H1> Resources- Take a Look! </H1>
<A NAME="general"></A>
<H2> General </H2>
<UL>
<LI> <A HREF="http://www.possibility.com/epowiki/Wiki.jsp?page=CodeReviews">Code Reviews</A>. If
you are interested in coding standards you may also be interested in Code Review Standards I
have created at <A HREF="http://www.possibility.com/epowiki/Wiki.jsp?page=CodeReviews">
http://www.possibility.com/epowiki/Wiki.jsp?page=CodeReviews</A>.
<LI> <A HREF="http://www.possibility.com/Cpp/DesignStories.html">
Design Stories </A>
<LI> <A HREF="http://www.sente.ch/cetus/software.html">
OO Info Sources </A>
<LI> <A HREF="http://doc.trolltech.com/qq/qq13-apis.html">
Designing Qt-Style C++ APIs </A> - A good paper on API I design for C++. I don't agree with everything, but it is good.
<LI> <A HREF="http://www.rational.com/uml/index.jtmpl">
Unified Modeling Language (UML) </A>
<LI> <A HREF="http://www.cyberdyne-object-sys.com/">
OO FAQ - All You Wanted to Know About OO </A>
<LI> <A HREF="http://www.parashift.com/c++-faq-lite/">
C++ FAQ LITE </A>
<LI> <A HREF="http://www.desy.de/user/projects/C++/Projects.html">
C++ Source Libraries </A>
<LI> <A HREF="http://www.desy.de/user/projects/C++/Learning.html">
C++ Tutorials </A>
<LI> <A HREF="http://www.cs.wustl.edu/~schmidt/ACE-overview.html"> ACE C++ Library </A>
<LI> <A HREF="http://www.cs.umd.edu/users/cml/cstyle"> Collection of Other Standards </A>
<LI> <A HREF="http://www.eiffel.com/doc/manuals/technology/contract/"> Design by Contract from Eiffle </A>
<LI> <A HREF="http://www.progsoc.uts.edu.au/~geldridg/cpp/cppcv3.html">
C++ isn't Perfect, Here are Some Reasons Why </A>
<LI> <A HREF="http://www.doxygen.org/">Doxygen</A> -
is a 'javadoc' like utility that extracts comments and
relevant information from your C++/C programs and generates HTML
pages from it.
<LI> <A HREF="http://www.possibility.com/Cpp/const.html">
Const Correctness </A> - A very nice article on const correctness
by Chad Loder.
<LI> <A HREF="http://www.abxsoft.com">Abraxis Code Check</A> -
A program for checking code for coding standard violations and
other problems.
</UL>
<A NAME="book"></A>
<H2> Book Recommendations </H2>
What are some good C++ books you can buy for you and your team?
<OL>
<LI> Koenig/Moo's "Accelerated C++"
<LI> Lippman/Moo's "C++ Primer" 4th Edition
<LI> Bruce Eckel's "Thinking In C++"
<LI> Scott Meyers "Effective C++"
<LI> Dewhurst's "C++ Gotchas"
<LI> Meyers' "Effective STL"
<LI> Josuttis' "The C++ Standard Library"
<LI> Vandevoorde/Josuttis' "C++ Templates"
<LI> Langer/Kreft's "Standard C++ IOStreams and Locales"
<LI> Sutter's "Exceptional C++"
<LI> Sutter's "More Exceptional C++ and Exceptional C++ Style"
<LI> Martin's "Agile Software Development: Principles, Patterns, and Practices"
</OL>
<P><HR><HR><A NAME="names"></A>
<H1> Names </H1>
<P><HR><A NAME="descriptive">
<H1> Make Names Fit </H1>
Names are the heart of programming. In the
past people believed knowing someone's true name gave them magical
power over that person. If you can think up the true name for something,
you give yourself and the people coming after power over the code.
Don't laugh! <P>
A name is the result of a long deep thought process about
the ecology it lives in. Only a programmer who understands the system as a whole
can create a name that "fits" with the system. If the name is appropriate
everything fits together naturally, relationships are clear, meaning is derivable,
and reasoning from common human expectations works as expected. <P>
If you find all your names could be Thing and DoIt then you should probably
revisit your design. <P
<H3> <B>Class Names</B> </H3>
<UL>
<LI> Name the class after what it is. If you can't think of what it is that is a clue
you have not thought through the design well enough.
<LI> Compound names of over three words are a clue your design may be confusing
various entities in your system. Revisit your design. Try a CRC card session
to see if your objects have more responsibilities than they should.
<LI> Avoid the temptation of bringing the name of the class a class derives from into the
derived class's name. A class should stand on its own. It doesn't
matter what it derives from.
<LI> Suffixes are sometimes helpful. For example, if your system uses agents then naming
something DownloadAgent conveys real information.
</UL>
<H3> Method and Function Names </H3>
<UL>
<LI>
Usually every method and function performs an action, so the name
should make clear what it does: CheckForErrors()
instead of ErrorCheck(), DumpDataToFile() instead of DataFile().
This will also make functions and data objects more distinguishable.
<P>
Classes are often nouns. By making function names verbs and following
other naming conventions programs can be read more naturally.
<P>
<LI>
Suffixes are sometimes useful:
<UL>
<LI> <I>Max</I> - to mean the maximum value something can have.
<LI> <I>Cnt</I> - the current count of a running count variable.
<LI> <I>Key</I> - key value.
</UL><P>
For example: RetryMax to mean the maximum number of retries, RetryCnt to
mean the current retry count.
<P>
<LI>
Prefixes are sometimes useful:
<UL>
<LI> <I>Is</I> - to ask a question about something. Whenever someone sees <I>Is</I> they
will know it's a question.
<LI> <I>Get</I> - get a value.
<LI> <I>Set</I> - set a value.
</UL><P>
For example: IsHitRetryLimit.
<P>
</UL>
<P><HR><A NAME="units">
<H1> Include Units in Names </H1>
If a variable represents time, weight, or some other unit then include the
unit in the name so developers can more easily spot problems.
For example:
<PRE>
uint32 mTimeoutMsecs;
uint32 mMyWeightLbs;
</PRE>
Better yet is to make a variable into a class so bad conversions
can be caught.
<P><HR><A NAME="noabbrev">
<H1> No All Upper Case Abbreviations </H1>
<UL>
<LI> When confronted with a situation where you could use an all
upper case abbreviation instead use an initial upper case
letter followed by all lower case letters. No matter what.
</UL>
<H3> Justification </H3>
<UL>
<LI> People seem to have very different intuitions when making names
containing abbreviations. It's best to settle on one strategy
so the names are absolutely predictable.
<P>
Take for example <I>NetworkABCKey</I>. Notice
how the C from ABC and K from key are confused. Some people don't
mind this and others just hate it so you'll find different
policies in different code so you never know what to call something.
</UL>
<H3> Example </H3>
<PRE>
class FluidOz // NOT FluidOZ
class NetworkAbcKey // NOT NetworkABCKey
</PRE>
<P><HR><A NAME="classnames"></A>
<H1> Class Names </H1>
<UL>
<LI> Use upper case letters as word separators, lower case for the rest of a word
<LI> First character in a name is upper case
<LI> No underbars ('_')
</UL>
<H3> Justification </H3>
<UL>
<LI> Of all the different naming strategies many people found this one the
best compromise.
</UL>
<H3> Example </H3>
<PRE>
class NameOneTwo
class Name
</PRE>
<P><HR><A NAME="classlnames">
<H1> Class Library Names </H1>
<UL>
<LI> Now that name spaces are becoming more widely implemented, name spaces
should be used to prevent class name conflicts among libraries
from different vendors and groups.
<LI> When not using name spaces, it's common to prevent class name clashes by
prefixing class names with a unique string. Two characters is sufficient,
but a longer length is fine.
</UL>
<H3> Example </H3>
John Johnson's complete data structure library could use <I>JJ</I>
as a prefix, so classes would be:
<PRE>
class JjLinkList
{
}
</PRE>
<P><HR><A NAME="methodsnames">
<H1> Method Names </H1>
<UL>
<LI> Use the same rule as for class names.
</UL>
<H3> Justification </H3>
<UL>
<LI> Of all the different naming strategies many people found this one the
best compromise.
</UL>
<H3> Example </H3>
<PRE>
class NameOneTwo
{
public:
int DoIt();
void HandleError();
}
</PRE>
<P><HR><A NAME="attrnames"></A>
<H1> Class Attribute Names </H1>
<UL>
<LI> Attribute names should be prepended with the character 'm'.
<LI> After the 'm' use the same rules as for class names.
<LI> 'm' always precedes other name modifiers like 'p' for pointer.
</UL>
<H3> Justification </H3>
<UL>
<LI> Prepending 'm' prevents any conflict with method names. Often your
methods and attribute names will be similar, especially for
accessors.
</UL>
<H3> Example </H3>
<PRE>
class NameOneTwo
{
public:
int VarAbc();
int ErrorNumber();
private:
int mVarAbc;
int mErrorNumber;
String* mpName;
}
</PRE>
<P><HR><A NAME="margnames"></A>
<H1> Method Argument Names </H1>
<UL>
<LI> The first character should be lower case.
<LI> All word beginnings after the first letter should be upper case
as with class names.
</UL>
<H3> Justification </H3>
<UL>
<LI> You can always tell which variables are passed in variables.
<LI> You can use names similar to class names without conflicting with class names.
</UL>
<H3> Example </H3>
<PRE>
class NameOneTwo
{
public:
int StartYourEngines(
Engine& rSomeEngine,
Engine& rAnotherEngine);
}
</PRE>
<P><HR><A NAME="stacknames">
<H1> Variable Names on the Stack </H1>
<UL>
<LI> use all lower case letters
<LI> use '_' as the word separator.
</UL>
<H3> Justification </H3>
<UL>
<LI> With this approach the scope of the variable is clear in
the code.
<LI> Now all variables look different and are identifiable in the code.
</UL>
<H3> Example </H3>
<PRE>
int
NameOneTwo::HandleError(int errorNumber)
{
int error= OsErr();
Time time_of_error;
ErrorProcessor error_processor;
Time* p_out_of_time= 0;
}
</PRE>
<P>
The standard pointer notation is not entirely satisfactory because it doesn't look quite
right, but it is consistent.
<P>
How do you handle statics? There's never a reason to have a static local to
a function so there's no reason to invent a syntax for it. But like for most
absolute rules, there is an exception, that is when making singletons. Use a "s_" prefix
in this case.
Take a look at <A HREF="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton Pattern</A>
for more details.
<P>
<P><HR><A NAME="pnames"></A>
<H1> Pointer Variables </H1>
<UL>
<LI> pointers should be prepended by a 'p' in most cases
<LI> place the <I>*</I> close to the pointer type not the variable name
</UL>
<H3> Justification </H3>
<UL>
<LI> The idea is that the difference between a pointer, object, and a
reference to an object is important for understanding the code,
especially in C++ where <I>-></I> can be overloaded, and casting
and copy semantics are important.
<LI>
Pointers really are a change of type so the <I>*</I> belongs near the type.
One reservation with this policy relates to declaring multiple variables
with the same type on the same line. In C++ the pointer modifier only applies
to the closest variable, not all of them, which can be very confusing,
especially for newbies. You want to have one declaration per line anyway
so you can document each variable.
</UL>
<H3> Example </H3>
<PRE>
String* pName= new String;
String* pName, name, address; // note, only pName is a pointer.
</PRE>
<P><HR><A NAME="rnames">
<H1> Reference Variables and Functions Returning References</H1>
<UL>
<LI> References should be prepended with 'r'.
</UL>
<H3> Justification </H3>
<UL>
<LI> The difference between variable types is clarified.
<LI> It establishes the difference between a method returning a
modifiable object and the same method name returning a
non-modifiable object.
</UL>
<H3> Example </H3>
<PRE>
class Test
{
public:
void DoSomething(StatusInfo& rStatus);
StatusInfo& rStatus();
const StatusInfo& Status() const;
private:
StatusInfo& mrStatus;
}
</PRE>
<P><HR><A NAME="gnames">
<H1> Global Variables </H1>
<UL>
<LI> Global variables should be prepended with a 'g'.
</UL>
<H3> Justification </H3>
<UL>
<LI> It's important to know the scope of a variable.
</UL>
<H3> Example </H3>
<PRE>
Logger gLog;
Logger* gpLog;
</PRE>
<P><HR><A NAME="gconstants">
<H1> Global Constants </H1>
<UL>
<LI> Global constants should be all caps with '_' separators.
</UL>
<H3> Justification </H3>
It's tradition for global constants to named this way.
You must be careful to not conflict with other global
<I>#define</I>s and enum labels.
<H3> Example </H3>
<PRE>
const int A_GLOBAL_CONSTANT= 5;
</PRE>
<P><HR><A NAME="snames"></A>
<H1> Static Variables </H1>
<UL>
<LI> Static variables may be prepended with 's'.
</UL>
<H3> Justification </H3>
<UL>
<LI> It's important to know the scope of a variable.
</UL>
<H3> Example </H3>
<PRE>
class Test
{
public:
private:
static StatusInfo msStatus;
}
</PRE>
<P><HR><A NAME="tnames"></A>
<H1> Type Names </H1>
<UL>
<LI> When possible for types based on native types make a typedef.
<LI> Typedef names should use the same naming policy as for a class
with the word <I>Type</I> appended.
</UL>
<H3> Justification </H3>
<UL>
<LI> Of all the different naming strategies many people found this one the
best compromise.
<LI> Types are things so should use upper case letters. <I>Type</I>
is appended to make it clear this is not a class.
</UL>
<H3> Example </H3>
<PRE>
typedef uint16 ModuleType;
typedef uint32 SystemType;
</PRE>
<P><HR><A NAME="enames">
<H1> Enum Names </H1>
<H2> Labels All Upper Case with '_' Word Separators </H2>
This is the standard rule for enum labels.
<H3> Example </H3>
<PRE>
enum PinStateType
{
PIN_OFF,
PIN_ON
};
</PRE>
<H2> Enums as Constants without Class Scoping </H2>
Sometimes people use enums as constants. When an enum is not
embedded in a class make sure you use some sort of differentiating
name before the label so as to prevent name clashes.
<H3> Example </H3>
<PRE>
enum PinStateType If <I>PIN</I> was not prepended a conflict
{ would occur as OFF and ON are probably
PIN_OFF, already defined.
PIN_ON
};
</PRE>
<H2> Enums with Class Scoping </H2>
Just name the enum items what you wish and always qualify
with the class name: Aclass::PIN_OFF.
<H2> Make a Label for an Error State </H2>
It's often useful to be able to say an enum is not
in any of its <I>valid</I> states. Make a label for
an uninitialized or error state. Make it the first label
if possible.
<H3> Example </H3>
<PRE>
enum { STATE_ERR, STATE_OPEN, STATE_RUNNING, STATE_DYING};
</PRE>
<P><HR><A NAME="mnames">
<H1> #define and Macro Names </H1>
<UL>
<LI> Put #defines and macros in all upper using '_' separators.
</UL>
<H3> Justification </H3>
This makes it very clear that the value is not
alterable and in the case of macros, makes it clear that you are using a
construct that requires care. <P>
Some subtle errors can occur when macro names and enum labels use the
same name.
<H3> Example </H3>
<PRE>
#define MAX(a,b) blah
#define IS_ERR(err) blah
</PRE>