119
Linear code is more readable (blog.separateconcerns.com)
you are viewing a single comment's thread
view the rest of the comments
[-] zygo_histo_morpheus@programming.dev 8 points 1 year ago* (last edited 1 year ago)

One downside with the code on the right is that it's not obvious where the different functions might be called from. In the example on the left, we know that we're not, say, adding toppings to a pizza that we've already baked. If we notice a bug in the topping adding function on the right, we might get tempted to reason about adding toppings as a general process that needs to handle all kinds of situations when in practice is doesn't.

When you have single use functions like this it's good to limit the scope of the function using whatever language features are available so that you can more easily reason about where it's being called from

[-] robinm@programming.dev 1 points 1 year ago* (last edited 1 year ago)

One way to make it obvious which function can be called at which state is to use different type. Like UnbackedPizza and CookedPizza, and the bake function takes the former and returns the later.

this post was submitted on 15 Sep 2023
119 points (89.9% liked)

Programming

17314 readers
548 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS