How can I improve this boolean function?
Question: 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 works, its a bad design. How can I improve this?


Answer: Building upon @Joran Beasley's answer, I'd put the something else in an else
block:

    
    
    class DoSomething(object):
    
        def process_file(self, filename):
    
            do_something()
    
            try:
                os.remove(filename)
            except OSError:
                return False
            else:
                do_something_else()
    

