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.
Latest comments (25)
There are situations where nesting is necessary, in all other you will do better with a flat solution.
Nesting conditional logic is not always bad.
Nesting promises is always less clear than it could be.
Consider:
alphatakes a url and returns a promise of an http response.betatakes a url, callsalpha, and returns the promise of some heavy async calculation of an http response.Versus in un-nested:
alphatakes a url and returns a promise of an http response.betatakes 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
gammathat 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!I recently took a function with many nested ifs to modify two variables and rewrote it to return immediately instead. It was much more readable but didn't work when put into testing, but the point is that nesting makes things more complicated and harder to read.
That can be necessary; required behaviors can be complex. But it's kinder to your brain and those of the next dev to flatten.
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.
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.
Nesting when you write is very useful, given its obvious in your editor.
Nesting in output, probably not useful at all.
Nesting when writing logic, i would say, i try to do as little of that as possible, because parsing that in my brain is a chore.
This is the answer I came for. :)
Without proper deep levels of nesting how can I post my hadoken gif?
😄
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.