10 ms·
"Indent 2 spaces. No tabs." Well fuck it. Thats the only coding style that remotely makes sense to me: https://www.kernel.org/doc/Documentation/CodingStyle htt
by zobzu 13y ago
"Indent 2 spaces. No tabs."
Well fuck it.
Thats the only coding style that remotely makes sense to me:
https://www.kernel.org/doc/Documentation/CodingStyle https://www.kernel.org/doc/Documentation/CodingStyle
While I'm at it, half the justifications Google gives for the shell guide are inaccurate. Looks like a "overall people are used to this style so we're using it and we'll try to justify it without knowing why"
This is a much better guide:
http://devmanual.gentoo.org/tools-reference/bash/ http://devmanual.gentoo.org/tools-reference/bash/
Example: Gentoo's [[ ]] explanation actually makes sense.
- nsmartt 13y agoI've always been conflicted on this. It's simply untrue that more than 3 levels of indentation indicates bad code. For example, HTML, XML, and JSON often benefit from deeper levels of indentation. Tabs have the benefit of having a dynamic size, so I could simply adjust the tab width within my editor. Spaces also do not play as well with editors. Unfortunately, browsers offer no means to change the width of tabs, so it's impossible to properly display code using tabs. This is especially frustrating when reading the source of a page indented using tabs. It would be much easier to read with spaces, but not as easy to read as if I were able to set tab width.
- GuiA 13y agoHTML, XML, and JSON are document description formats. For most programming languages, more than 3 levels of indentation is a red flag.
- nsmartt 13y agoWhile it's true of some languages, it's certainly not true of all of them. I'm not sure most is accurate either. JSON is a subset of JavaScript, so it stands to reason that JavaScript could have as many indentation levels as JSON without a loss of clarity. It also stands to reason that any language with hashes, dictionaries, etc could feasibly exceed 3 indentation levels without it being a red flag, particularly if those hashes are in a method that is already indented more than one or two levels.
- fhd2 13y agoWell, Torvalds said that about C where you have no indent at the class/namespace level. So I'd argue it can be a bit more for languages that have this (most of the popular ones), 4-5 I guess.
- sesqu 13y agoSo for those three levels, do I have to count the object and method-level indents, or is it a rule for languages with unindented includes?
- edoloughlin 13y agoIt's a pity that elastic tabstops never got any traction. it's a really nice idea: http://nickgravgaard.com/elastictabstops/ http://nickgravgaard.com/elastictabstops/
- Munksgaard 13y agoThis looks horrible if I want the opening brace on the same line as the closing parens around function parameters, but still want the function parameters to be aligned, like this: int someCodeDemo ( [tab] int fred, [tab] int wilma) { [tab] return fred + wilma; } I guess it needs some vertical space between blocks in order to distinguish them, hence why putting { on the next line works. In my opinion, it also looks horrible with Lisp code (where I pretty much consider indentation a solved problem). Edit: Oh god, how do I format this... Ah, there we go.
- jbrooksuk 13y agoThere is a Sublime Text plugin compatible with ST2 and ST3 which does this. https://sublime.wbond.net/packages/ElasticTabstops https://sublime.wbond.net/packages/ElasticTabstops The only problem I've noticed is that it muddies the undo stack.
- dscrd 13y agoReal men and women indent by prime numbers.
- robin_reala 13y agoSure they do, add this to your user styles: pre { tab-size: 2; } https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size
- nsmartt 13y agoThanks, I wasn't aware of this. Hopefully we'll begin to reach a point where tabs are used and sites like GitHub adjust tab-width according to the developer's preference.
- olau 13y agoOn tabs vs. spaces, read the old classic: http://www.jwz.org/doc/tabs-vs-spaces.html http://www.jwz.org/doc/tabs-vs-spaces.html tldr; don't use tabs. I read that piece many years ago and thought who cares, but I've later changed my editor to never use tabs. He's right. Tabs are nice in theory, but in practice you end up with garbled code. It's better to train yourself to get used to seeing code bases formatted slightly differently, just one of the things you have to get over, IMHO. Like camel notation version underscores versus dashes.
- fhd2 13y ago> It's better to train yourself to get used to seeing code bases formatted slightly differently, just one of the things you have to get over, IMHO. Like camel notation version underscores versus dashes. That's a pretty good point. Indentation is indeed just one small aspect of the overall code style - why would you want to have that configurable and nothing else?
- Shorel 13y agoThanks for posting the original s-expr based, tab bashing article! It has always been my own argument in favour of tabs: - The original argument against tabs was based on Lisp indentation rules. - Languages with very complex indentation rules like Lisp should use spaces. There's no exception to this rule. - Languages from the C family should use Tabs, as Lisp indentation rules don't apply to them. - Nowadays almost all popular languages can be considered to be part of the C syntax family. Python and Lua are notable exceptions. PHP and Objective C files should use tabs.
- lispm 13y agoNever let get facts in your way. http://trac.common-lisp.net/mit-cadr/browser/trunk/lisp/zwei/indent.lisp http://trac.common-lisp.net/mit-cadr/browser/trunk/lisp/zwei... MIT's Zmacs, the editor of the Lisp Machine indents with tabs.
- Shorel 13y agoGood find, thanks. However, one very old editor using tabs doesn't mean Lisp programmers actually prefer them. It may even be the cause of tabs hate.
- amaks 13y agoIt really doesn't matter how many spaces or tabs, what's important is that all code and scripts are written in the same style, which greatly reduces time required to understand someone else's code. At Google, that's a norm.
- apr 13y agoIt actually does, 2 spaces indents are hard to read, period.
- gryz 13y agoyou're getting used to it within few months.. (currently we have tabs though)
- sdoering 13y agoIs there any scientific/empirical proof on this? I indent 4 spaces, no tabs, but I would like to see the real rationale there, not just the PEP-8 "rules".
- Shish2k 13y agoZero spaces makes indentation harder to spot than infinity spaces. Infinity spaces gives less space for code than zero. Conclusion: use as wide an indentation as possible while still keeping enough space for the code itself.
- raverbashing 13y ago+1 for the kernel coding style I can understand (grudgingly) 4 spaces for Python But for C/C++ 4 spaces are awful. Maybe because C lines end up being longer? Tabs, they exist. Use them
- sdoering 13y agoMy editor automatically converts [TAB] to four spaces. Works just fine for me.
- raverbashing 13y agoAnd? Every editor can do it, I'm not complaining about pressing the space bar 4 times. I'm complaining about the appearance of code indented with 4 spaces.
- redwall_hp 13y agoWell, it shouldn't be a problem if you keep your source files clean and properly use tabs for indentation. Then everyone can set their editor to render tabs at their desired width, whether it's 2, 4, or 8 spaces. It's ludicrous that these space-wielding heretics (to borrow from the kernel style guide) are keeping everyone from having that.
- klibertp 13y agoEvery editor, or at least every good editor for programmers, can convert 4 (or whatever) spaces to tabs. If it bothers you that much, you can set up your editor so that it tabifies files when opening them and untabifies when saving. This way you won't even know if the file used spaces or tabs - and that's how it should be. In short: configure your editor to display to your tastes and save to whatever style guide you use in your organization. Problem solved.
- sesqu 13y agoYou might be shocked by how many editors can't convert indentation spaces to tabs. You might also be surprised by how many source files with indentation spaces have off-by-one indentation errors.
- chetanahuja 13y agoHah... if you think 2 space indents are bad in shell (and C++), you should try writing python with it (yes, google coding style, in a fit of madness, insists that engineers try to keep python blocking straight with 2 space indents. Remember, some of these hapless folks are maintaining large bodies of production python code (as opposed to itty bitty automation scripts).
- tomlu 13y agoGoogle mandates 4 space indentation according to this: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Indentation http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...
- chetanahuja 13y agoOh.. hmmm.. so this has changed since the (first and) last time I tried to get a piece of python code through a review at Google. Looks like better sense has prevailed finally. (There's a version number on that public document but as far as I see, no version history so I could tell when the change happened.)
- jamesgeck0 13y agoWayback Machine shows the document recommending 4 spaces when it was first released in 2009.
- chetanahuja 13y agoLooks like the google's external style guide is saner than the internal one :-) I was talking about the internal style. I can't even begin the fathom the reasoning for the internal style.
- estebank 13y agoI think 4 spaces is for public code and 2 spaces for internal code. FWIW, I got used to 2 spaces quickly enough, and now it doesn't bother me using either 2 or 4, other than sometimes forgetting to change the setting on vim when changing between projects.
- tmallen 13y agoThe Gentoo documentation is a programming guide and doesn't advise on style. The Google style guide doesn't explain [[ because it is out of scope.
- anuragramdasan 13y agoTotally. I love the kernel style over anything else. Even the GNU style for C isn't something I am very fond of. Still there are some languages where 4 spaces indent is fine. But two is just horrible. Also I like tabs. Convert tabs to spaces in the editor and it works out pretty fine. 2 spaces is just too cluttery.
- th3byrdm4n 13y agoGentoo is extremely thought out and over engineered. They seem to do most everything right. . . It's just so much work though. Note also on the [[ ]] usage... you'll see that [[ ]] won't work for boolean algebra ie. '||' and '&&'
- Shish2k 13y agoWhy is this still a thing that people have to care about? Can IDEs still not automatically convert between company-style and personal-style? (Sad that tabs solved that problem decades ago (by having a byte that means "+1 indent" instead of having to build some ascii-art that looks like an indent) but people screwed up the implementations so badly :( )
- simias 13y agoProper indentation (tabs for indentation, spaces for alignment) is still pretty hard to configure in most editors in my experience. Even in emacs I had to install an external package to get it to behave properly (and it still breaks from time to time). Failing that I'd rather people just used spaces and no tabs. Using tabs for alignment just means it's going to look fucked up everywhere else. At least spaces are consistent.
- dredmorbius 13y agoThere's a lot of fiddly bits. And my IDE is called "vim". Sometimes it's called "ed".
- TeMPOraL 13y agoIt's fun how the "tabs vs. spaces" war is relevant only where it doesn't really mean anything. See, most of the languages (I've seen so far) have a well-defined rule of identation. The thing you get when you do "select all, ident file" in your IDE / editor. At this point it doesn't really matter whether you use spaces, or 2-space tabs, or 4-space tabs, or mix, or whatever - there's only one way non-whitespace characters can be positioned, so it will (in theory) look the same on every editor after you reindent it. The only place where the choice of tabs vs. spaces actually matters is when you want, for some reason, to break the default rules of identation for your language and position something manually. But in this case, there's no debate; tabs are not suited for precise, manual positioning. Only spaces will be guaranteed to make the code look the same everywhere.
- wattengard 13y agoUmm... No... Yes, the IDE does this, but indent-length and tabs/spaces is mostly ALWAYS definable within the IDE's settings, and is more related to the IDE than the language you are currently editing.
- valtron 13y agoI am extremely annoyed when I try to click at the beginning of a line and I clicked a bit too far left and the cursor gets placed _before_ a space because spaces are being used instead of tabs.
- delinka 13y agoThread's gotten too deep and wide to reply to everyone, so I'd like to vent right here at the top... We have these machines that are meant to take the tedium out of our lives, let's USE THEM. Why do we not have tools that format things they way I like them? Who cares how the other team members like it? They can use the tool, too. Who cares how it's stored in your VCS? The tool should just be part of the workflow and format spaces/tabs the way I want them and maybe revert to a canonical format for committing to the repo. Personally, I prefer tabs. I read code best when it's indented four spaces. But John wants to use two spaces in this code. And Ralph likes three. Holy Hell, don't make me wade through someone else's preferences to understand this codebase! Use tabs and people can set them the way they want. Yes, tabs are broken in many places (e.g. Objective-C methods can get so verbose that they need to wrap; convention is to wrap and align the colons; tabbing as far as possible and then spacing to fill breaks things when Ralph formats his tabs to three spaces; in this case, there is indeed a solution: tab to the start of the original line of code, and space to format from there ... but then the IDE has ideas of its own about what should be spaces and what should be tabs...) Maybe you can read that code with only two spaces of indent, but I can't. I'm gonna need that reformatted to put a larger visual separation between scope changes or whatever required the indention. Rather than have this debate, a tool should be forged.
- gbog 13y agoDisagree. Readability is not about 4 or 2 spaces, it is about strongly enforced convention that makes these typographic details literally disappear from the eyes of the readers. This effect is very noticeable in book publishing: if you ever notice the typography, the typographer failed at his task. It is the same in code, just file the rules and it will become your preferred guideline in no time. And repeated experiences everywhere point to the fact that code text is already very hard to manage, very sensitive to alterations, misreading, etc. I'd rather remove layers and tools than add more. Edit: obviously, readability is ask about convention when they are within the realm of sane acceptable conventions. And by everywhere I mean in teams where the loc count is above a few thousands.
- mden 13y agoDisagree with your disagree. There is a difference between the actual code and how the code is styled. It makes sense to have conventions on how to format a for statement but not where to add brackets - making them be on the same line or on their own line makes little difference to noticing bugs and is purely a stylistic preference.
- consultant23522 13y ago"The majority of people prefer it this way" seems like the perfect justification for a coding standard. Consistency is way more important to me than perfectly matching up with some specific ideal.
- e40 13y agoThat reference is the best I've seen. Thanks for that.
- untog 13y agoThe sheer number of replies to this point is staggering, for what really ought to be a total non-issue in this world. Someone needs to make some kind of GitHub integration that lets you download code using whatever esoteric formatting you prefer, then transform back to some given standard on commit. Then everyone can finally just agree to disagree and get on with life.