4 ms·
Indentation war is ON again ;-) TL;DR - Linux kernel source code use TABs (8 characters) instead of spaces. The rationale behind is that the maintainers believ
by terrywang 10y ago
Indentation war is ON again ;-)
TL;DR - Linux kernel source code use TABs (8 characters) instead of spaces. The rationale behind is that the maintainers believe that large indentation makes code easier to read on screen (especially for long hours), makes sense.
Personally I (not a programmer but Linux SysAdmin/Ops/Infra Architect background) tend to use 4 spaces everywhere else (e.g. Shell, Ruby, Java and all sorts of configuration files). Not to pick a fight (sounds familiar? ;-) but 2 spaces in general make readability worse.
Anyway, the most important point is to honour what is already established/in place and stick to it, whatever you work on.
- tiglionabbit 10y agoPeople should just use actual tabs. Then you can make the indentation display however you want in your editor. And you never have to worry about deleting part of an indent level, creating weird slightly offset indentations. I know, everyone is going to downvote me for saying this. But if you're literally using 8 spaces to represent a tab, which is the default width tabs are rendered at, then what downside could there be to switching to actual tabs? Edit: "spaces are never used for indentation" so do they actually use tab characters then?
- aanm1988 10y ago> In all cases, prefer spaces to tabs in source files. People have different preferred indentation levels, and different styles of indentation that they like; this is fine. What isn’t fine is that different editors/viewers expand tabs out to different tab stops. This can cause your code to look completely unreadable, and it is not worth dealing with. from the LLVM style guide.
- tiglionabbit 10y agoWhat's an example of some source code that becomes unreadable when you change the size of tabs? Are you editing code in Microsoft Word or something? Setting custom tabstops and shooting yourself in the foot? Don't mix tabs and spaces in indentation btw. That is the one thing you must never do.
- stouset 10y agoTabular alignment is practically the one thing of all these holy wars that actually improves readability. So no, that's not something funny — it's something that trivially and immediately improves readability of your source. foo = 1; this_value_is_not_foo = 8; another_int = 419; pi = 314159; versus: foo = 1; this_value_is_not_foo = 8; another_int = 419; pi = 314159;
- tiglionabbit 10y agoIt's fine to use spaces for your tabular alignment. Go ahead. But whether you have tabs or spaces as the indentation portion won't make any difference to the resulting alignment. The important thing is that you don't mix tabs and spaces in either portion. So, in my proposal, the indentation portion would be all tabs, and the tabular alignment part would be all spaces.
- stouset 10y agoThis works in theory. I've yet to see a project accomplish it in practice. There's always someone with an incorrect editor config that does indentation and alignment with tabs (because it's easier to configure your editor to just send a <TAB> than to have smart behavior, or copy-pasted code contains a tab rather than spaces, or a million other reasons), and nobody notices until two years later and everything is a mess for anyone trying to use a custom tab-depth.
- deleted 10y ago[deleted]
- mcbits 10y agoThis gives the false impression that the variables are all related. If they are related, they should probably be encapsulated in some kind of structure. And once you add the comments explaining what the first three variables are and how they should be used, and the comment explaining why pi doesn't have a decimal point, there is no benefit to alignment. If you add another long line, you have to realign the whole list (or leave it looking sloppy). Overall, this kind of alignment is a bad idea.
- paulryanrogers 10y agoWhen there are [line] width guidelines then the difference between 2 and 8 adds up
- tiglionabbit 10y agoI hate line length guidelines. People should just enable word wrap. I use Sublime Text and it wraps lines in a really readable way, maintaining the indentation from the start of the line.
- brudgers 10y agoProbably tabs because that's what makefiles require. That and having a standard allows people to speak a common language.
- aanm1988 10y agoAlternative viewpoint from a popular style guide, google uses 2 spaces for C++.
- terrywang 10y agoGoogle's Shell Style Guide says: Indent 2 spaces. No tabs. Use blank lines between blocks to improve readability. Indentation is two spaces. Whatever you do, don't use tabs. For existing files, stay faithful to the existing indentation. Makes sense. I don't mind stick to established style, personally I'll still prefer 4 instead of 2 for readability (easy on eyes...).
- deleted 10y ago[deleted]
- voycey 10y agoC Programming vs most other languages is a different animal for readability and formatting, I think you hit the nail on the head when you said "Honour what is already established" On that subject, I really dislike some of the PHP PSR formatting guidelines, it is a C style language and they have implemented some really painful paradigms
- chj 10y ago> I really dislike some of the PHP PSR formatting guidelines What are they? I have found that PHP guidelines are surprisingly good.
- vizeroth 10y agoPSR-2: http://www.php-fig.org/psr/psr-2/ http://www.php-fig.org/psr/psr-2/ Of course, these have little to do with core PHP, but PSR-1, PSR-0/4, and large portions of PSR-2 have been widely adopted within the PHP development community. The most-debated portion of PSR-2 is, of course, the use of 4 spaces for indentation (no tabs). PSR-12 aims to bring the guidelines up to date with the latest features in PHP 7, but I've mostly lost track of things in PHP-land since moving to a new job and burying myself in XSL (and occasionally C#).
- voycey 10y agoBrackets on new lines specifically get my goat, I think it's unnecessary most of the time, there are a few other things that piss me off as well but I guess it's nice to have a standard and I am just one dude who is used to doing it his way :) I have code tidying stuff that happens on commit so I still just do it my way and then let automation handle the pedantry!