-
Notifications
You must be signed in to change notification settings - Fork 18
/
naming_formatting.html
1367 lines (1268 loc) · 61.7 KB
/
naming_formatting.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 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcq="http://purl.org/dc/qualifiers/1.0/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<HEAD>
<TITLE>ALICE O² C++ Naming & Formatting Rules</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK HREF="http://www.google.com/favicon.ico" type="image/x-icon" rel="shortcut icon">
<LINK HREF="styleguide.css" type="text/css" rel="stylesheet">
<SCRIPT language="javascript" type="text/javascript">
function GetElementsByName(name) {
// Workaround a bug on old versions of opera.
if (document.getElementsByName) {
return document.getElementsByName(name);
} else {
return [document.getElementById(name)];
}
}
/**
* @param {string} namePrefix The prefix of the body name.
* @param {function(boolean): boolean} getVisibility Computes the new
* visibility state, given the current one.
*/
function ChangeVisibility(namePrefix, getVisibility) {
var bodyName = namePrefix + '__body';
var buttonName = namePrefix + '__button';
var bodyElements = GetElementsByName(bodyName);
var linkElement = GetElementsByName('link-' + buttonName)[0];
if (bodyElements.length != 1) {
throw Error('ShowHideByName() got the wrong number of bodyElements: ' +
bodyElements.length);
} else {
var bodyElement = bodyElements[0];
var buttonElement = GetElementsByName(buttonName)[0];
var isVisible = bodyElement.style.display != "none";
if (getVisibility(isVisible)) {
bodyElement.style.display = "inline";
linkElement.style.display = "block";
buttonElement.innerHTML = '▽';
} else {
bodyElement.style.display = "none";
linkElement.style.display = "none";
buttonElement.innerHTML = '▶';
}
}
}
function ShowHideByName(namePrefix) {
ChangeVisibility(namePrefix, function(old) { return !old; });
}
function ShowByName(namePrefix) {
ChangeVisibility(namePrefix, function() { return true; });
}
function ShowHideAll() {
var allButton = GetElementsByName("show_hide_all_button")[0];
if (allButton.innerHTML == '▽') {
allButton.innerHTML = '▶';
SetHiddenState(document.getElementsByTagName("body")[0].childNodes, "none", '▶');
} else {
allButton.innerHTML = '▽';
SetHiddenState(document.getElementsByTagName("body")[0].childNodes, "inline", '▽');
}
}
// Recursively sets state of all children
// of a particular node.
function SetHiddenState(root, newState, newButton) {
for (var i = 0; i != root.length; i++) {
SetHiddenState(root[i].childNodes, newState, newButton);
if (root[i].className == 'showhide_button') {
root[i].innerHTML = newButton;
}
if (root[i].className == 'stylepoint_body' ||
root[i].className == 'link_button') {
root[i].style.display = newState;
}
}
}
function EndsWith(str, suffix) {
var l = str.length - suffix.length;
return l >= 0 && str.indexOf(suffix, l) == l;
}
function RefreshVisibilityFromHashParam() {
var hashRegexp = new RegExp('#([^&#]*)$');
var hashMatch = hashRegexp.exec(window.location.href);
var anchor = hashMatch && GetElementsByName(hashMatch[1])[0];
var node = anchor;
var suffix = '__body';
while (node) {
var id = node.id;
var matched = id && EndsWith(id, suffix);
if (matched) {
var len = id.length - suffix.length;
ShowByName(id.substring(0, len));
if (anchor.scrollIntoView) {
anchor.scrollIntoView();
}
return;
}
node = node.parentNode;
}
}
window.onhashchange = RefreshVisibilityFromHashParam;
window.onload = function() {
// if the URL contains "?showall=y", expand the details of all children
var showHideAllRegex = new RegExp("[\\?&](showall)=([^&#]*)");
var showHideAllValue = showHideAllRegex.exec(window.location.href);
if (showHideAllValue != null) {
if (showHideAllValue[2] == "y") {
SetHiddenState(document.getElementsByTagName("body")[0].childNodes,
"inline", '▽');
} else {
SetHiddenState(document.getElementsByTagName("body")[0].childNodes,
"none", '▶');
}
}
var showOneRegex = new RegExp("[\\?&](showone)=([^&#]*)");
var showOneValue = showOneRegex.exec(window.location.href);
if (showOneValue) {
ShowHideByName(showOneValue[2]);
}
RefreshVisibilityFromHashParam();
}
</SCRIPT>
</HEAD>
<BODY>
<H1>ALICE O² C++ Naming & Formatting Rules</H1>
<address>
Vasco Barroso<br>
Alina GrigoraȘ<br>
Ivana Hřivnáčová<br>
Matthias Kretz<br>
Adriana Telesca<br>
Barthélémy von Haller<br>
</address>
<p align="right">
This document is based on the work of <br>
B. Weinberger, C. Silverstein, <br>
G. Eitzmann, M. Mentovai <br>
and T.Landray <br>
at <a href="http://google-styleguide.googlecode.com"> http://google-styleguide.googlecode.com</a>, <br>
C++ Google Style guide, Revision 3.245 <br>
under the <a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US">CC-By 3.0 License</a>
</p>
<DIV style="margin-left: 50%; font-size: 75%;">
<P>
Each style point has a summary for which additional information is available
by toggling the accompanying arrow button that looks this way:
<SPAN class="showhide_button" style="margin-left: 0; float: none">▶</SPAN>.
You may toggle all summaries with the big arrow button:
</P>
<DIV style=" font-size: larger; margin-left: +2em;">
<SPAN class="showhide_button" style="font-size: 180%; float: none" onclick="javascript:ShowHideAll()" name="show_hide_all_button" id="show_hide_all_button">▶</SPAN>
Toggle all summaries
</DIV>
</DIV>
<DIV class="toc">
<DIV class="toc_title">Table of Contents</DIV>
<TABLE>
<TR valign="top" class="">
<TD><DIV class="toc_category"><A href="#Naming">Naming</A></DIV></TD>
<TD><DIV class="toc_stylepoint">
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#General_Naming_Rules">General Naming Rules</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#File_Names">Code File Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Executable_Names">Executable Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Library_Names">Library Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Type_Names">Type Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Interface_Names">Interface Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Variable_Names">Variable Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Function_Names">Function Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Namespace_Names">Namespace Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Enumerator_Names">Enumerator Names</A></SPAN>
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Macro_Names">Macro Names</A></SPAN>
</DIV>
</TD>
</TR>
<TR valign="top" class="">
<TD><DIV class="toc_category"><A href="#Formatting">Formatting</A></DIV></TD>
<TD><DIV class="toc_stylepoint">
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Line_Length">Line Length</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#One_Statement_Per_Line">One Statement Per Line</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Spaces_vs._Tabs">Spaces vs. Tabs</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Function_Declarations_and_Definitions">Function Declarations and Definitions</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Pointer_and_Reference_Expressions">Pointer and Reference Expressions</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Boolean_Expressions">Boolean Expressions</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Variable_and_Array_Initialization">Variable and Array Initialization</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Preprocessor_Directives">Preprocessor Directives</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Classes">Classes</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Constructor_Initializer_Lists">Constructor Initializer Lists</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Namespaces">Namespaces</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Braces">Braces</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Horizontal_Whitespace">Horizontal Whitespace</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Vertical_Whitespace">Vertical Whitespace</A></SPAN> <SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Where_to_put_const">Where to put const</A></SPAN> </DIV></TD>
</TR>
<TR valign="top" class="">
<TD><DIV class="toc_category"><A href="#Exceptions_to_the_Rules">Exceptions to the Rules</A></DIV></TD>
<TD><DIV class="toc_stylepoint">
<SPAN style="padding-right: 1em; white-space:nowrap;" class=""><A href="#Existing_Non-conformant_Code">Existing Non-conformant Code</A></SPAN> </DIV></TD>
</TR>
</TABLE>
</DIV>
<DIV class="">
<H2 name="Background" id="Background">Background</H2>
<p>
The goal of this guide is to provide a number of rules that keep the code base manageable by enforcing
<em>consistency</em>.
It is very important that any programmer
can look at another programmer's code and understand it quickly.
Maintaining a uniform style and following conventions means that "pattern-matching" can be
more easily used to identify different symbols and invariants.
</p>
<p>
Creating common, required
idioms and patterns makes code much easier to understand. In some
cases there might be good arguments for changing certain style
rules. Nonetheless, for reasons of consistency the rules are left unchanged.
</p>
<p>
Note that this guide is not a C++ tutorial: we assume that the
reader is familiar with the language.
</p>
</DIV>
<DIV class="">
<H2 name="Naming" id="Naming">Naming</H2>
<p>
The most important consistency rules are those that govern
naming. The style of a name immediately informs us what sort of
thing the named entity is: a type, a variable, a function, a macro, etc., without requiring us to search for the
declaration of that entity. The pattern-matching engine in our
brains relies a great deal on these naming rules.
</p>
<p>
Naming rules are pretty arbitrary, but we feel that consistency is more important than individual preferences in this area, so regardless of whether you find them sensible or not, the rules are the rules.
</p>
<DIV class="">
<H3><A name="General_Naming_Rules" id="General_Naming_Rules">General Naming Rules</A></H3>
<SPAN class="link_button" id="link-General_Naming_Rules__button" name="link-General_Naming_Rules__button"><A href="?showone=General_Naming_Rules#General_Naming_Rules">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('General_Naming_Rules')" name="General_Naming_Rules__button" id="General_Naming_Rules__button">▶</SPAN>
<DIV style="display:inline;" class="">
Names should be meaningful; abbreviations should be avoided.
They follow camel case convention. Types and variables should be nouns,
while functions should be "command" verbs. </DIV>
<DIV class=""><DIV class="stylepoint_body" name="General_Naming_Rules__body" id="General_Naming_Rules__body" style="display: none">
<P class="">
<SPAN class="stylepoint_subsection">How to Name</SPAN>
<p>
Within reason, give as descriptive a name as possible. Do
not worry about saving horizontal space as it is far more
important to make your code immediately understandable to a
new reader. Examples of well-chosen names:
</p>
<DIV class=""><PRE>int numberOfErrors; // Good.
int numberOfCompletedConnections; // Good.</PRE></DIV>
<p>
Poorly chosen names use ambiguous abbreviations or arbitrary
characters that do not convey meaning.
Do not use directly the variable names from mathematical formulas.
In mathematics, variable names are usually limited to a single letter.
To implement a mathematical formula use variable names that
clearly indicate what value it holds.
</p>
<DIV class=""><PRE>float distance = velocity * time; // Good - no ambiguity</PRE></DIV>
<DIV class=""><PRE class="badcode">int n; // Bad - meaningless.
int nerr; // Bad - ambiguous abbreviation.
int nCompConns; // Bad - ambiguous abbreviation.
float s = v * t; // Bad - almost meaningless.</PRE></DIV>
<p>
Type and variable names should typically be nouns: e.g.,
<code>FileOpener</code>, <code>numberOfErrors</code>.
</p>
<p>
Function names should typically be imperative (that is they
should be commands): e.g., <code>openFile()</code>,
<code>setNumberOfErrors()</code>.
</p>
</P>
<P class="">
<SPAN class="stylepoint_subsection">CamelCase Convention</SPAN>
<p>
All names in C++ code follow camel case convention. This is
the practice of writing compound words so that each word
begins with a capital letter. No underscores are allowed.
Acronyms are the only exception : they can be capitalized.
</p>
<p>
For example:
</p>
<DIV class=""><PRE>string tableName; // Good</PRE></DIV>
<DIV class=""><PRE>class ITSReco // Good, acronyms can be capitalized</PRE></DIV>
<DIV class=""><PRE class="badcode">string table_name; // Bad - uses underscore.
string tablename; // Bad - all lowercase.</PRE></DIV>
</P>
<P class="">
<SPAN class="stylepoint_subsection">Capitalization Rules</SPAN>
<p>
Variables and functions start with a lowercase letter.<br>
Everything else in C++ code (type names, constant
expressions) starts with an uppercase letter.
</p>
</P>
<P class="">
<SPAN class="stylepoint_subsection">Abbreviations</SPAN>
<p>
Do not use abbreviations except for acronyms. For example:
</p>
<DIV class=""><PRE>// Good. These show proper names with no abbreviations.
int numberOfDnsConnections; // Most people know what "DNS" stands for.
int priceCount; // Price count, it makes sense.
int daqRate; // In ALICE everyone knows what DAQ stands for.</PRE></DIV>
<DIV class=""><PRE class="badcode">// Bad. Abbreviations can be confusing or ambiguous outside a small group.
int wgcConnections; // Only your group knows what this stands for.
int pcReader; // Lots of things can be abbreviated "pc".</PRE></DIV>
<p>
A single letter variable name can be used for well-known idioms like iterators of integer type and pimpl-idioms (d-pointer).
</p>
<DIV class=""><PRE>for (int i = 0 ; i < 10 ; i++) { // Good.
prices[i] = 0;
}</PRE></DIV>
<DIV class=""><PRE class="badcode">for (auto i = &prices[0]; i < &prices[10]; ++i) { // Bad
*i = 0; // i is not of integer type
}</PRE></DIV>
<p>
Never abbreviate by leaving out letters. <br>
Exception:
You may use <code>it</code> for iterator names or their names prefix.
</p>
<DIV class=""><PRE>// Good
std::vector<int > myVector;
auto it = myVector.begin();
auto itMyVector = myVector.begin();
auto myVectorIterator = myVector.begin(); </PRE></DIV>
<DIV class=""><PRE class="badcode">auto myVectorIt = myVector.begin(); // Bad. It is abbreviated, but not a prefix.</PRE></DIV>
</P>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="File_Names" id="File_Names">Code File Names</A></H3>
<SPAN class="link_button" id="link-File_Names__button" name="link-File_Names__button"><A href="?showone=File_Names#File_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('File_Names')" name="File_Names__button" id="File_Names__button">▶</SPAN>
<p> </p>
<DIV style="display:inline;" class="">
C++ code file names are derived from the class (or the namespace)
names. <br>
Program file names and utility file names, which do not define a class
or a namespace, start with a lower case letter.<br>
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="File_Names__body" id="File_Names__body" style="display: none">
<p>
C++ implementation files should end in <code>.cxx</code> and header files
should end in <code>.h</code>.
</p>
<p>
Inline functions should go directly into your
<code>.h</code> file. However, if they include a lot of code, they may go into a third file that
ends in <code>.inl</code>. This rule applies to templates too.
</p>
<p>
Do not use filenames that already exist
in <code>/usr/include</code>, such as <code>db.h</code>.
</p>
<p>
Examples of acceptable file names:
</p>
<DIV class=""><PRE>MyClass.h // The class declaration.
MyClass.cxx // The class definition.
MyClass.inl // Inline functions that include lots of code.
myUtilities.h // Utility functions/definitions
testMyClass.cxx // Test program</PRE></DIV>
<p>
Having upper case letters in a file name might theoretically lead to problems for case-insensitive operating systems.
However, as the file name corresponds to the class name it seems important to keep the same case as well.
Moreover, having two valid class/file names that would collide on a not-case sensitive OS seems extremely unlikely.
</p>
</DIV></DIV>
</DIV>
<div class="">
<h3>
<a name="Executable_Names" id="Executable_Names">Executable Names</a>
</h3>
<span class="link_button" id="link-Executable_Names__button" name="link-Executable_Names__button">
<a href="?showone=Executable_Names#Executable_Names">link</a>
</span>
<span class="showhide_button" onclick="javascript:ShowHideByName('Executable_Names')"
name="Executable_Names__button" id="Executable_Names__button">▶</span>
<div style="display:inline;" class="">
Executable names should be in lowercase, prefixed with <code>o2</code>, and hyphenated : <code>o2-tpc-reco-workflow</code>
</div>
<div class="">
<div class="stylepoint_body" name="Executable_Names__body" id="Executable_Names__body" style="display: none">
<p>When applicable, the <code>o2</code> prefix is immediately followed by the subsystem short name.</p>
<p>For example:</p>
<pre>
o2-sim
o2-conditions-client
o2-digitizer-workflow
o2-tpc-reco-workflow
o2-tpc-monitor
o2-flp-sender-device
</pre>
</div>
</div>
</div>
<div class="">
<h3>
<a name="Library_Names" id="Library_Names">Library Names</a>
</h3>
<span class="link_button" id="link-Library_Names__button" name="link-Library_Names__button">
<a href="?showone=Library_Names#Library_Names">link</a>
</span>
<span class="showhide_button" onclick="javascript:ShowHideByName('Library_Names')" name="Library_Names__button"
id="Library_Names__button">▶</span>
<div style="display:inline;" class="">
Library names should follow camel case convention (with the exception of the subsystem names) and be prefixed with
<code>O2</code>.
</div>
<div class="">
<div class="stylepoint_body" name="Library_Names__body" id="Library_Names__body" style="display: none">
<p>When applicable, the <code>O2</code> prefix is immediately followed by the subsystem short name (in upper
case).</p>
<p>For example:</p>
<pre>
libO2CPVSimulation.dylib
libO2HitAnalysis.dylib
libO2TPCMonitor.dylib
</pre>
</div>
</div>
</div>
<DIV class="">
<H3><A name="Type_Names" id="Type_Names">Type Names</A></H3>
<SPAN class="link_button" id="link-Type_Names__button" name="link-Type_Names__button"><A href="?showone=Type_Names#Type_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Type_Names')" name="Type_Names__button" id="Type_Names__button">▶</SPAN>
<DIV style="display:inline;" class="">
Type names follow camel case convention and start with an upper case letter:
<code>MyClass</code>, <code>MyEnum</code>.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Type_Names__body" id="Type_Names__body" style="display: none">
<p>
The names of all types — classes, structs, typedefs, and enums
— have the same naming convention. For example:
</p>
<DIV class=""><PRE>// classes and structs
class UrlTable
{ ...
class UrlTableTester
{ ...
struct UrlTableProperties
{ ...
// typedefs
typedef HashMap<UrlTableProperties *, string> PropertiesMap;
// enums
enum UrlTableErrors { ...</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Interface_Names" id="Interface_Names">Interface Names</A></H3>
<SPAN class="link_button" id="link-Interface_Names__button" name="link-Interface_Names__button"><A href="?showone=Interface_Names#Interface_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Interface_Names')" name="Interface_Names__button" id="Interface_Names__button">▶</SPAN>
<DIV style="display:inline;" class="">
The "Interface" suffix can optionally be used only if a class matches the <I><a href="https://htmlpreview.github.io/?https://github.com/AliceO2Group/CodingGuidelines/blob/master/coding_guidelines.html#Interfaces">Pure Interface</a></I> requirements.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Interface_Names__body" id="Interface_Names__body" style="display: none">
<DIV class=""><PRE>// pure interface
class UrlTableInterface
{ ...</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Variable_Names" id="Variable_Names">Variable Names</A></H3>
<SPAN class="link_button" id="link-Variable_Names__button" name="link-Variable_Names__button"><A href="?showone=Variable_Names#Variable_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Variable_Names')" name="Variable_Names__button" id="Variable_Names__button">▶</SPAN>
<DIV style="display:inline;" class="">
Variable names follow camel case convention and start with
a lower case letter: <code>myLocalVariable</code>.
<ul>
<li>Class member variables are prefixed with <code>m</code>.</li>
<li>Static class member variables are prefixed with <code>s</code>.</li>
<li>No <code>m</code> prefix for struct members.</li>
<li>Global variables are prefixed with <code>g</code>.</li>
<li>
<code>constexpr</code> variables are capitalized.</li>
<li>No additional prefix for <code>const</code>.</li>
</ul>
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Variable_Names__body" id="Variable_Names__body" style="display: none">
<P class="">
<SPAN class="stylepoint_subsection">Local Variable names</SPAN>
<p>
For example:
</p>
<DIV class=""><PRE>string tableName; // Good.</PRE></DIV>
</P>
<P class="">
<SPAN class="stylepoint_subsection">Class Data Members</SPAN>
<p>
Data members (also called instance variables or member
variables) are prefixed with <code>m</code>.
If the data member is <code>static</code>, prefix the variable with <code>s</code> instead.
</p>
<DIV class=""><PRE>class Something
{
private:
string mTableName;
static int sGlobalState;
};</PRE></DIV>
</P>
<P class="">
<SPAN class="stylepoint_subsection">Struct Variables</SPAN>
<p>
Data members in structs are named like regular
variables.
</p>
<DIV class=""><PRE>struct UrlTableProperties
{
string name;
int numberOfEntries;
}</PRE></DIV>
</P>
<P class="">
<SPAN class="stylepoint_subsection">Global Variables</SPAN>
<p>
Global variables,
which should be rare in any case, are prefixed with <code>g</code>.
</p>
</P>
<P class="">
<SPAN class="stylepoint_subsection">constexpr</SPAN>
<p>
A variable declared as <code>constexpr</code> typically is only used as compile-time constant (but may be stored in read-only memory).
Therefore it uses the same convention as <code>enum</code>s and uses uppercase CamelCase.
</p>
<DIV class=""><PRE>constexpr double LightSpeed = 2.99792458e+8;</PRE></DIV>
</P>
<P class="">
<SPAN class="stylepoint_subsection">const</SPAN>
<p>
A variable declared as <code>const</code> does not have any additional naming rules.
The prefixes describe the scope of the variable.
<code>const</code> is just one aspect of the type of the variable.
</p>
<p>
To provide a constant in the public interface consider <code>constexpr</code>,
<code>enum</code>, or a (<code>constexpr</code>) function instead.
</p>
</P>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Function_Names" id="Function_Names">Function Names</A></H3>
<SPAN class="link_button" id="link-Function_Names__button" name="link-Function_Names__button"><A href="?showone=Function_Names#Function_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Function_Names')" name="Function_Names__button" id="Function_Names__button">▶</SPAN>
<DIV style="display:inline;" class="">
Regular functions follow camel case convention and start with a lower case
letter: <code>myFunction()</code>.
<ul>
<li>Accessors and mutators match the name of the variable and are
prefixed with <code>get</code> and <code>set</code>:
<code>getMyMemberVariable()</code>, <code>setMyMemberVariable()</code>.</li>
<li> Functions (including accessors) returning a boolean value should be prefixed with
<code>is</code> or <code>has</code>. </li>
</ul>
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Function_Names__body" id="Function_Names__body" style="display: none">
<P class="">
<SPAN class="stylepoint_subsection">Regular Functions</SPAN>
<p>
Example of functions:
</p>
<DIV class=""><PRE>addTableEntry();
deleteUrl();</PRE></DIV>
<p>
The rule applies also to <code>constexpr</code> functions:
</p>
<DIV class=""><PRE>constexpr int getFive() { return 5; } </PRE></DIV>
</P>
<P class="">
<SPAN class="stylepoint_subsection">Accessors and Mutators</SPAN>
<p>
Accessors and mutators match
the name of the variable they are getting and setting and use
the prefixes <code>get</code> and <code>set</code>. The prefixes
<code>get</code> and <code>set</code> are not exclusive for accessors and mutators and they could
be used for other functions, if applicable.
This shows an excerpt of a class whose instance variable is
<code>mNumberOfEntries</code>:
</p>
<DIV class=""><PRE>class MyClass
{
public:
...
int getNumberOfEntries() const { return mNumberOfEntries; }
void setNumberOfEntries(int numberOfEntries) { mNumberOfEntries = numEntries; }
private:
int mNumberOfEntries;
};</PRE></DIV>
</P>
<P class="">
<SPAN class="stylepoint_subsection">Functions returning a boolean value</SPAN>
<p>
Functions returning a boolean value should be prefixed with
<code>is</code> or <code>has</code>:
</p>
<DIV class=""><PRE>bool isOpen();
bool hasZero();</PRE></DIV>
<p> This rule applies also to class member functions where <code>is</code> or <code>has</code> replace <code>get</code>:
</p>
<DIV class=""><PRE>class MyClass
{
public:
void setValid(bool isValid) const { mValid = isValid; }
bool isValid() const { return mValid; }
private:
bool mValid;
};</PRE></DIV>
</P>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Namespace_Names" id="Namespace_Names">Namespace Names</A></H3>
<SPAN class="link_button" id="link-Namespace_Names__button" name="link-Namespace_Names__button"><A href="?showone=Namespace_Names#Namespace_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Namespace_Names')" name="Namespace_Names__button" id="Namespace_Names__button">▶</SPAN>
<DIV style="display:inline;" class="">
Namespace names follow underscore convention and start with a lower case
letter: <code>my_namespace</code>.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Namespace_Names__body" id="Namespace_Names__body" style="display: none">
<DIV class=""><PRE>namespace my_namespace
{
void MyClass::doSomething()
{
...
}
}</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Enumerator_Names" id="Enumerator_Names">Enumerator Names</A></H3>
<SPAN class="link_button" id="link-Enumerator_Names__button" name="link-Enumerator_Names__button"><A href="?showone=Enumerator_Names#Enumerator_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Enumerator_Names')" name="Enumerator_Names__button" id="Enumerator_Names__button">▶</SPAN>
<DIV style="display:inline;" class="">
Enumerations and enumerators (the type and the values) follow camel case
convention and start with an upper case letter:
<code> MyEnumType</code>, <code>MyEnumValue</code>.
<ul>
<li>Enumerators in unscoped enumerations should have a common
prefix/postfix derived from the enumerations name. </li>
<li>Enum classes are already scoped and therefore the enumerators do not
need a prefix/postfix.</li>
</ul>
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Enumerator_Names__body" id="Enumerator_Names__body" style="display: none">
<P class="">
<SPAN class="stylepoint_section">Definition: </SPAN>
A <i>scoped</i> enum is one that is declared with the <code>class</code>
keyword, as opposed to a <i>traditional</i> enum, which is unscoped and
doesn't include the <code>class</code> keyword in its declaration.
</P>
<p>
Unscoped enumerators are exposed to the enclosing scope.
Therefore they should be prefixed or postfixed (decided by which position makes the code more prose-like)
with the enumeration's name (or a sensible part thereof).
All names use uppercase CamelCase.
</p>
<p>
Example of an unscoped enumeration:
</p>
<DIV class=""><PRE>enum Something {
SomethingSmall, // the type name as prefix
SomethingBig,
SomethingElse
};
void add(Something);
...
add(SomethingSmall);
add(SomethingElse);</PRE></DIV>
<p>
Another example of an unscoped enumeration whose enumerators are prefixed with just a (sensible) part
of the type name.
</p>
<DIV class=""><PRE>enum MallocAlignment {
AlignOnVector, // the "Align" prefix taken from the type name
AlignOnCacheline,
AlignOnPage
}
template<MallocAlignment A> void* malloc(size_t);
...
void* memory = malloc<AlignOnCacheline>(64);</PRE></DIV>
<p>
Enum classes (new in C++11) create scoped enumerators.
Therefore there is no need for prefixing or postfixing.
</p>
<DIV class=""><PRE>enum class Something : int {
Small,
Big,
Unknown
};
void add(Something);
...
add(Something::Small);
add(Something::Unknown);</PRE></DIV>
The following enum is forbidden unless it is enclosed in a private namespace.
The names <code>Ok</code> and <code>OutOfMemory</code> are too generic to be added to the global namespace.
<DIV class=""><PRE class="badcode">enum UrlTableErrors {
Ok, // Bad.
OutOfMemory // Bad.
};</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Macro_Names" id="Macro_Names">Macro Names</A></H3>
<SPAN class="link_button" id="link-Macro_Names__button" name="link-Macro_Names__button"><A href="?showone=Macro_Names#Macro_Names">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Macro_Names')" name="Macro_Names__button" id="Macro_Names__button">▶</SPAN>
<DIV style="display:inline;" class="">
All uppercase letters and underscores, prefixed with the sub/project name, i.e. <code>MY_PROJECT_PKG1_MY_MACRO</code>.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Macro_Names__body" id="Macro_Names__body" style="display: none">
<p>
For the rules on Macro usage see <a href="#Preprocessor_Macros">here</a>.
</p>
<p>
They must be named with all uppercase letters and underscores, prefixed with the
sub/project name.
</p>
<DIV class=""><PRE>#define O2_VERTEX_ROUND(x) ...
#define O2_CORE_PI_ROUNDED 3.0</PRE></DIV>
Be sure to never put an underscore as prefix nor use a double underscore.
First it doesn't follow our naming convention.
Second, and more importantly, it is reserved to compiler developers.
<DIV class=""><PRE class="badcode">#define _WRONG
#define WRONG__AGAIN
</PRE></DIV>
</DIV></DIV>
</DIV>
</DIV>
<DIV class="">
<H2 name="Formatting" id="Formatting">Formatting</H2>
<p>
Coding style and formatting are pretty arbitrary. However, a good project
is much easier to follow if everyone uses the same style. Individuals
may not agree with every aspect of the formatting rules, and some of
the rules may be hard to get used to. Even so, it is important that all
project contributors
follow the style rules so that
they
can all read and understand everyone's code easily.
</p>
<DIV class="">
<H3><A name="Line_Length" id="Line_Length">Line Length</A></H3>
<SPAN class="link_button" id="link-Line_Length__button" name="link-Line_Length__button"><A href="?showone=Line_Length#Line_Length">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Line_Length')" name="Line_Length__button" id="Line_Length__button">▶</SPAN>
<DIV style="display:inline;" class="">
Each line of text in your code should be at most 120 characters
long.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Line_Length__body" id="Line_Length__body" style="display: none">
<p>
Try keeping lines below 120 characters.
In some cases it may make sense to use much longer lines (e.g. for block editing).
But this should be confined to special sections in the code.
</p>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="One_Statement_Per_Line" id="One_Statement_Per_Line">One Statement Per Line</A></H3>
<SPAN class="link_button" id="link-One_Statement_Per_Line__button" name="link-One_Statement_Per_Line__button"><A href="?showone=One_Statement_Per_Line#One_Statement_Per_Line">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('One_Statement_Per_Line')" name="One_Statement_Per_Line__button" id="One_Statement_Per_Line__button">▶</SPAN>
<DIV style="display:inline;" class="">
Prefer one statement per line because it improves code readability.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="One_Statement_Per_Line__body" id="One_Statement_Per_Line__body" style="display: none">
<DIV class=""><PRE>// Good. One statement per line.
if (condition) {
doSomething();
}
else {
doAnotherThing;
}</PRE></DIV>
<DIV class=""><PRE class="badcode">// Bad. Two statements per line.
if (condition) doSomething(); else doAnotherThing; </PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Spaces_vs._Tabs" id="Spaces_vs._Tabs">Spaces vs. Tabs</A></H3>
<SPAN class="link_button" id="link-Spaces_vs._Tabs__button" name="link-Spaces_vs._Tabs__button"><A href="?showone=Spaces_vs._Tabs#Spaces_vs._Tabs">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Spaces_vs._Tabs')" name="Spaces_vs._Tabs__button" id="Spaces_vs._Tabs__button">▶</SPAN>
<DIV style="display:inline;" class="">
Indent with 2 spaces. Use only spaces, no tabs.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Spaces_vs._Tabs__body" id="Spaces_vs._Tabs__body" style="display: none">
<p>
Spaces are used for indentation. Do not use tabs in your code.
You should set your editor to emit spaces when you hit the tab
key.
</p>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Function_Declarations_and_Definitions" id="Function_Declarations_and_Definitions">Function Declarations and Definitions</A></H3>
<SPAN class="link_button" id="link-Function_Declarations_and_Definitions__button" name="link-Function_Declarations_and_Definitions__button"><A href="?showone=Function_Declarations_and_Definitions#Function_Declarations_and_Definitions">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Function_Declarations_and_Definitions')" name="Function_Declarations_and_Definitions__button" id="Function_Declarations_and_Definitions__button">▶</SPAN>
<DIV style="display:inline;" class="">
A function declaration is on one line if possible. Otherwise the parameters that do not
fit are on the next line(s).
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Function_Declarations_and_Definitions__body" id="Function_Declarations_and_Definitions__body" style="display: none">
<p>
The return type, function name and open parenthesis are always on the same line.
The open curly brace is at the beginning of the new line after the last parameter
except for inline functions.
</p>
<p>
Example:
</p>
<DIV class=""><PRE>ReturnType ClassName::functionName(Type parName1, Type parName2)
{
doSomething();
...
}</PRE></DIV>
<p>
Example of an inline function on one line:
</p>
<DIV class=""><PRE>inline int getValue() const { return mValue; }</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Pointer_and_Reference_Expressions" id="Pointer_and_Reference_Expressions">Pointer and Reference Expressions</A></H3>
<SPAN class="link_button" id="link-Pointer_and_Reference_Expressions__button" name="link-Pointer_and_Reference_Expressions__button"><A href="?showone=Pointer_and_Reference_Expressions#Pointer_and_Reference_Expressions">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Pointer_and_Reference_Expressions')" name="Pointer_and_Reference_Expressions__button" id="Pointer_and_Reference_Expressions__button">▶</SPAN>
<DIV style="display:inline;" class="">
No spaces around period or arrow. Pointer operators are either followed
or preceded with a space.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Pointer_and_Reference_Expressions__body" id="Pointer_and_Reference_Expressions__body" style="display: none">
<p>
The following are examples of correctly formatted pointer and
reference expressions:
</p>
<DIV class=""><PRE>int value = *valuePointer;
int* valuePointer = &value;
int value = myObject->getValue();
int value = myStruct.value;</PRE></DIV>
<p>
When declaring a pointer or reference, the `*` or `&` goes with the type.
</p>
<DIV class=""><PRE>
// Pointers
char* myCharacter; // typical C++ notation
// References
const string& myString;</PRE></DIV>
<DIV class=""><PRE class="badcode">char * myCharacter; // Bad - spaces on both sides of *
char *myCharacter; // Bad - space before the *, the * is with the variable name.
const string &myString; // Bad - the & is with the variable name.
const string & myString; // Bad - spaces on both sides of &</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Boolean_Expressions" id="Boolean_Expressions">Boolean Expressions</A></H3>
<SPAN class="link_button" id="link-Boolean_Expressions__button" name="link-Boolean_Expressions__button"><A href="?showone=Boolean_Expressions#Boolean_Expressions">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Boolean_Expressions')" name="Boolean_Expressions__button" id="Boolean_Expressions__button">▶</SPAN>
<DIV style="display:inline;" class="">
In the case of a boolean expression that is longer than the <a href="#Line_Length">standard line length</a>, lines should be broken up in a consistent way. All operators should be either at the beginning or at the end of the line.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Boolean_Expressions__body" id="Boolean_Expressions__body" style="display: none">
<p>
In this example, the logical AND operator is always at the end
of the lines:
</p>
<DIV class=""><PRE>if (thisOneThing > thisOtherThing &&
aThirdThing == aFourthThing &&
yetAnother && lastOne) {
...
}</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Variable_and_Array_Initialization" id="Variable_and_Array_Initialization">Variable and Array Initialization</A></H3>
<SPAN class="link_button" id="link-Variable_and_Array_Initialization__button" name="link-Variable_and_Array_Initialization__button"><A href="?showone=Variable_and_Array_Initialization#Variable_and_Array_Initialization">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Variable_and_Array_Initialization')" name="Variable_and_Array_Initialization__button" id="Variable_and_Array_Initialization__button">▶</SPAN>
<DIV style="display:inline;" class="">
Prefer initialization with braces except for single-argument assignment.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Variable_and_Array_Initialization__body" id="Variable_and_Array_Initialization__body" style="display: none">
<p>
Example of single-argument assignments:
</p>
<DIV class=""><PRE>int x = 3; // preferred style
std::string name = "Some Name";
int x { 3 }; // also possible
std::string name{ "Some Name" };
std::string name = { "Some Name" };</PRE></DIV>
<p>
Using <code>{}</code> for initialization is more consistent, more correct, and avoids
having to know about old-style pitfalls [1].
</p>
<p>
Example of variable initialization:
</p>
<DIV class=""><PRE>Rectangle window{ 0, 0, 1024, 768 }; // preferred style
Rectangle window = { 0, 0, 1024, 768 }; // also possible
Rectangle window(0, 0, 1024, 768); // avoid unless necessary</PRE></DIV>
<p>
There is one exception: In rare cases a class may provide an <code>std::initializer_list</code> constructor and other constructors that are hidden by the <code>std::initializer_list</code> constructor.
The hidden constructor can then still be accessed with <code>()</code>.
</p>
<DIV class=""><PRE>std::vector<int> v( 10, 20 ); // calls vector(size_t n, const int &value)
std::vector<int> v{ 10, 20 }; // calls vector(std::initializer_list<int> values)</PRE></DIV>
</DIV></DIV>
</DIV>
<DIV class="">
<H3><A name="Preprocessor_Directives" id="Preprocessor_Directives">Preprocessor Directives</A></H3>
<SPAN class="link_button" id="link-Preprocessor_Directives__button" name="link-Preprocessor_Directives__button"><A href="?showone=Preprocessor_Directives#Preprocessor_Directives">
link
</A></SPAN><SPAN class="showhide_button" onclick="javascript:ShowHideByName('Preprocessor_Directives')" name="Preprocessor_Directives__button" id="Preprocessor_Directives__button">▶</SPAN>
<DIV style="display:inline;" class="">
The hash mark that starts a preprocessor directive is
always at the beginning of the line.
</DIV>
<DIV class=""><DIV class="stylepoint_body" name="Preprocessor_Directives__body" id="Preprocessor_Directives__body" style="display: none">
<p>
Even when preprocessor directives are within the body of
indented code, the directives should start at the beginning of
the line.
</p>
<DIV class=""><PRE>// Good - directives at beginning of line
if (lopsidedScore) {
#if DISASTER_PENDING
dropEverything();
# if NOTIFY // Good but not required -- Spaces after #
notifyClient();
# endif
#endif
goBackToNormal();
}</PRE></DIV>
<DIV class=""><PRE class="badcode">// Bad - indented directives
if (lopsidedScore) {
#if DISASTER_PENDING // The "#if" should be at beginning of line
dropEverything();