Menu

Post image 1
Post image 2
1 / 2
0

Stop nesting deeply

DEV Community·Ian Johnson·18 days ago
#eOewe1Bj
Reading 0:00
15s threshold

Open a function and let your eyes drift to the right edge of the screen. If the code is leaning over halfway to that edge by line ten, the function is in trouble. Maybe not in a way that breaks tests (deeply nested code can be perfectly correct) but in a way that breaks comprehension. Every level of indentation is another condition the reader has to hold in their head to understand what the innermost line means. Five levels in, the reader is tracking five separate predicates, and the actual work is squeezed against the wall. This isn't a new observation. The JavaScript community has a name for the worst case: callback hell. It's the staircase of function(err, result) { followed by another, and another, each indented further than the last, until the actual business logic (the reason the code exists) is buried so far inside that you have to scroll right to read it. The escape was promises, then async/await , but the underlying problem isn't specific to callbacks.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More