Global variables can be dangerous and cause bugs because they can be simultaneously accessed from multiple sections of a program. Most global variable bugs are caused when one function reading and acting on the value of a global variable before another function has the chance to set it to an appropriate value. We recommend using a configuration module to mutate global state.
1def dangerous_global_noncompliant(w):
2 # Noncompliant: uses global variable, which can be accessed
3 # from multiple sections.
4 global width
5 width = w
1def dangerous_global_compliant(w):
2 # Compliant: avoids using global variables, restricting
3 # the scope to this method.
4 width = w
5 return width