Avoid pyramid branching #4
Labels
No labels
Bug
Feature
Feedback Wanted
Refactoring
Won't Do
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: waifu/kemoverse#4
Loading…
Add table
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
while True:
It's usually easier to read if the pyramid is inverted, for example you did well here:
https://git.waifuism.life/waifu/kemoverse/src/branch/master/bot/add_character.py#L25
especially in python, where you don't have brackets.
As a tule of thumb, just avoid doing more than 2 nestings in a function, and if it happens: invert the control flow
Okok I see the problem, I remembering Linus saying more than 3 nests in a function meant you needed another one. So I could write another function that does the notification stream, and keep the configuration loading in main so it can pass it over to the other function.
You don't even need more functions, just consider the starting point:
while true
is entirely wrapped in one hugeif notifications:
. So you can safely invert it, and just do:instead of
Everything that used to be within the
if
is now on the same level with it, so you just avoided a nesting with literally no effort.It can move to right before
notifications = client.i_notifications()
and yes, as you said, you can alternatively move everything to a function, so it's something like
that works too and is a way to approach it, whatever you like better
Think this can be closed when dev is next merged into master as most of the pyramid branching in the repository has been addressed over the past few days.