Rule Definition
Using global variables in short scripts can facilitate the rapid development of effective solutions. Python being originally a scripting language has the "global" keyword to declare in a given code block the scope of a variable as global. However use of global variables in OOP is strongly discouraged as it is contrary to encapsulation. Indeed, relying on global state can be a source of unexpected behavior because different elements in a code can freely modify it. Using global variables in Python can also have a negative impact on performance as code involving only local variables runs significantly faster.
Remediation
Create a static data member in the appropriate class to replace the global variable.
Violation Code Sample
status = 0
def set_status(value):
global status
status = value
Fixed Code Sample
class Status:
def __init__(self):
self.status = None
def set_status(value)
status = value
Related Technologies
Technical Criterion
Programming Practices - Modularity and OO Encapsulation Conformity
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.