Avoid pyramid branching #4

Open
opened 5 days ago by nai · 3 comments
nai commented 5 days ago
Collaborator

b8493440bd/bot/bot_app.py (L29)

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

https://git.waifuism.life/waifu/kemoverse/src/commit/b8493440bdf8dbb87535f403a49135ae05f89c54/bot/bot_app.py#L29 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
nai added the
Refactoring
label 5 days ago
waifu was assigned by nai 5 days ago
Owner

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.

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.
Poster
Collaborator

You don't even need more functions, just consider the starting point:

  • The whole while true is entirely wrapped in one huge if notifications:. So you can safely invert it, and just do:
if not notifications:
  continue
  
notifications.reverse()
new_last_seen_id = last_seen_id
for notification in notifications:
  // 2 spaces!

instead of

if notifications:
  notifications.reverse()
  new_last_seen_id = last_seen_id
  for notification in notifications:
     // now 4 spaces

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.

B-but, my sleep(5) !

It can move to right before notifications = client.i_notifications()

You don't even need more functions, just consider the starting point: - The whole `while true` is entirely wrapped in one huge `if notifications:`. So you can safely invert it, and just do: ``` if not notifications: continue notifications.reverse() new_last_seen_id = last_seen_id for notification in notifications: // 2 spaces! ``` instead of ``` if notifications: notifications.reverse() new_last_seen_id = last_seen_id for notification in notifications: // now 4 spaces ``` 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. > B-but, my sleep(5) ! It can move to right before `notifications = client.i_notifications()`
Poster
Collaborator

and yes, as you said, you can alternatively move everything to a function, so it's something like

notifications = client.i_notifications()
process_notifications(notifications)
time.sleep(5)

that works too and is a way to approach it, whatever you like better

and yes, as you said, you can alternatively move everything to a function, so it's something like ``` notifications = client.i_notifications() process_notifications(notifications) time.sleep(5) ``` that works too and is a way to approach it, whatever you like better
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

No dependencies set.

Reference: waifu/kemoverse#4
Loading…
There is no content yet.