4 ms·
A useful time for a quote from the Linux Kernel coding standards [1] - exactly the first item in fact: "Tabs are 8 characters, and thus indentations are also 8
by jen20 5y ago
A useful time for a quote from the Linux Kernel coding standards [1] - exactly the first item in fact:
"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.
In short, 8-char indents make things easier to read, and have the added benefit of warning you when you’re nesting your functions too deep. Heed that warning."
[1]: https://www.kernel.org/doc/html/v4.10/process/coding-style.html#indentation https://www.kernel.org/doc/html/v4.10/process/coding-style.h...
- omegalulw 5y ago4 character indents are already too big. 8 character indented code is vomit inducing. 2 character indents and any modern monospace font is enough for readability. I concede that reading 2 indented is hard when you begin braces on a new line - but then your problem is beginning braces on a new line, not the indent.
- SmooL 5y agoI'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.
- Waterluvian 5y agoI have a hard time believing these opinions are correct because they were heavily constrained based on column size and monitors of the old days. So it’s not like the community freely came to this conclusion after a lot of informed experimentation.
- kazinator 5y agoJust because you have an ultra-wide, 8K monitor in 2021 doesn't mean it's a good idea to write 400 character lines. This argument is specious. In fact 80 columns is as much related to readability as it is to hardware limitations of yore. 100-something columns for code can be a pretty big relaxation over 80, but beyond that it gets into diminishing returns.
- Waterluvian 5y agoI generally agree. I once played with 130 and found that to be crazy. Settled on 100 (when it's my own project). My point is that it's not like they came up with these opinions based on a sandbox of experimentation. The opinions on line length, indentation, etc. are all results of constraints at the time. Which makes the opinions on things like "80 chars, 8 char tabs" feel like retcons.
- deleted 5y ago[deleted]
- sk0g 5y agoThat's all well and good for Linux-style C programming maybe, but it would be painful in languages like C#. EG the simplest function in a Unity game, that prints "hello world" for the player only would be something like: namespace Something { class SomeClass { void Awake() { if (CompareTag("Player")) { print("Hello, world") print("This line's closing bracket is at # 80") } } } }
- catmanjan 5y agoThe new C# 6 templates don't require namespace or class blocks - for this reason I suppose.
- sk0g 5y agoI do like having the namespace there, if only to gently nudge you towards better architecture. As a temp fix I have overridden Rider's code formatting to not indent within the namespace, but C# 10 will bring file scoped namespaces. You can just declare the namespace between the imports and the classes, and everything in the file will be namespaced automatically.
- fingerlocks 5y agoPretty much every language with a class that contains a function with a loop that breaks on an inner condition would be 4 levels deep. I suppose Objective-C would only be 3 deep, but I can't think of any others off the top of head.
- hyperjeff 5y agoThis presumes one isn't using real tab characters in their code. (No idea why they claim tabs are 8 spaces, as they're their own character.) The rest of these arguments are just curious decisions by a certain community. The point is for the visual indentation to be up to the end user, seasoned to taste. Requiring some code viewer to stick to n spaces is unfriendly. (I'd sooner quit programming altogether than follow the above rules.)
- rurban 5y agoThat's certainly true for spaghetti kernel or firmware code, but certainly not true for application code, with a much higher abstraction. 8 char indentation also doesn't make your code easier to read, on the contrary. Your eyes need to move much further.