-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce indentation by inverting if
conditions
#357
Conversation
Some of these conditionals and loops are also very long, and for those the rule of thumb I was taught was to avoid any such section that is longer than one editor screen. (aka refactoring is advised if/when one ends in that situation) |
@bsipocz I totally agree, just trying not to spend that much time on this code right now. The changes I made here were some low-hanging fruit that will make the rest of my review a little easier. This tends to happen a lot with these notebooks, and I think it would be good for us to come up with a small number of relatively simple things the scientists can think about / do to avoid ending up in this situation with future code. |
Yes, I totally meant it for that simple list of guidelines, rather than to fully rewrite of what we have. (OTOH, you practically did a couple of the refactorings in this PR, so we can even link to this as real life example of how to refactor a long conditional by e.g. inverting) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All looks correct, thanks!
|
||
# If multiple entries are found, pick the closest. | ||
# Or should we take the average instead?? | ||
if len(tab) > 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code block (lines 50-62) is weird in two ways:
- It seems to think the condition was
if len(tab) > 1:
instead ofif len(tab) > 0:
(which would make more sense given that the== 0
case was already handled). - The resulting variable
tab_final
is not used. Maybe the following line of code was supposed to use it instead oftab
?
The diff in this PR makes it difficult to look at, and I'd rather not make substantive changes in this PR anyway. Opening an issue to be addressed separately. (#358)
Reduce indentation by inverting `if` conditions 63d4dfa
Some of the spectroscopy functions have quite a few levels of indentation (almost 10 in some cases) due to nested
for
loops andif
statements. This tends to squish the code into a narrow region on the right making it more difficult to read. This PR reduces some of the indentation by inverting the conditions ofif
statements where possible.Simple example with just 2 levels (benefits are greater when there are more levels). This:
becomes this: