23

Just curious. The most I have ever had was a for loop within a for loop, because after reading this from Linus Torvalds:

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

https://www.kernel.org/doc/Documentation/CodingStyle

I figured it was an unacceptable practice for me to go to a third layer of looping, and would restructure my code (Primarily Qt).

Was Linus joking?

Does it depend on the language or application?

Are there some things which absolutely need three or more levels of looping?

Anon
  • 3,565
  • 3
  • 27
  • 45
  • 1
    Of course there are. The point is not that you should not have nesting if the task requires it, but that you should not have that *within one method*. – Kilian Foth Dec 23 '15 at 12:06
  • 8
    I get confused why you jump from indentation to levels of looping? You have a large quote discussing indentation and suddenly from that follows a question about nested loops. – Pieter B Dec 23 '15 at 12:11
  • 3
    @PieterB Well, if your loop is nested, you need to indent at least one level. The implication from this developer is that if you have indented up to the 4th layer, then your program needs fixing. – Anon Dec 23 '15 at 12:13
  • 5
    Linus is probably not (only) joking in that section, but note that this is only one style guide, and the same style guide stresses that "Kernel coding style is super simple", i.e. more so than other styles. –  Dec 23 '15 at 12:18
  • 5
    @Akiva You can't go through a 4-dimensional matrix without having 4 nested loops. I find it insane that someone would limit the amount of nested loops you can have. Linus was obviously being very general and you shouldn't take everything you read as holy scripture. – Alternatex Dec 23 '15 at 12:20
  • 9
    @Alternatex That you need 4 loops does not mean they have to be *lexically* nested. It is quite obvious from the quote that we're talking about how to organize the code, not about the execution. –  Dec 23 '15 at 12:26
  • 2
    Then again, Linus Torvalds often programs (or has programmed) on terminals that are exactly 80 characters wide, so what does he know? Restricting lines to only 80 characters may be an okay guideline, but bending over backwards to stay under this arbitrary limit is a complete waste of time, and readability. – Hugo Dec 23 '15 at 13:24
  • 4
    @delnan I'm not saying 4 nested loops are visually pleasing and I'm aware there are other ways to go about it but I find it silly how OP took Linus' words so literally. 4th level of indentation = end of the world. Give me a break. – Alternatex Dec 23 '15 at 13:30
  • 2
    Indentation styles definitely depend on the language. Many functional or functional-inspired languages, such as Haskell or Scala, use 2-space indentations. The "shape" of the code you write is very different to C. If you tried to use 8-space identation in those languages, it'd look weird and nonstandard. – Andres F. Dec 23 '15 at 13:57
  • 1
    @Alternatex the point is that if you try to commit code to Linux kernel that goes through a 4-dimensional matrix in 4 nested loops, it most likely should be rejected as such, no matter how it's indented - the point of the Linus recommendations is that also such algorithms generally have no place in kernel code; if your solution to some kernel problem is to use a 4-dimensional matrix in this way then you should really rethink if that solution is appropriate for this domain. – Peteris Dec 23 '15 at 15:17
  • 2
    Taboos are for people who don't understand what they are doing or why they are doing it. If you are a competent developer, then you do what you know to be right. (Hint: If your organization has style guidelines, then it's right to follow them. It's also right to change them if you can convince your peers that they don't make sense.) – Solomon Slow Dec 23 '15 at 15:44
  • 1
    There's two thing in here you need to take into consideration. Tabs are 8 characters and the screen size is 80 character wide. If you are programming on a system that have those two properties, then 3 level of indentation is a good limit to read to code properly without needing to scroll horizontally all the time. – the_lotus Dec 23 '15 at 16:29
  • 1
    If you read the whole document, he takes the time to call some people heretics and some people prophets. Just because Linus Torvalds is the High Priest of his Church and gets to decide who sits in what pew doesn't mean you have to follow his hokey religions and use his ancient weapons. – corsiKa Dec 23 '15 at 16:59
  • 3
    Honestly, tldr everything here. However, Linus Torvalds also thinks people who can't debug the Linux kernel without a debugger shouldn't be programmers. I disagree with him on multiple subjects. http://lwn.net/2000/0914/a/lt-debugger.php3 "And quite frankly, I don't care. I don't think kernel development should be "easy". I do not condone single-stepping through code to find the bug." Now, go look through Stack Overflow and see how often using a debugger is the recommended troubleshooting method. – Steve Dec 23 '15 at 17:43
  • @Steve was that in the same vain that he doesn't want git to be in C++ because: `It's made more horrible by the fact that a lot of substandard programmers use it, to the point where it's much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C.` ? I remember him saying that he likes the kernel in C, because it forces developers to think like a machine. Isnt the principle that he just wants to root out those who dont understand code in a machine context? – Anon Dec 23 '15 at 19:50
  • 1
    Note that a "taboo" is something you are not allowed to talk about. Since we are discussing the subject, deeply nested loops are not taboo. – gnasher729 Dec 23 '15 at 20:26
  • 3
    @Akiva Seems like a stupid reason to choose a language to solve a problem. There are horrible C programmers out there too, that understand machine context and just write bad code. But this isn't related to the OP's question, except that I wouldn't take what Linus Torvalds says as gospel. This question is *about an opinion*. I'm just pointing that out. However, the point that indentation is a code smell isn't completely wrong, either. It can be a code smell for sure. – Steve Dec 23 '15 at 20:52
  • 3
    In C#, namespace => class => program. 3 levels of indentation right there. Apparently we are screwed before we begin! – JMK Dec 23 '15 at 21:40
  • 1
    @gnasher729: Actually, people talk about taboo topics all the time. A taboo doesn't physically prevent you from talking about the topic; it just makes it socially inadvisable. – Lightness Races in Orbit Dec 23 '15 at 21:47
  • 1
    Good grief, where is Linus looking where he sees bad C++ programmers yet not an equal number of _at least as bad_ C programmers directly adjacent? I've moved to C++, it seems, but I'm still fully in favour of C - yet I can find no shortage of terrible examples that (falsely!) make me wonder how I ever tolerated C. This is, at least, if we pretend many of the worst C++ programmers aren't so bad precisely _because_ they're still using it like bad C... eh, I'm confused. – underscore_d Dec 23 '15 at 21:55
  • 1
    @underscore_d Have you ever programmed C++ code that relies heavily on template metaprogramming? I have, and I've learned to hate it. Dearly. It just leads to too many indirections within the source, conceiling what's really going on. Still I meet quite a fair number of C++ developers on SO, to whom template metaprogamming is gospel. I guess, that is the same class of people that Torvalds wants to keep out of kernel and git programming. They just don't think KISS. Don't get me wrong: I do use both C++ and templates. They are a tools that have many good uses, the danger is overusing them. – cmaster - reinstate monica Dec 23 '15 at 22:51
  • 1
    @cmaster Interesting - didn't think of TMP. Maybe I'm biased to the simpler &, I'd argue, more evidently useful bits of C++. I teach myself it as I go along & my meandering course has me wading through templates a lot lately. Not sure I'd class mine as heavy/complex, but - as well as hugely useful _in moderation_ - I certainly see huge potential for horror. I guess if I had a point, it was the obvious: there are ways to write awful code in both languages, not to victimise C++. But closer to the hardware, C often makes more sense. Anyway, one might fairly speculate LT was exaggerating somewhat! – underscore_d Dec 23 '15 at 23:07
  • 1
    Linus Torvalds is basically a political pundit, but for programming - he says the most outrageous things possible, to get attention. See eg. [Supporting motherboards with UEFI is a "di**-sucking contest"](http://goo.gl/QhElT0); [real programmers don't use debuggers](http://goo.gl/0yYHOI); ["SVN is the most pointless project ever started"](https://goo.gl/CBw1cg); ["GNOME is a total UX failure"](http://goo.gl/m6GiKz); etc. You should just ignore him. – BlueRaja - Danny Pflughoeft Dec 23 '15 at 23:35
  • 1
    It seems like you're asking two different things here. Loops within loops are structural issue, and the discussion would be about time complexity and readability. This has little or nothing to do with the display width of tab characters. – Kevin Krumwiede Dec 24 '15 at 02:07
  • 2
    @BlueRaja-DannyPflughoeft While some of his wording is ill-advised, I think you're misrepresenting Linus in your 'headlines'; he either didn't say those things, was exaggerating in-character, or (in case of GNOME, I'd argue) was ultimately correct. None of that justifies saying "just ignore" such an influential developer; at the very least, folk should read _everything_ he says, adjust for his exaggerating style, and react to that. Don't cut-and-paste his words around to make a point of your own. – underscore_d Dec 24 '15 at 10:04

8 Answers8

49

To a degree, I stopped taking this quote seriously at "Tabs are 8 characters". The whole point of tabulators is that they are not a fixed number of characters (if anything, a tab is one character). What a load of tosh. Similarly, I'm not completely convinced that setting a hard-and-fast rule of "three levels of indentation" is sane (as much as setting a hard-and-fast rule for anything is sane).

However, limiting your levels of indentation is in general a reasonable suggestion, and not one that should come as a surprise to you.

Ultimately, if your program needs three levels of iteration, that's what your program needs. The spirit of the quote is not to magically alleviate that requirement from your project, but to hive off logic into functions and types so that your code is terser and more expressive.

This just feeds back into the same guideline given above regarding indentation levels. It's about how you structure your code and keep it readable, maintainable and fun to modify for years to come.

Lightness Races in Orbit
  • 8,755
  • 3
  • 41
  • 45
  • 6
    I believe the "declaration" that tabs are 8 characters are specifically in the context of kernel development. This quote is taken from a coding guideline for a specific project and isn't intended to be a general use guideline, and thus it is expected to be quite opinionated. – Lie Ryan Dec 23 '15 at 15:52
  • 6
    @LieRyan: Then it's still tosh - a coding guideline for anything has no business dictating how wide I set my tabs! But I suspect Linus knows that. – Lightness Races in Orbit Dec 23 '15 at 15:53
  • @LightnessRacesinOrbit I suspect Linus has been rather annoyed by people using 4 character-width tabs with 76, 72, or 68 additional characters after 1, 2, or 3 indentations, respectively, and then claiming that those are all 80 character lines. With Linus's preferred 8 character-width tabs, those lines are 84, 88, and 92 characters, respectively. – 8bittree Dec 23 '15 at 16:26
  • 2
    It's pretty rare to ***need*** 3 levels of indentation. It's more likely to happen when people avoid the use of guard clauses in favor of single point of return or are too lazy to encapsulate blocks of logic into separate functions. – Eric Dec 23 '15 at 16:27
  • 6
    and of course it's language-dependent -- in c# it is common that you indent inside your namespace, in your class, and in your method.. you're already at 3 levels of indentation before you even *talk* about control flow statement bodies being indented. – PeterL Dec 23 '15 at 16:55
  • 1
    @PeterL: I imagine the updated, non-C version of that common advice, would take into account indentation levels only within function bodies. – Lightness Races in Orbit Dec 23 '15 at 17:00
  • 1
    I agree, which I think highlights that it is more about complexity of nested control flow rather than about 8-space tabs and 80-character line lengths. – PeterL Dec 23 '15 at 17:05
  • 1
    @PeterL: That's certainly the important consideration, yes. – Lightness Races in Orbit Dec 23 '15 at 17:07
  • 3
    @LightnessRacesinOrbit I interpret the "Tabs are 8 characters" comment to not mean that you must personally view tabs as 8 wide in your editor, but for the purpose of other rules in the style guide (such as "The limit on the length of lines is 80 columns and this is a strongly preferred limit.") one must treat tabs as being 8 colums, this is also relevant to other rules regarding argument alignment in function calls. Again, I don't think the intent of that line is forcing you to view tabs that way at all, I have done kernel patching before with 4 wide tabs and reflowed the code at the end. – Vality Dec 23 '15 at 18:19
  • 2
    @Vality: That's been addressed already in the comments above. :) – Lightness Races in Orbit Dec 23 '15 at 18:27
  • 2
    It's also worth noting that Linus is a bit hypocritical here, as there are a number of places in the Linux kernel that use more than 3 levels of indentation. On the flip side, the Linux kernel code is surprisingly clean for that kind of low-level, critical code, and it does have the characteristic of favoring minimal nested blocks in favor of simpler functions (and surprisingly nicely-organized, not like a bunch of awkwardly forced "helpers"). –  Dec 23 '15 at 20:13
  • 3
    @LightnessRacesinOrbit: That coding guide is not about how you view your files. It's about how you save your files. If you want your kernel code to be accepted then you must save your files by indenting with spaces, not tabs, and each indent level must be 8 spaces. If your editor can handle this automatically then fine, view your tab any number of spaces you want. If not then you have no choice but to work with 8 spaces for tab. Personally I prefer tab to spaces (the standard Go style) but that's a discussion for another flamewar. – slebetman Dec 23 '15 at 21:39
  • 2
    @slebetman: _"If you want your kernel code to be accepted then you must save your files by indenting with spaces, not tabs, and each indent level must be 8 spaces."_ Another reason not to contribute ;) – Lightness Races in Orbit Dec 23 '15 at 21:46
  • 1
    @slebetman Is there another citation that makes that explicit? I'm confused by why someone as precise as Linus would say "tab" if he actually meant 'level of indentation, but woe betide ye who indent with a tab character'. The wording gave me the impression that tabs could be used and he was only referring to how the code should be _rendered_, with a view towards making it appear visually the same to all readers. – underscore_d Dec 23 '15 at 22:00
  • 1
    @underscore_d: Using the word "tab" to mean "an arbitrary number of spaces used to denote as a single indentation level" is sadly commonplace (presumably due to configuration of editors to produce said spaces upon pressing the Tab key), and Linus isn't God. (There can only be one God mehehehe) – Lightness Races in Orbit Dec 23 '15 at 22:08
  • 1
    I have no religious inclination to Mr T; my point is just that, usually, when he has an opinion, he words it very clearly - and usually very loudly - leaving little room for doubt. Having said that, the ambiguity isn't his; as you've shown, we might be talking about the tab character XOR Tab key. My assuming the character might just mean I've used too much regexp... – underscore_d Dec 23 '15 at 22:22
  • 4
    @underscore_d: It appears that I'm wrong: `Outside of comments, documentation and except in Kconfig, spaces are never used for indentation, and the above example is deliberately broken.` -- 6 paragraphs down from the quote in the OP. – slebetman Dec 23 '15 at 22:40
  • 1
  • 2
    @slebetman Classic Linus!! Haha, thanks for the update. – underscore_d Dec 23 '15 at 22:46
20

The kernel strongly prefers simple algorithms

While a variety of algorithms may require deeply nested loops within loops, in context of the Linux kernel (in which the quote was said) you generally need quick real time responses. In that context, deep nesting is a smell that may indicate that the code flow is too complex for this domain and may needs to be changed because of it's execution characteristics, not readability or indentation issues.

Furthermore, Linux kernel is different from most application code as for the requirements of auditability and testing - and thus would prefer to not have a 4+ level nested algorithm in a single function. It should be obvious to see what each code fragment does exactly and in detail, including all the possible control flow and edge cases. Deeply nested code hampers that.

Peteris
  • 1,097
  • 6
  • 9
  • So do you think that with lower level languages such as C, deeply nested loops are generally more taboo `because` projects utilizing lower level languages benefit from a coding style that focusses on simpler algorithms? – Anon Dec 23 '15 at 15:06
  • 4
    @Akiva I wouldn't tie it to lower level languages or C as such, but rather to the domain of code. I think that similar guidelines would apply for *any* language when writing code that *must* be robust, security focused and auditable at the expense of other things. E.g. an encryption library written in Java or Haskell should also be written in a style that keeps things as simple as possible, limits nesting, and tries to separate everything into chunks that can be easily analyzed with all their possible consequences. – Peteris Dec 23 '15 at 15:13
  • A very insightful and useful comment/answer. Just curious; what kind of project done today which utilizes a low level language, would not focus on being robust, audit-able, and secure? – Anon Dec 23 '15 at 15:18
  • 7
    @Akiva for example, machine learning code where you may want to use C just for performance reasons but don't care much about robustness or security since it will be run internally in controlled conditions. Also, implementing simple business functionality on small embedded microcontrollers - in practice this often has a business like focus on features and development speed at the expense of quality and security, but uses low level languages. – Peteris Dec 23 '15 at 15:26
16

The point is the same as for any flow-control constructs: if the code is hard to understand, you need to refactor it. If you're doing some simple manipulation of a multi-dimensional array, then having loops nested five or six deep may be appropriate, as long as the logic in the innermost loop is straightforward. However, if you're processing some complicated business logic and the body of your loop is a dozen lines or more, then you will probably not want to nest that more than one loop deep. You can try calculating the cyclomatic complexity of the code, but what it really comes down to is the readability and maintainability of the code in question.

TMN
  • 11,313
  • 1
  • 21
  • 31
  • 11
    Exactly. It's too easy to suggest Torvalds is a loon. (He is, of course.) He may be too rigid for your taste, but he's describing a real development concern that causes real problems. You don't have to do exactly what he says, but you should think about why he's saying it. – Scant Roger Dec 23 '15 at 13:38
  • 7
    @ScantRoger Actually, that Torvalds-quote only sounds too rigid if you don't get his sense of humor. As I remember, earlier on in the same document, he suggests to print out a copy of the GNU coding style guidelines, only to burn them in some sort of ceremony. You'll hardly take that seriously, do you? In this quote, his main point is to define indentation for the linux kernel to be eight spaces, nothing more, and nothing less, that is what he's rigid about. The last sentence is only to underline that point, not to say that you must not use more levels of indentation - no rigidness implied. – cmaster - reinstate monica Dec 23 '15 at 14:35
  • 1
    @cmaster Thanks for the context, right on! In answer to your query, I hardly take anything seriously. ;) – Scant Roger Dec 23 '15 at 15:02
  • 2
    @cmaster and then one reads his responses to github pull requests and line length of commit messages. He is a total nut-case. – Gusdor Dec 23 '15 at 15:28
  • 3
    Ceremoniously burning the GNU coding guidelines may not actually be necessary, but it is entirely in order at any point in time. – dmckee --- ex-moderator kitten Dec 23 '15 at 16:40
  • 1
    Iterating over nested data structures with multiple levels of looping is a pretty bad code smell if it happens more than about twice. You now have complex code tightly coupled to the data structure. What if that data structure changes and you need yet another layer? All those loops should be extracted into an iterating class. – kevin cline Dec 23 '15 at 20:49
