# How to simplify a lot of if... elif... else code?

Posted by Amit Rathi on Tue, 01 Mar 2022 04:49:16 +0100

In daily code, we always face a lot of problems of if... elif... else conditional branch selection. To tell you the truth, the first thing I recommend in most cases is to write honestly
if... elif, and try to extract the content under each branch into independent functions. Clear structure and clear intention are great convenience for writing and reading. However, in some special cases, other more elegant writing methods can be used, such as "don't use" we shared before
If elif statement, how to judge the rank of a number gracefully, As well as this article to be shared today, you can broaden your thinking of coding.

Today, I read the code of EdgeDB in Github and found that it uses a very clever decorator when dealing with a large number of if... elif... else judgments. Let's see what this method looks like.

Today is double eleven. Suppose we want to make a function to judge the discount that can be obtained according to the user's level. The conventional if... elif... Is written as follows:

```def get_discount(level):
if level == 1:
"Mass calculation code"
discount = 0.1
elif level == 2:
"Mass calculation code"
discount = 0.2
elif level == 3:
discount = 0.3
elif level == 4:
discount = 0.4
elif level == 5:
discount = 0.5
elif level == 6:
discount = 3 + 2 - 5 * 0.1
else:
return discount
```

As we all know, such a large amount of if... elif... Code is very ugly and difficult to maintain. Each if has a lot of internal code. This function will be pulled very long.

Some students know that they can use the dictionary to rewrite this too long if judgment:

```def parse_level_1():
"Mass calculation code"
discount = 0.1
return discount

def parse_level_2():
"Mass calculation code"
discount = 0.2
return discount

def parse_level_3():
"Mass calculation code"
discount = 0.3
return discount

def parse_level_4():
"Mass calculation code"
discount = 0.4
return discount

def parse_level_5():
"Mass calculation code"
discount = 0.5
return discount

def parse_level_6():
"Mass calculation code"
discount = 3 + 2 - 5 * 0.1
return discount

discount_map = {
1: parse_level_1,
2: parse_level_2,
3: parse_level_3,
4: parse_level_4,
5: parse_level_5,
6: parse_level_6,
}

```

But the method I learned today is simpler than using a dictionary. Let's look at its effect first:

```@value_dispatch
def get_discount(level):

@get_discount.register(1)
def parse_level_1(level):
"Mass calculation code"
discount = 0.1
return discount

@get_discount.register(2)
def parse_level_2(level):
"Mass calculation code"
discount = 0.2
return discount

@get_discount.register(3)
def parse_level_3(level):
"Mass calculation code"
discount = 0.3
return discount

@get_discount.register(4)
def parse_level_4(level):
"Mass calculation code"
discount = 0.4
return discount

@get_discount.register(5)
def parse_level_5(level):
"Mass calculation code"
discount = 0.5
return discount

@get_discount.register(6)
def parse_level_1(level):
"Mass calculation code"
discount = 3 + 2 - 5 * 0.1
return discount

discount = get_discount(3)
print(f'For Level 3 users, the discount is:{discount}')
```

The operation effect is shown in the figure below: Writing in this way is more intuitive than using a dictionary and more concise than using if... elif... Directly.

So, this decorator_ How is dispatch implemented? The password is hidden in the source code  of the open source project EdgeDB. The core code is only more than 20 lines: In addition, it can realize or query. For example, when the user level is 2 or 3 and the discount is 0.2, the code can be written as:

```@get_discount.register(2)
@get_discount.register(3)
def parse_level_2(level):
"Mass calculation code"
discount = 0.2
return discount
```

The operation effect is shown in the figure below: Its code can only realize equal query at present. But in fact, as long as the code is slightly modified, we can judge greater than, less than, greater than or equal to, less than or equal to, not equal to, in and so on. If you are interested, please leave a message at the bottom of the article. Tomorrow we will talk about how to transform this code and realize more logical judgment.

## Post push, learning and communication

We need a lot of front-end posts, python posts, Java posts, Android and iOS development posts. Working location: Beijing byte. Welcome to scan the QR code below for internal push

Python information, technology, courses, answers and consultation can also directly click the business card below to add the official customer service Qi ↓

Topics: Python Programmer crawler