forked from OpenXRay/xray-16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_code.txt
1022 lines (938 loc) · 24.5 KB
/
cpp_code.txt
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
TITLE: C++ Coding Conventions
STATUS: WORK IN PROGRESS
---------------------
Shift width (indentation) is 4 spaces.
Line size is 120 columns.
Indentation is always one shift-width (4):
BAD
GatherInfo(expression, description, arg0, arg1, file, line,
function, assertionInfo);
GOOD
GatherInfo(expression, description, arg0, arg1, file, line,
function, assertionInfo);
Open and close brackets of a section should be on the same level:
BAD
if (lastErr==ERROR_SUCCESS) {
*buffer = 0;
return;
}
GOOD
if (lastErr==ERROR_SUCCESS)
{
*buffer = 0;
return;
}
Access modifiers are indented like goto labels - at the class level
indentation:
BAD
class ObjectComparer
{
public:
ObjectComparer();
int Compare(const Object &a, const Object &b);
private: int InternalCompare(const Object &a, const Object &b);
};
GOOD
class ObjectComparer
{
public:
ObjectComparer();
int Compare(const Object &a, const Object &b);
private:
int InternalCompare(const Object &a, const Object &b);
};
Namespace content should not be indented:
BAD:
namespace XRay
{
namespace Math
{
#define PI 3.14159265359
}
}
GOOD
namespace XRay
{
namespace Math
{
#define PI 3.14159265359
}
}
When breaking up a long line, it should continue with one shift-width for
indentation:
BAD
if (lineLength>1 && (screenSize.Vertical<bufferSize.Vertical
|| explicitLines))
{
printf("this is a test section that will show how to handle "
"long lines, such as %s which is %d lines long",
"this", 2);
SetWindowText( hWnd,
"Button Name" );
}
GOOD
if (lineLength>1 && (screenSize.Vertical<bufferSize.Vertical
|| explicitLines))
{
printf("this is a test section that will show how to handle "
"long lines, such as %s which is %d lines long", "this", 2);
SetWindowText(hWnd, "Button Name");
}
if/for/while statement that takes more than one line should always have
brackets same thing goes when the then/loop part is more than one line:
BAD
if (!ErrorAfterDialog)
MessageBox(NULL, "Fatal error occured\n\n"
"Press OK to abort program execution", "Fatal error",
MB_OK|MB_ICONERROR|MB_SYSTEMMODAL);
GOOD
if (!ErrorAfterDialog)
{
MessageBox(NULL, "Fatal error occured\n\n"
"Press OK to abort program execution", "Fatal error",
MB_OK|MB_ICONERROR|MB_SYSTEMMODAL);
}
Function prototypes should be spaced like function calls:
BAD
void Debug::Backend(const char *reason,
const char *expression,
const char *arg0,
const char *arg1,
const char *file,
int line,
const char *function,
bool &ignoreAlways);
{
// function code
}
BAD
void Debug::Backend(const char *reason, const char *expression, const
char *arg0, const char *arg1, const char *file, int line, const char *
function, bool &ignoreAlways);
GOOD
// declaration
static void Backend(const char *reason, const char *expression,
const char *arg0, const char *arg1, const char *file, int line,
const char *function, bool &ignoreAlways);
// implementation
void Debug::Backend(const char *reason, const char *expression,
const char *arg0, const char *arg1, const char *file, int line,
const char *function, bool &ignoreAlways)
{
// function code
}
// if you need to comment parameters
void Debug::Backend(
const char *reason, // error reason
const char *expression, // failed expression
const char *arg0, // first argument
const char *arg1, // second argument
const char *file,
int line,
const char *function,
bool &ignoreAlways) // ignore errors of this type
{
// function code
}
switch statements should have the case on the same level, no space before ':'.
if the case is one line, then one space after ':':
BAD
switch (vendor)
{
case Vendor::Intel :
...
case Vendor::AMD :
...
}
GOOD
switch (vendor)
{
case Vendor::Intel:
...
case Vendor::AMD:
...
}
GOOD
switch (controlId)
{
case IDC_TOGGLESTATS:
app->drawStats = !app->drawStats;
break;
case IDC_RESETCAMERA:
app->ResetCamera();
break;
case IDC_LIGHT_SIZE:
{
auto r = hud->GetSlider(IDC_LIGHT_SIZE)->GetValue()*0.01f;
app->spotLightObj->SetLightRadius(r);
break;
}
}
GOOD
switch (vendorId)
{
case 0x756e6547: vendor = Vendor::Intel; break;
case 0x68747541: vendor = Vendor::AMD; break;
default: vendor = Vendor::Unknown; break;
}
No space between function name and opening brackets,
no space between opening brackets and first parameter,
one space after comma:
BAD
Msg ("hello %s\n", "world");
Msg( "hello world\n" );
Msg("hello world\n","world");
GOOD
Msg("hello world\n", "world");
One space after reserved words before the opening parenthesis:
BAD
if(OnDialog)
GOOD
if (OnDialog)
'then' part of if statement should be in a separate line.
Reason: allows to debug the if and see when it is run.
BAD
if (OnDialog) OnDialog(true);
GOOD
if (OnDialog)
OnDialog(true);
else if statements should be on the same level at the starting if
Reason: this is similar to switch statement.
BAD
if (!strcmp(argv[1],"--help")) PrintUsage();
else if (!strcmp(argv[1], "--run")) {
RunApplication();
PrintResults();
} else PrintError();
GOOD:
if (!strcmp(argv[1], "--help"))
PrintUsage();
else if (!strcmp(argv[1], "--run"))
{
RunApplication();
PrintResults();
}
else
PrintError();
return statements should not have parentheses and should not have a space
after them:
BAD
return (0);
GOOD
return 0;
BAD
return ;
GOOD
return;
Don't use unneeded parentheses in expressions:
BAD
if ((a!=b) || (c!=d))
...
GOOD
if (a!=b || c!=d)
...
Don't call return at the end of a function returning void:
BAD
void foo()
{
bar();
return;
}
GOOD
void foo()
{
bar();
}
Don't separate code sections with more than one blank line.
class/struct/enum definitions, initializations should open { at the next line:
BAD
struct Size {
int Width;
int Height;
};
GOOD
struct Size
{
int Width;
int Height;
};
GOOD (no members)
struct Dummy {};
BAD
Size size = {
LayoutHelper::AdjustLength(Clamp(length, 0, 1024)),
LayoutHelper::CalculatePreferredHeight(length, scaleFactor.y)
};
GOOD
Size size =
{
LayoutHelper::AdjustLength(Clamp(length, 0, 1024)),
LayoutHelper::CalculatePreferredHeight(length, scaleFactor.y)
};
GOOD (initialization fits in a single line)
Size size = {1024, 16};
Prefer C++ comments to C comments:
GOOD
/* draw overlay statistics */
DrawStats();
GOOD (better)
// draw overlay statistics
DrawStats();
Comments should be aligned as the code they comment, or one space after
the end of line:
BAD
/*
* draw overlay statistics
*/
DrawStats();
BAD
//
// draw overlay statistics
//
DrawStats();
GOOD
/* draw overlay statistics */
DrawStats();
GOOD (better)
// draw overlay statistics
DrawStats();
GOOD
DrawStats(); // draw overlay statistics
Comments which occupy more than one line should adhere to the following
guidline:
BAD
//
// last render stage: draw overlay engine performance statistics
// (input, render, sound, ai, physics)
//
DrawStats();
GOOD
// last render stage: draw overlay engine performance statistics
// (input, render, sound, ai, physics)
DrawStats();
Common sense should come into play when deciding whether to use for or while
loops:
BAD
int i = 0;
while (i<10)
{
i++;
...
}
GOOD
for (int i = 0; i<10; i++)
...
BAD
for (ptr = ptr->next; ptr; ptr = ptr->next)
...
GOOD
while (ptr = ptr->next, ptr)
...
In for and while loops without a statement, statement separator should be in
the same line.
BAD
for(i = 0; Test(i); i++)
;
GOOD
for (i = 0; Test(i); i++);
Put one space before and after an assignment operator:
BAD
int a=0;
x= x+1;
capacity*=2;
GOOD
int a = 0;
x = x+1;
capacity *= 2;
Don't put a space before a statement separator, put one after it:
BAD
for (i = 0 ;i<10 ;i--, j *= 2) ;
GOOD
for (i = 0; i<10; i--, j *= 2);
Don't put a space after increment and decrement. Increment after the value,
not before.
BAD
i --;
++j;
GOOD
i--;
j++;
Trivial > >= < <= == != expressions should not have spaces around them.
Examples: a, array[i].var[j], sizeof(Foo), x+1
Pointers (->) are not counted as trivial expressions for > >= < <=, since it's
hard to read x->y>z, while its easy to read x->y==z.
BAD
if (x > 5)
GOOD
if (x>5)
BAD
if (x+1>5)
GOOD
if (x+1 > 5)
BAD
if (f(x, y) > g(y, z))
GOOD
if (f(x, y)>g(y, z))
BAD
if (x->y == 5)
GOOD
if (x->y==5)
BAD
if (x->y>=5)
GOOD
if (x->y >= 5)
Don't increment, set or change value of variable inside a function call:
BAD
CalcXY(i++, 2);
BAD
SetX(i = 3);
GOOD (if i is not a global)
CalcXY(i, 2);
i++;
GOOD (if i is a global that CalcXY() uses)
i++;
CalcXY(i-1, 2);
GOOD
if (i++)
a[i++] = 4;
GOOD (if i is not a global)
i = 3;
SetX(i);
In for loops, when there is a function that gets the next element, it should
be done once (inside the step condition):
BAD:
for (int i = 0, ch = GetChar(); ch=='\r'; ch = GetChar(), i++)
HandleResult(ch);
GOOD:
for (int i = 0; (ch = GetChar())=='\r'; i++)
HandleResult(ch);
When assigning in a truth value (if/for/while), use the following pattern in
order to eliminate compiler warnings:
BAD:
for (int i = 0; ch = GetChar(); i++)
HandleResult(ch);
if (x = ComputeNum())
return x;
GOOD:
for (int i = 0; ch = GetChar(), ch; i++)
HandleResult(ch);
if (x = ComputeNum(), x)
return x;
In string initialization and null termination, you must use 0:
BAD:
string[0] = '\0';
if (string[3]=='\0')
{
}
GOOD:
*string = 0;
if (!string[3])
{
}
Naming conventions:
- Do choose easily readable identifier names. For example, a variable named
horizontalAlignment is more readable in English than alignmentHorizontal.
- Do not use Hungarian notation except in the following cases:
- UI-related code, i.e. btnOpen, lblMessage, chkRecursive, cbGameType
- Interface classes: IClient, IGameLevel, IGamePersistent, etc
- Template type arguments: TBase, TResult
Casing styles:
- class/struct/enum: PascalCase
- Math primitives can be in lowercase:
vector2f, angle3f, matrix44f
- Using declarations of primitive types can be in lowercase:
uint, int16, uint64
- Functions: PascalCase
- Public and protected fields: PascalCase
- Math primitives can use lowercase:
vector2f.x, angle3f.yaw, matrix44f.m03
- Private fields: camelCase
- Local variables: camelCase
- Global variables: PascalCase
- Namespaces: PascalCase
Don't put inline qualifier on a function defined within a class definition -
such function is inline according to the standard.
When defining an interface class, declare destructor as pure virtual and add
its empty inline implementation after the class definition:
GOOD
class IServer
{
public:
virtual ~IServer() = 0;
};
inline IServer::~IServer() {}
Do not mark interface classes with linkage attributes.
When overriding virtual function, use override specifier on function
declaration.
BAD
class Server : public IServer
{
...
virtual void OnClientConnected(IClient* client);
...
};
GOOD
class Server : public IServer
{
...
virtual void OnClientConnected(IClient* client) override;
...
};
Checking function return values:
Functions that return bool or a pointer will be checked without
comparing, if they return true or false.
BAD
if (strstr(args, " -h")!=nullptr)
PrintHelp();
GOOD
if (strstr(args, " -h"))
PrintHelp();
BAD
if (IsUpdateRequired()==true)
Update();
GOOD
if (IsUpdateRequired())
Update();
BAD
cl = GetClient(id);
if (cl==nullptr)
return nullptr;
GOOD
cl = GetClient(id);
if (!cl)
return nullptr;
BAD
cl = GetClient(id);
if (cl!=nullptr)
Disconnect(cl);
GOOD
cl = GetClient(id);
if (cl)
Disconnect(cl);
When allocating a structure on stack, use zero initializer unstead of memset:
BAD
ShaderParams params;
memset(¶ms, sizeof(params), 0);
GOOD
ShaderParams params = {};
When possible, prefer inline functions to macros:
BAD
#define RAD2DEG(angle) ((angle)*180/PI)
GOOD
template <typename T>
T RadiansToDegrees(T angle) { return angle*180/PI; }
Macro names should normally be all upper case, with normal spacing as rest of
code:
BAD
#define RAD2DEG( angle ) ((angle)*180/PI)
#define RAD2DEG(angle) \
((angle) * 180 / PI)
GOOD
#define RAD2DEG(angle) ((angle)*180/PI)
GOOD
#define RAD2DEG(angle) \
((angle)*180/PI)
Common use macros that are more than one statement long should be encapsulated
by do...while(false). This enables using them inside if...else statements:
Example usage:
if (condition)
DO_A_AND_B;
else
DoSomethingElse();
BAD
#define DO_THIS_AND_THAT() \
{ \
DoThis(); \
DoThat(); \
}
GOOD
#define DO_THIS_AND_THAT() \
do { \
DoThis(); \
DoThat(); \
} while (false)
Don't use forward declarations unless they are necessary. Instead, change the
order so the caller is below the callee.
BAD
void Callee();
void Caller()
{
Callee();
}
void Callee()
{
}
GOOD
void Callee()
{
}
void Caller()
{
Callee();
}
Prefer portable data types from Common.hpp over OS specific data types:
BAD
BYTE b;
DWORD w;
GOOD
byte b;
uint32 w;
At the beginning of a file add inclusion protection. Don't use include guards.
Instead, use #pragma once - it's less prone to making mistakes and it is less
code to type. Though it's not a part of the standard, it's well supported
across compilers.
BAD
#ifndef _XRAY_HPP_
#define _XRAY_HPP_
// The XRay.hpp content comes here
#endif // _XRAY_HPP_
GOOD
#pragma once
// The XRay.hpp content comes here
Files should terminate with an empty line:
BAD
int main()
{
return 0;
}<EOF>
GOOD
int main()
{
return 0;
}
<EOF>
COMPLEX #if sections should adhere to the following guideline:
BAD
#ifndef NO_SINGLE
Msg("* Found new patch: %s", versionName);
#ifdef DOWNLOAD_UPDATES
#ifdef DOWNLOAD_UPDATES_GATHER_STATS
DownloadStats stats;
DownloadUpdate(downloadUrl, stats);
stats.Dump();
#else
DownloadUpdate(downloadUrl);
#endif
#endif
#endif
BAD
#ifndef NO_SINGLE
Msg("* Found new patch: %s", versionName);
# ifdef DOWNLOAD_UPDATES
# ifdef DOWNLOAD_UPDATES_GATHER_STATS
DownloadStats stats;
DownloadUpdate(downloadUrl, stats);
stats.Dump();
# else
DownloadUpdate(downloadUrl);
# endif
# endif
#endif
GOOD
#ifndef NO_SINGLE
Msg("* Found new patch: %s", versionName);
#ifdef DOWNLOAD_UPDATES
#ifdef DOWNLOAD_UPDATES_GATHER_STATS
DownloadStats stats;
DownloadUpdate(downloadUrl, stats);
stats.Dump();
#else
DownloadUpdate(downloadUrl);
#endif
#endif
#endif
SIMPLE #if sections should not be indented:
GOOD
#ifndef DEBUG
#ifdef _DEBUG
#define DEBUG
#endif
#ifdef MIXED
#define DEBUG
#endif
#endif
BAD
#ifndef DEBUG
#ifdef _DEBUG
#define DEBUG
#endif
#ifdef MIXED
#define DEBUG
#endif
#endif
GOOD
void Sleep(int milliseconds)
{
#ifdef WINDOWS
::Sleep(milliseconds);
#else
::sleep(milliseconds);
#endif
}
BAD
void Sleep(int milliseconds)
{
#ifdef WINDOWS
::Sleep(milliseconds);
#else
::sleep(milliseconds);
#endif
}
After #endif you can put a comment telling to what if it belongs if there is
a large gap between the #if and its corresponding #endif.
BAD
#ifdef _EDITOR
VerifyPath(path);
#endif // _EDITOR
GOOD
#ifdef _EDITOR
VerifyPath(path);
#endif
GOOD
#ifdef _EDITOR
// Lots of editor related code, that you really have to scroll down
// to see all of it.
#endif // _EDITOR
Only use the defined() macro when you have a complex expression. If you find
that you do need to use the defined() macro for more than one flag, see if
that flags can be grouped under another one new flag:
BAD
#ifndef DEDICATED_SERVER
#ifdef CONFIG_SHOW_LOGO_WINDOW
DestroyWindow(logoWindow);
#endif
#endif
BAD
#if defined(CONFIG_SHOW_LOGO_WINDOW)
DestroyWindow(logoWindow);
#endif
GOOD
#ifdef CONFIG_SHOW_LOGO_WINDOW
DestroyWindow(logoWindow);
#endif
GOOD (since you really need both flags)
#if !defined(DEDICATED_SERVER) && defined(CONFIG_SHOW_LOGO_WINDOW)
DestroyWindow(logoWindow);
#endif
Use using instead of typedef:
BAD
typedef void (*FunctionType)(double);
typedef Vector3<float> vector3f;
GOOD
using FunctionType = void (*)(double);
using vector3f = Vector3<float>;
Use strongly typed enums instead of plain C enums:
BAD
enum CmdStatus
{
CmdStatusOk,
CmdStatusInProgress,
CmdStatusFailed,
};
GOOD
enum class CmdStatus
{
Ok,
InProgress,
Failed,
};
Enums that can be serialized should have values assigned:
BAD
enum class CmdStatus
{
Ok = 1,
InProgress,
Failed,
};
GOOD
enum class CmdStatus
{
Ok = 1,
InProgress = 2,
Failed = 3,
};
Every enum line have to be comma terminated - this makes the enum extensible.
Only separate the last line, if it is meant to never be surpassed.
BAD
enum class CmdType
{
Reset = 1,
Load = 2,
Save = 3
};
GOOD
enum class CmdType
{
Reset = 1,
Load = 2,
Save = 3,
};
GOOD
enum class CmdType
{
Reset = 1,
Load = 2,
Save = 3,
Invalid = 0xFF
};
Put the pointer and reference next to the identifier:
BAD
IClient* GetServerClient();
void BanAddress(const IPAddress& ip);
GOOD
IClient *GetServerClient();
void BanAddress(const IPAddress &ip);
Put static/const/volatile/mutable before the type:
BAD
int const static z;
bool volatile exit;
int static Log(char const *s);
bool mutable lastState;
char const* const* name;
GOOD
static const int z;
volatile bool exit;
static int Log(char const *s);
mutable bool lastState;
const char *const *name;
Use stack allocation for local variables of constant size. Use dynamic
allocation only when needed.
BAD
ConnectionParams *cp = new ConnectionParams();
...
delete cp;
GOOD
ConnectionParams cp;
Operator delete also handles nullptr, so no need for 'if ()' before it:
BAD (if str can be null):
if (str)
delete str;
GOOD:
delete str;
Constant strings longer than one line should be closed on each line by a quote
and opened again on the next line. Words should have the space after them on
the same line.
BAD
Log("Hello world, this is a long string that we want to print and \
is more than 120 chars long so we need to split it");
BAD
Log("Hello world, this is a long string that we want to print"
" and is more than 120 chars long so we need to split it");
GOOD
Log("Hello world, this is a long string that we want to print "
"and is more than 120 chars long so we need to split it");
Functions that accept zero parameters should be defined without void:
BAD
IClient *GetServerClient(void);
GOOD
IClient *GetServerClient();
Trivial + - * / expressions should not have spaces around them:
BAD
Foo(a + b);
GOOD
Foo(a+b);
BAD
Foo(2 * (a + b));
GOOD
Foo(2*(a+b));
BAD
Foo((a + b) / (1000 * 1000));
GOOD
Foo((a+b)/(1000*1000));
BAD
Foo((Min(aMin, bMin)+Max(aMax, bMax))*(width+Pow(a, p))/(1000*1000));
GOOD
Foo((Min(aMin, bMin)+Max(aMax, bMax)) * (width+Pow(a, p)) / (1000*1000));
Don't put empty lines between code inside functions. If you want to separate
code fragments, put a comment line.
BAD
for (auto &item : items)
storage.push_back(item);
if (!storage.size())
Log("! ERROR: No items found");
GOOD
for (auto &item : items)
storage.push_back(item);
// check if there's no items
if (!storage.size())
Log("! ERROR: No items found");
Class Foo in src/Bar/Foo.cpp & Foo.hpp.
All files should follow this exact example.
Unless using precompiled headers, #include "Config.hpp" must be first in
the *.cpp file to make sure the features configuration affect all the rest of
the files. If using precompiled headers, #include "Config.hpp" must appear
immediately after the precompiled header file (e.g. stdafx.hpp).
Header files must be self contained, i.e. they must be able to compile
without relying on another include line to come before them.
Therefore #include "Foo.hpp" must be immediately after to make sure Foo.hpp
compiles standalone without any dependency on includes before it.
GOOD
Foo.hpp template
------------------------------
#pragma once
#include "Config.hpp"
... Put here minimal includes required by Foo.hpp ...
... Put here your declarations ...
Foo.cpp template
------------------------------
#include "Config.hpp"
#include "Foo.hpp"
... Put here minimal includes required by Foo.cpp ...
... Put here your code ...
GOOD (using precompiled headers)
stdafx.hpp template
------------------------------
#pragma once
#include "Config.hpp"
... Put here additional includes ...
Foo.hpp template
------------------------------
#pragma once
#include "Config.hpp"
... Put here minimal includes required by Foo.hpp ...
... Put here your declarations ...
Foo.cpp template
------------------------------
#include "stdafx.hpp"
#include "Config.hpp"
#include "Foo.hpp"
... Put here minimal includes required by Foo.cpp ...
... Put here your code ...
Use #include "Foo.hpp" for headers in the same directory as the source
including them.
Use #include "PathToFoo/Foo.hpp" for headers in other directories, where
PathToFoo is relative to root engine directory.
Use #include <Foo.hpp> for external system files.
BAD
#include <Config.hpp>
#include <xrCore/xrCore.hpp>
#include "IReader.hpp"
#include "algorithm"
GOOD
#include "Config.hpp"
#include "xrCore.hpp" // we are in src/xrCore directory
#include "IO/IReader.hpp" // from src/xrCore/IO/IReader.hpp
#include <algorithm>
Free function used in one CPP file:
should be implemented static, and a declaration in the CPP file should only