13

Was Linus joking?

The piece is written in a playful style which suggests that the author is familiar with the way coding style is discussed among serious practitioners: We all have our preferences, and we defend them rabidly, but with tongue at least partially in cheek. We understand perfectly well that much of it is just a matter of personal taste. He says, in so many words, "Coding style is very personal, and I won't _force_ my views on anybody" -- at least outside of code he personally maintains. But consistency of style in a given project is a very good idea. I'd much rather code to a style I dislike than deal with multiple styles in a given function.

Here's an example of clearly playful writing:

However, there is one special case, namely functions: they have the
opening brace at the beginning of the next line, thus:

int function(int x)
{
    body of function
}

Heretic people all over the world have claimed that this inconsistency
is ...  well ...  inconsistent, but all right-thinking people know that
(a) K&R are _right_ and (b) K&R are right.  Besides, functions are
special anyway (you can't nest them in C).

Playful(1).

It's arguably good advice to try to keep indenting from getting out of control, though a three level maximum might be hyperbolic. I'm not going to grep the kernel source and count sequences of four tab characters, but I'd bet money you could find at least one that Torvalds wrote.

On the other hand, if somebody can write the Linux kernel without often exceeding three levels of indenting, a three-level limit might be an exercise worth trying out for a while in your own code, just to see where it takes you. This isn't like a sex change, you know. It's not a lifetime commitment.

If you run into somebody on the Internet who thinks he understands programming much better than Torvalds(2), well, you know what kind of people like to talk big on the Internet.

On the other hand, he is criminally wrong about eight-space tabs. That is the raving of a man who should be kept in restraints and fed through a slot. Four spaces is obviously correct.

(1) But note how he erroneously puts a space before the ellipses, and two spaces after them, and two spaces after a full stop. WRONG, WRONG, WRONG. And then he has the brazen gall to castigate heretics. The heretic is you, Torvalds! IT IS YOU!

(2) If you want to talk about "understanding how to design a source control system", there might be some room for debate.

Note: Dear fellow user who has repeatedly submitted the same edit: The formatting in the quoted material is kept exactly as the author meant for it to be. That's because it's from an essay about the formatting of fixed-width text, written in fixed width text, by somebody who has given the formatting of fixed width text a fair amount of thought. The formatting is a conscious and deliberate part of the author's intent, and it's relevant to the subject.

In addition, I referred back to that formatting in my own text. If you take out the pre-formatting, my footnote (1) becomes gibberish. If the pre-formatting is removed, so should be the text in my footnote (1) referring to the pairs of spaces after the full stops at the ends of sentences. I can see a rationale for removing that footnote anyway, on account of it being less funny than it seemed when I wrote it. But to remove the formatting without removing the footnote is unhelpful.

Ed Plunkett
  • 609
  • 4
  • 16
  • 3
    Wonderfull answer. One of the cases that would deserve a +2... (Note: No wrong spaces around `.` in this comment ;-) ) – cmaster - reinstate monica Dec 23 '15 at 20:38
  • 2
    Linus's intro paragraph that you pointed out is very important so thank you for doing that! I think the first sentence is also very important for context, specifically `preferred coding style` as well as `but this is what goes for anything that I have to be able to maintain` – Chris Haas Dec 23 '15 at 20:56
