This might be the most important tip of all the tips covered which applies mainly to if-else statements.
Nesting clutters your code.
Where does the block end
It becomes hard to find where a particular block ends easily (especially if someone writes 8 or more nests 😖)
Unnecessary horizontal scrolling
Also, you might have to scroll horizontally to see what the complete code is since it does not fit your screen.
Nesting makes your code hard to understand.
While you are reading your nested block, you have to remember what all conditions preceded it.
The below guideline should counter that:
Max indent should be 2, in case of exceptions 3
Avoid the use of else.
Return/throw when not okay
By returning early you make sure that in each of your functions the last line of your function returns what it is actually supposed to return. This will make your code consistent.
Also, you would find that you avoid unnecessary nesting (which will calm your eyes 👁 and your linter). Thus, making your code easy to understand.
Bubble vs Gateway
Bad (left): Bubble-Style
Good (right): Gateway-Style
If deep nests exist? Separate loop and operation
Extract the logic to other readable methods or use streams (Java)/lambda or something equivalent in your programming language of choice.
Usually, there is a problem where you have to perform a bunch of operations on an array of objects.
I tend to create at-least** 2 functions** for this:
One that performs an operation on a single object.
Other calls the first function in a loop.
In case if something goes wrong, it is easier to debug it since I know what function has what responsibility.
Simplified Example: Link to Code
You have a bunch of students for which you calculate the average grade.
In the above BadGrader, you can see than the code of calculating the grades is within the loop. This is bad as we cannot use the same piece of code elsewhere in the program.
The BetterGrader separates the calculation part from the printing part.
GoodGrader removes the naive logic that was used to calculate average and replaces it with a much shorter code that makes use of inbuilt functions.
This is an example of chunking which will be explained more in the next post.
Index: Tips on writing Clean Code