1

Let's say I have a function that returns a dataset. First it tries to read it and if that fails it is requested from an API and then written:

def get_dataset():
    try:
        df = pd.read_csv('dataset.csv')
    except FileNotFoundError:
        df = pd.DataFrame(api.get_dataset())
        df.to_csv('dataset.csv')
    return df

Now we may want to force making the request and update the CSV file, so we pass a flag. In order to be more concise with the code, we can use a cheap trick to force the execution of the except block:

def get_dataset(force_udpate=False):
    try:
        if force_update:
            raise FileNotFoundError
        df = pd.read_csv('dataset.csv')
    except FileNotFoundError:
        df = pd.DataFrame(api.get_dataset())
        df.to_csv('dataset.csv')
    return df

Of course this looks bad, because a FileNotFoundError is not really applicable. But if we didn't do this we would either have to break the DRY principle:

def get_dataset(force_update=False):
    if force_update:
        df = pd.DataFrame(api.get_dataset())
        df.to_csv('dataset.csv')
        return df
    try:
        df = pd.read_csv('dataset.csv')
    except FileNotFoundError:
        df = pd.DataFrame(api.get_dataset())
        df.to_csv('dataset.csv')
    return df

Or introduce a local function, dirtying the whole function a bit:

def get_dataset(force_update=False):
    def request_and_write():
        d = pd.DataFrame(api.get_dataset())
        d.to_csv('dataset.csv')
        return d

    if force_udpate:
        return request_and_write()
    try:
        df = pd.read_csv('dataset.csv')
    except FileNotFoundError:
        df = request_and_write()
    return df

Is that questionable practice acceptable for the sake of code simplicity and clarity?

dabadaba
  • 2,216
  • 6
  • 25
  • 35
  • 2
    You are not violating dry as long as you are catering to different scenarios. Exceptions are thrown to signal exceptional conditions that require special attention. So they 'd better be clear and not deceptive or puzzling. Your laziness will be another man's waste of time. – Martin Maat Mar 04 '18 at 12:51

1 Answers1

1

Never use exceptions to control program flow or "for convenience". It couples the code in a way it shouldn't be.

Exceptions are meant to indicate exceptional cases and error situations which cannot be handled at the place where they are thrown.


For your particular case you might prefer to test the file for existence in first place, instead of relying on the exception.
It is not really an exceptional case for your code that the files might not exist at that point, and should be handled with some clear code.

πάντα ῥεῖ
  • 1,540
  • 3
  • 12
  • 19