9

Linus has a very blunt speaking style, and a dry sense of humor, but he was not joking in this instance. There are situations where an algorithm needs nesting deeper than two levels, but you can accomplish this using other means than indenting your code. The Linux kernel style guide strongly prefers these other methods, because of the difficulty of maintaining deeply nested loops, and that is what Linus is saying here.

For some examples of alternative methods, you can use recursion, split off inner loops into their own functions, or make intermediate data structures.

Excessive nesting is one of those cases that's easier to write, but harder to read. Setting a large tab depth is Linus' way of making it more annoying to write too.

Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479
3

There are many questions where the advice is different for someone asking the question than for someone who doesn't ask. If you ask "Should I ever have loops that are nested more than two levels deep" then for you, the person asking that question, the answer is NO. If you ask, then don't do it. If you have enough experience that you don't need to ask, then you know what's the correct answer in each case. And don't argue if you don't agree with the answer, because the answer is not for you.

gnasher729
  • 42,090
  • 4
  • 59
  • 119
1

This would appear to be a textbook case of the tail wagging the dog.

If you have an 80 character display then of course you are going to try and make the code fit as best you can even if it doesn't produce the best structure for the code.

Tackling the remainder of your points head on:

I figured it was an unacceptable practice.

I think you're reading too much into this. Resist the urge to take everything you read as gospel without properly understanding the context.

