question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor deeply nested if blocks

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
chrizandrcommented, Jan 5, 2019

Is there any OO pattern we can apply, like Observer on a property or something similar?

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.

1reaction
kartikpandey596commented, Feb 2, 2019

You can use flag to solve this problem. For example: Your present condition is:

If (condition):
     If (condition):
          ....
     else:
else:
    ...

The solution is:

If(condition):
     If(condition):
           ....   Before reaching the max situation pass check variable value to 1 when if condition is true
Otherwise its default value is 0
    else:
else:
    ...

And at last Check the condition

If(check==1):
    If(condition):
         .. Again continue the condition
    else:
else:
    ...```
In this way you solve this problem
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found