I have a pretty big project, due to complicity and privacy I would like to just post the meaning of my problem.
def H():
B()
...
# return with something else
def G():
...
B()
# return with something else
def F():
dosomething(counter, ...)
...
def E():
...
def D():
...
B()
...
# return with something else
def C():
...
def B():
...
if success:
global counter += 1
...
def A():
B()
C()
D() # the list goes on...
...
def main():
try:
A()
except Exception as e:
...
else:
...
counter = 0
if __name__ == '__main__':
main()
As you can see, the problem is that there is a global variable which is incremented each time a function called B() with a successful outcome.
I did my research on stackoverflow, most people are cursing at global variables that are not simply used as contants and that they are evil and they should be avoided at ALL COSTS.
Most people didn't really explain the whys and the main reason for that, but some people explained that global variables should be avoided because of increasing complexity, readability and confusion, which I absolutely understand and see if used improperly.
As a solution, most people said that it would be using returns, specifically with a tuple (returning with two values).
However in my case, the project is pretty big and I made a list of pros and cons of using a global variable and returns
Pros of using global variable in my case
- Global variable is incremented by only one function, which is called by other functions, thus no complexity or confusion
- costs me literally 2 lines of code
Cons of using global variable in my case
- ?
Pros of using returns instead of global variable in my case
- ?
Cons of using returns instead of global variable in my case
- I have to rewrite basically everything, because I have to pass counter to main, then to D(), H(), B(), G(), A(). I have to rewrite in-function variables so that they used the correct returning element, I have to rewrite parameters, I have to rewrite arguments, I have to edit classes. It's a huge mess.
So in my case, I absolutely disagree with using returns instead of a global variable.
Using global variable -> literally two lines of code, simple and easy and easily understandable, intuitive.
Using returns -> probably 50 - 100 lines to be rewritten and spending time fixing classes and objects, increasing confusion and complexity.
Is there something I don't see? Do you have something in mind?