Was he joking?

Hard to ascertain the context, but see my original point above.

Does it depend on the language or application?

Very much so. Take any mainframe/midrange language where you're likely to be coding on a terminal (or terminal emulator).

Are there some things which absolutely need three or more levels of looping?

Yes, it is very common in some brute force algorithms. See Problem 31 on Project Euler. This is a classic example of a problem that could be solved with brute force using a number of loops (8 to be exact).

Robbie Dee
  • 9,717
  • 2
  • 23
  • 53
  • 1
    Looks like the Problem 31 doesn't require bruteforce and could be solved using a dynamic programming algorithm (edit: which means your code structure isn't the best if you're using a bruteforce algorithm). Also, Linus's point is that if your code requires many levels of indentation, it's likely not the best structure for the code. – Vincent Savard Dec 23 '15 at 14:00
  • 2
    @VincentSavard Never said it *required* brute force. Disagree with your 2nd point - sometimes it is the clearest and most succinct approach, not to mention the most efficient in some cases. – Robbie Dee Dec 23 '15 at 14:04
  • 1
    With that kind of problem I usually don't indent the loops. I think I had one case with 20 nested loops, absolutely trivial to write, and no indentation so you could see the loops were almost identical. – gnasher729 Dec 23 '15 at 14:14
  • 1
    @RobbieDee: My point is that your example of a problem solved by many loops is that your algorithm isn't as efficient as a dynamic programming solution, which doesn't require as many levels of indentation. Thus, as Linus said, your levels of indentation can be removed by using a better solution. You also misunderstood my second point because I agree with what you said. _Sometimes_, it's the best solution. Sometimes isn't often, and isn't likely. – Vincent Savard Dec 23 '15 at 14:41
  • 1
    The Linus quote pretty much explicitly says that if some code requires something like bruteforcing that Problem-31, then you're screwed anyway - it will not be fast nor simple, and kernel operations *must* be fast and simple. Including any O(n^4) algorithm in kernel is a significant risk of performance or denial of service problems, so in this context the recommendation simply warns that this is a sign of code that may be fundamentally inappropriate and wanted in Linux. – Peteris Dec 23 '15 at 15:24
  • 1
    Yes, the OP has since edited the question and stated that this is *specific* to kernel code. Just emphasises what I said about context in my first point. – Robbie Dee Dec 23 '15 at 15:27
