Imagine to have this code:
class Foo:
def __init__(self, active):
self.active = active
def doAction(self):
if not self.active:
return
# do something
f=Foo(false)
f.doAction() # does nothing
This is a nice code; I actually have (not in Python) a global active variable called "dosomething" and a routine called "something," where the first thing happening inside the "something" routine is if not dosomething return.
An alternative implementation would call to a routine that always performs an action, and the flag is checked at invocation time, as in the following code:
class Foo:
def doAction(self):
# do something
doaction = False
f=Foo()
if doaction:
f.doAction()
What is your opinion on this? I personally find that the first solution violates the least surprise principle: The caller is invoking an action which is never performed in response to a status which has been set somewhere else, but from the code it looks like the action is performed.
Would you consider it a total pattern, a total antipattern, or just an option with no strong opinion for or against it?