Nesting if clauses vs cascade return vs assertion

If a negative evaluation in the if argument causes a return call inside the function / method, what is more recommended in Python to nest if statements or use a reverse evaluation and call the return function? eg:.

 if required_condition_1: if required_condition_2: if required_condition 3: pass return 'error for condition 3' return 'error for condition 2' return 'error for condition 1' 

Or:

 if not required_condition_1: # preparation... return 'error for condition 1' if not required_condition_2: # preparation... return 'error for condition 2' if not required_condition_3: # preparation... return 'error for condition 3' # if runtime reaches this point it means that it has passed all conditions 

Imagine that you have to register a user and you need different conditions to satisfy. A user will only be registered if they are all satisfied, but error messages depend on which condition fails.

My assumption is that in other cases, as the user mentions in the answer section, other actions may be taken if any condition fails. Then I think I should attach ifs. However, I will only return an error message, so I think the second option is preferable.

I also thought about the statement:

 try: assert(required_condition_1) try: assert(required_condition_2) # do tasks except AssertionError: # error for condition 2 except AssertionError: # error for condition 1 

Despite the fact that I think this last method is not highly recommended, as in exception handling. Just as the SO user mentions :

If the code is correct, the prohibition of one-time events, hardware failures and such, no statement will ever fail. This is why the behavior of the program for the end user should not be affected. Especially, they say they cannot help but cope even in exceptional program conditions. It just never happens. If this happens, the programmer must be locked for it.

I know that this may seem mainly based on opinions, but for me it is not , because all languages ​​have stylistic recommendations that create a more stable, scalable and readable environment depending on its characteristics and functionality. I would like to know what if there is a recommended way to address this issue within methods and , most importantly, why .

+5
source share
2 answers

Although this can be considered as based on opinions, I believe that objectively the second method seems more readable.

In your first example, someone reading your code will have to decrypt so that each statement is not logically related to the one in which it is embedded, something you don't want. In the general case, nested blocks imply some inherited logic.

The second example is much more accurately written and does not require much thought to implement the flow of logic. Instead of stepping deeper and deeper than the more conditions that you want to apply, the logical flow seems to imply that each of them must be satisfied independently.

The first style has its own precedents, but not for this task: checking the state.


As for your second question, the use of statements in this use case will not be considered appropriate. As a rule, use only assert when something that should never go wrong, but very important for program execution, goes wrong. One of the most obvious uses is to record test cases, the function should give a certain result, and it is very bad if it does not give you this conclusion.

Exceptions are what you might expect to make a mistake, for example. division by zero errors, attribute errors, etc. when working with user input.

+3
source

As Ziyad Eder answered, the second is more readable:

 if not required_condition_1: # preparation... return 'error for condition 1' if not required_condition_2: # preparation... return 'error for condition 2' if not required_condition_3: # preparation... return 'error for condition 3' 

And, if you want to have only 1 return , then:

 if not required_condition_1: # preparation... _error = 'error for condition 1' elif not required_condition_2: # preparation... _error = 'error for condition 2' elif not required_condition_3: # preparation... _error = 'error for condition 3' else: _error = None return _error 
+2
source

Source: https://habr.com/ru/post/1274322/


All Articles