Refactor deeply nested if blocks
See original GitHub issueI’m submitting a
- bug report.
- feature request.
Current Behaviour:
Currently the code in resources.py
has deeply nested if blocks which decrease the readability of the code, also it causes the code to violate PEP max-line-length constraint, trying to avoid that we use line breaks which makes the code even less readable.
Expected Behaviour:
Instead of using nested Ifs we should check every condition separately and return an appropriate response for that. Ex.
if condition_1:
if condition_2:
if condition_3:
do_something
else:
response for violation of condition_3
else
response for violation of conditon_2
else:
response for violation of condition_1
should become
if(!condition_1):
handle_it
elif(!conditio_2):
handle_it
elif(!condtion_3):
handle_it
else:
do_something
So to summarize instead of checking for conditions to be true we should check for false and handle it immediately.
Steps to reproduce:
Do you want to work on this issue?
No
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (11 by maintainers)
Top Results From Across the Web
How would you refactor nested IF Statements? [duplicate]
Just use a blnContinueProcessing variable and check to see if it is still true in your if check. Then if the check fails,...
Read more >How to Refactor Nested if Statements in C#? - MethodPoet
So, how do you refactor multiple nested if statements? The easiest possible way is to use guard clauses. A guard clause is an...
Read more >How to Refactor Your Complex Nested 'if-else' Code?
Let's say we have three nested layers, and we have three else if in each layer, then we have 3 ^ 3 =...
Read more >How to write clean deeply nested if..else statements - Medium
Let's look at how we can refactor a deeply nested if-else statement to improve maintainability. Consider the following example from a point-of- ...
Read more >Replacing nested if statements - Stack Overflow
This talks about replacing nested if's with guard-statements, that return early, instead of progressively checking more and more things ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll look into this and maybe see how we can change it. For now, I think shifting to the negative conditions to reduce the nesting would not be of much benefit, since code complexity would still remain the same, if not more.
Maybe remove the begginer tags, since one would need a deeper understanding of how Hydrus is handling things in order to refactor this and we might also have to use app context objects to group these conditions into functions.
You can use flag to solve this problem. For example: Your present condition is:
The solution is:
And at last Check the condition