Search code examples
pythonif-statementstructure

How do I structure if-statements for maintainability?


I have a package for a very specific purpose that is imported into many other systems and it consists of a lot of if statements. Is there a way of rewriting the code so that it can be imported into an additional system with slightly different requirements (all additions to the list of if statements) while maintaining backwards compatibility?

Vastly simplified current version:

def analyze(result):
    a, b, c, d = result
    value = ''
   
    if a > 50:
        value += 'Y'
    else:
        value += 'N'

    if a == 1 and b == 1 and c == 1 and d == 1:
        value += '1'
    elif a == 2 and b == 1 and c == 1:
        value += 'A'
    elif a == 1 and b == 1 and c == 1:
        value += '2'

    ...

    if d > 100:
        value += '24'
    else:
        value += '0'
    

    return value

There's no particular logic to the return values.

Desired functionality:

def analyze(result):
    a, b, c, d = result
   
    if a > 50:
        value += 'Y'
    elif a > 25 and b > 50:                              # new line added here
        value += 'X'                                     # new line added here
    else:
        value += 'N'

    if a == 1 and b == 1 and c == 1 and d == 1:
        value += '1'
    elif a == 2 and b == 1 and c == 1:
        value += 'A'
    elif a == 1 and b == 1 and c == 1:
        value += '2'

    elif a == -1 and b == -1 and c == -1 and d == -1:    # new line added here
        value += 'False'                                 # new line added here
    elif a == -2 and b == -2 and c == -2 and d == -2:    # new line added here
        value += '3'                                     # new line added here

    ...

    if d > 100:
        value += '24'
    else:
        value += '0'
    

    return value

My current approach duplicates the code, which means maintaining it in two places. I wondered if it might be possible to structure the code needing the additions in such a way that it could import the original function and make some additional checks before or after it, although this changes the order. Is there a better way of doing this?

The original code is used in a lot of places and providing updates across so many other systems isn't an option - my code will only be using it in one place.


Solution

  • I have a solution to this, although it's not a great one. analyze() is rewritten to take in an additional argument which links to the else statements.

    def analyze(result, is_experimental=False):
        a, b, c, d = result
        value = ''
       
        if a > 50:
            value += 'Y'
        elif not is_experimental:
            value += 'N'
    
        if a == 1 and b == 1 and c == 1 and d == 1:
            value += '1'
        elif a == 2 and b == 1 and c == 1:
            value += 'A'
        elif a == 1 and b == 1 and c == 1:
            value += '2'
    
        ...
    
        if d > 100:
            value += '24'
        elif not is_experimental:
            value += '0'
        
    
        return value
    

    This ensures that the code maintains backwards compatibility with anything that currently uses it.

    The main part of the code that uses this particular function now needs to parse the output and makes a few substitutions using regex and positional searches. There is likely a better way of doing it but it can now be used in both systems and any updates to the original function should be reflected in the new system.