-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNumberOfGlobals.qhelp
More file actions
34 lines (28 loc) · 1.31 KB
/
NumberOfGlobals.qhelp
File metadata and controls
34 lines (28 loc) · 1.31 KB
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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>This metric measures the number of global variables in a file. This includes both public and file-local global variables.</p>
<p>Globals in general should be avoided as it is too easy for them to be modified in other parts of the code.
It also pollutes the name space and public globals expose internal representation. This is particularly
important in multi-threaded applications as every global is likely going to require some synchronization
mechanism to ensure that they are accessed and written in the correct order.</p>
</overview>
<recommendation>
<p>Try to eliminate globals by passing them as parameters to functions. If global state is necessary,
try to encapsulate related variables into structs and classes. In C++ it is preferable to group state
as well as functions into classes.</p>
</recommendation>
<references>
<li>
<a href="http://www.learncpp.com/cpp-tutorial/42-global-variables/">Global variables</a>
</li>
<li>
<a href="http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/globals.html">C++: Global warning</a>
</li>
<li>
<a href="https://www.securecoding.cert.org/confluence/display/c/DCL19-C.+Minimize+the+scope+of+variables+and+functions">Minimize the scope of variables and functions</a>
</li>
</references>
</qhelp>