4 ms·
I've heard this "no more than 3 levels of indentation" thing before, but isn't that somewhat language specific? If in coding in python, I have 1 level for being
by SmooL 5y ago
I've heard this "no more than 3 levels of indentation" thing before, but isn't that somewhat language specific? If in coding in python, I have 1 level for being inside a class, 1 level for being inside a function, and that gives me only 1 level to write actual program flow with. I find it hard to believe that a double nested for loop, having 4 levels of indentation, is in-itself a sign that I have to "fix my program".
- smnrchrds 5y agoList comprehensions in Python allow you to avoid having to add another level of indentation. If the feature did not exist, a lot of Python code would be one more level deep. Language features definitely have an effect on the number of indentation levels you need. Map and reduce reduce the number, Node-style callbacks increase it, etc. Although I suppose a Linux kernel developer would consider having to write Node JS code as 'being screwed'.
- 0xdeadb00f 5y agoPersonally I agree with the sentiment of no more than three indentations, but I don't take it as a hard rule (mostly because I'm not writing kernel code). I think it is very language specific. In Python I'd probably ignore the "rule" outright.
- asciimov 5y agoThe 3 levels of indentation, is about being mindful of runtime complexity. For example, do you really need four or more levels of for loops in a function. This "rule" is very domain specific. If you are writing for the Linux Kernel. You should rethink your strategy if you need something that is very nested. However there are places that where such nesting might be the favorable way to do a task so that the code maintains long term readability. Most often this is for something that runtime complexity isn't a important factor. I've seen this most often in document and report generation.