Why you shouldn't nest your code

Recently I came across an excellent video that helps me to understand that I am a never-nester, and gives a great demonstration of how to refactor your code when the complexity from nesting becomes too much.

Merry Christmas!


I mean, I dont know that I consider the braces of the function definition to be ā€˜nestingā€™ levels (consider: In most C# code, that means youā€™re ā€œ3 levels deepā€ at the root level of your main code line, because the namespace, class, and main function opening braces) , butā€¦eh.

I dont particularly like extraction either. Functions are for repeated code or isolated execution (IE: Youā€™ve made One Function to Rule Them All, as opposed to breaking the code up reasonably), not just to make it so iā€™ve gotta dig through 12 layers of function call that are in different locations or different files, just because the code had some brackets in it.

There is probably some happy medium. Iā€™m definitely not a ā€œnever nesterā€, but I can (and do) still practice some of the things in the video.


I think the same as hutley. If a code is unique there is no need to put it in a function. especially if the function Name could not completely explain what the function does or the function name becomes long like hell to do so.

If the functionallity is clear and short to explain like ā€œcopyPersonā€ then it makes sense to create a function even if you only need it at one place. But if the function is something like ā€œgetAllPersonsWichAreOlderThanAndhasMoreSistersAsAndAreStillAliveā€ I would in-line the code if it is only needed at one place.

Even if unnested code is easier to read, it is mostly much harder to understand. Because the code I have seen mostly does contain many functions you do not know what exactly they do. So if you try to understand the code, you need to jump all over the different functions. This is much easier if it is only one long code without any function calls.

Yes, that is a very good video.

I am a nester! In some JavaScript I am nesting 9 deep. However, by using good comments I think my code is much easier to read than it would be if extracted into numerous functions. I note there were no comments in the code shown in the video.

In the video I really like how opening and corresponding closing braces are always in the same tab positions. My code is always like that except when I post on this forum!

Despite trying to keep down the number of functions, even sometimes by using HTML inline code, my code in question has 174 functions. I am not going to refactor into many, many more functions!

I like the idea of abstracting out code and do it a lot by I tend not to extract to extract to seperate methods within a class, but to separate classes. Even if a class is only used once I still like to seperate it out because it makes testing easier and it more clearly separates concerns.

The longer Iā€™ve worked this way the more I donā€™t see the point of having private methods at all.

As Iā€™ve read once ā€œa private method is a hard coupling to an anonymous collaboratorā€ and I couldnā€™t agree more.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.