0

Was Linus joking?

No, those are the official guidelines.

Does it depend on the language or application?

Coding guidelines are generally dependent on the language and application, however deeply nested code always taxes the reader.

The issue with nested code is that in general it increases cyclomatic complexity: that is, the more nested the code is, the more potential execution paths exist within the function. A combinatorial explosion of potential execution paths makes it difficult to reason about the code, and therefore should be avoided in general.

So why 3? A subjective coding guideline is both hard to enforce and impossible to enforce automatically. Setting up an objective coding guideline on the maximum level of indentation requires agreeing on a number: in the Linux kernel they picked 3.

It's arbitrary, and apparently sufficient for them.

Are there some things which absolutely need three or more levels of looping?

Algorithm-wise, probably, however in sufficiently expressive languages you can always refactor the code into smaller chunks (whether with functions or closures).

You can obviously write obfuscated code with little nesting and lots of small functions calling each other without ever spelling their contract...

... however, small functions with clear contracts are much easier to audit than large functions with clear contracts in general.

Matthieu M.
  • 14,567
  • 4
  • 44
  • 65
  • 2
    While this might be the official guideline, it's trivial to find places in the kernel code where the guideline isn't enforced. – MikeB Dec 23 '15 at 18:00
  • 1
    @MikeB: All the more reasons to enforce guidelines automatically... – Matthieu M. Dec 23 '15 at 18:03
  • 1
    @MatthieuM. Are you sure you understand the difference between guidelines and mandatory requirements? As a general "rule of thumb" (a guideline if you like), guidelines are more like recommendations and aren't enforced. – Brendan Dec 28 '15 at 10:17