4 ms·
I'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 benefi
by nsmartt 13y ago
I'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.