this post was submitted on 05 Sep 2023
165 points (79.6% liked)

Programming

17432 readers
205 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
you are viewing a single comment's thread
view the rest of the comments
[–] IRQBreaker@startrek.website 13 points 1 year ago (1 children)

As an embedded software developer that does linux kernel drivers I've come to love the tab size 8 indentation level.

I'm paraphrasing: "if your indentation level gets too deep, it's time to rethink/refactor your function."

And with tab 8 you'll notice it rather quick if your function does too much/unrelated stuff.

A function should be short and do one thing only, if possible. It also makes unit testing easier if that's a requirement.

[–] xigoi@lemmy.sdf.org -1 points 1 year ago (1 children)

When you're operating on such a low level of abstraction, it's no wonder you don't need deep nesting.

[–] IRQBreaker@startrek.website 1 points 1 year ago (1 children)

Oh, I've done my fair share of C++ and Python as well. But you got to agree with me that when you are on your fourth indented "if case" it's time to step back and think about what you are trying to achieve. I mean it's probably going to work, but probably also very hard to maintain that type of code.

[–] xigoi@lemmy.sdf.org 2 points 1 year ago (1 children)

How would you implement, for example, Gaussian elimination with at most 3 levels of nesting?

[–] JesperZ@programming.dev 2 points 1 year ago* (last edited 1 year ago) (1 children)

Abstraction.

The solution for all levels of nesting.

[–] xigoi@lemmy.sdf.org 2 points 1 year ago* (last edited 1 year ago) (1 children)

Be specific. Which exact part would you abstract away and how?

[–] JesperZ@programming.dev 3 points 1 year ago* (last edited 1 year ago)

There a many ways to implement abstractions, but it’s highly dependent on the language in question. You could simply refactor each level of nesting into its own function, with all dependents provided as parameters instead of scoped variables. You could then flatMap to avoid a bunch of nested looping, favoring a linear approach that’s often easier to reason about. You could go all out and refactor all your conditional statements away, in favor of the Either monad. You’d then have a number of functions, each doing one thing (including no nesting), and a main function gluing it all together, linearly. That is a pattern you can always apply; there’s nothing controversial about it, and on a similar note there’s nothing particularly challenging about Gaussian elimination.