I was reading this post...
And I think we can generally agree on the idea here.
It got me wondering where nesting isn't bad, because in a lot of situations, nesting is a problem.
Is nesting in markdown a smell in and of itself?
<section>
<div>
<div>
<div>
...
Is this good or neutral or is it something to be avoided when possible?
Is nesting conditional logic always bad, or are there situations where it's more helpful?
This is broad and abstract, so feel free to take it in any direction.
Top comments (25)
I'm kind of confused by the premise. Certainly one should avoid driving nails with the butt of a hacksaw if there's anything better to hand. That doesn't make hacksaws bad. Hierarchization and nesting are organizational tools; sometimes they suit, sometimes they don't.
When it comes to conditional logic, I always try to ask myself 'how important is readability for this code block' i.e is this something that would change as my project progresses, and further than that, am I the only maintainer of the code? If it is highly volatile code (subject to change often) and if I am not the sole maintainer, then I err on the side of rewriting nested conditionals. I don't think nested blocks are generally bad, but I can't think of a case where they outperform inversion of conditions to single statements, except I guess in terms of mental overhead.
So for instance I would tend to write this
Instead of this
So I guess the question is more down to the conditions the code exists in, rather than the immediate computational benefits of nested vs alternatives?
This is a really great question
And now with monads....
Please note that this example is bad because it appears to rely on side effects
I have googling to do it seems
Please note that the brevity of my example relies upon a strong type system with at least basic generics support.
Interesting google terms:
Discriminated Unions
Functional programming
Monads
Haskell (or Hindley-Milner type systems)
Lodash (decent javascript functional library, though this may be a little opaque unless you experiment with a FP language for a while)
Thank you for this. I'll add it to this weekends reading list.
And what about:
Easy to read and no nested code.
That works too. The lack of braces is just my lazy inner java dev showing
From a Python perspective, we are informed by the Zen of Python
Python provides many patterns that allow one to avoid nesting in most cases. Of course, it's not altogether unavoidable, but you should try to avoid going any deeper than 2-3 levels, functions included.
It's (sometimes) horrible how frameworks/tool generate HTML but also necessary in some cases and not a big deal for developers as those tools handle those lines themselves. On the other hand if written by human it is something that should be avoided as much as possible.
Reactive style made it possible to have no idea what you wrote last month, let alone last year. Now it can be written better etc but there's a reason async/await was added.
Although it's a bit uglier and harder to write. with microservices reactive style is good enough in terms of clearness as your code is split. Now a lot of people would go with Go (pun intended), Kotlin etc for those prefix_routines but
which is quite good when having more than 1 technology. Now you do write either more functions or less clear code and there's no "a matter of taste" in it as it turns out you can take a deep look at previous iterative codes and compare to reactive ones and the actual problem starts with nesting
If you try to write this more clearly you end up either with more lines or more functions which convert stuff, zip, publish, toIterrable etc.
You need to deal with complex/ugly code. You need to couple some stuff. You need iterative approach sometimes. You need nesting sometimes. It's just a thing we have to live with :D
Nesting conditional logic is not always bad.
Nesting promises is always less clear than it could be.
Consider:
alpha
takes a url and returns a promise of an http response.beta
takes a url, callsalpha
, and returns the promise of some heavy async calculation of an http response.Versus in un-nested:
alpha
takes a url and returns a promise of an http response.beta
takes a http response and returns a promise of some heavy async calculation of that responseAll nesting can be expressed this way. (Convince me otherwise)
This property of monads is why Clojure has a threading macro, Haskell has pointfree, and most ML derivatives have a concept of piping.
E.g. you can define function
gamma
that is expressed asalpha | beta
(Akabeta(alpha(x))
) that just takes a url and spits out a single promise of a processed http response, but still isn’t nesting!A few cases, sometimes we cannot extract out a function because the abstraction is bad for maintaining, however, we have a complex condition like:
if a && b && c
, in case I preferif a { if b { if c {} else {} } else {} } else {}
. But most of the time I can find out how to extract it as a new function, but if really cannot do that, simple-ugly is better than elegant-hiding-the-problem.All scoping is nesting.
I would suggest that scoping is data structure nesting, not control structure nesting, and therein lies a vitally important difference.
Everything's a data structure.
I depends on verbosity in my case.
In nested code i find my self often looking for where indented sections end.
If i don't have to do look for where things end, then i'm find.
Consequently, this is usually around 2-3 indentations.
After that i often start to run our of useful vertical screen-space and the moment a indented section cross into where i cannot see it, then it becomes a problem to track again.
Nesting control structures quickly creates very many possible code paths, which makes the code harder to read, harder to work on, and, perhaps most importantly, harder to test. For this reason, it should be avoided/minimized.
This is the answer I came for. :)