I often find myself returning a boolean from a method, that's used in multiple locations, in order to contain all the logic around that method in a single place. All the (internal) calling method needs to know is whether the operation was successful, or not.
I'm using Python but the question isn't necessarily specific to that language. There are only two options I can think of
- Raise an exception, though the circumstances are not exceptional, and remember to catch that exception in every place the function is called
- Return a boolean as I'm doing.
This is a really simple example that demonstrates what I'm talking about.
import os
class DoSomething(object):
def remove_file(self, filename):
try:
os.remove(filename)
except OSError:
return False
return True
def process_file(self, filename):
do_something()
if remove_file(filename):
do_something_else()
Although it's functional, I really dislike this manner of doing something, it "smells", and can sometimes result in a lot of nested ifs. But, I can't think of a simpler way.
I could turn to a more LBYL philosophy and use os.path.exists(filename)
prior to attempting deletion but there's no guarantees the file won't have been locked in the meantime (it's unlikely but possible) and I still have to determine whether the deletion has been successful or not.
Is this an "acceptable" design and if not what would be a better way of designing this?