5 ms·
Programming Styles in the Node Community
- mise 15y agoIndenting by two spaces is new to me. As a PHP programmer, an inserted tab (not converted to spaces) is the length of four spaces. Is the two-space indentation relatively new? Or has it always been seen around in the JS community?
- dspillett 15y agoI've always been a four-spaces man, even in Javascript, and that seems quite common. Javascript uses in an event driven environment (like a browser of node.js) can encourage some pretty deep nesting so perhaps people use smaller indentation granularity for that reason.
- themanr 15y agoTwo spaces are the norm for ruby development but it seems fairly new in javascript.
- obayesshelton 15y agoNot in all cases, some IDE's or text editors have more space for a tab.
- tomjen3 15y agoThat is common in the Common Lisp and Scheme communities. So it is actually really old.
- richthegeek 15y agoDrupal insists on 2 spaces.. but that really doesn't give enough indication of depth to me so I use either 4 spaces or a tab character.
- insraq 15y agoWhen I program in PHP, I always use four spaces (converted by a tab), which is suggested by PEAR coding standard. Python mandates indentation. Four spaces is highly recommended. One tab is accepted but discouraged.
- lalc 15y agoWas hoping for something less bikeshed-y. What about, say, adherence to all-streaming APIs? What granularity of stream fragment? At what complexity do they switch from callbacks to EventEmitters? Keeping view partials ordered?
- nestlequ1k 15y agoEasy solution. Just use coffeescript. Nice and clean, and you'll never have to see anyone, anywhere put comma's before lines. Win win :)
- gcp 15y agoThe canonical indentation for CoffeeScript is 2 spaces. In a language that relies on indentation to define blocks...that's...insane. I can attest firsthand this causes bugs. 2 spaces just isn't enough to reliably visually identify the horizontal separation. Python got this right with PEP 8. CoffeeScript did not and is worse for it.
- tomjen3 15y agoThat is also the convention from Common Lisp, Scheme and (I believe) Ruby. As for it causing bugs, bugs are like cancer -- they are caused by everything.
- gcp 15y agoUh, Common Lisp and Scheme use parentheses for block delineation. How does someone on HN not know this? :-)
- nene 15y agoI've been switching between 2 and 4 spaces through my whole programming career. Even now I'm switching daily between different languages and projects with different amount of indentation. And I for one have noticed no correlation between bugs and amount of indentation. But I might be wrong - I have done no actual measurements. What I can say though, is that there is a correlation between a lousily indented code and bugs. Consistency is the key in indentation. Reading non-consistently indented code, that's what drives you insane... but two instead of four spaces... hardly.
- gcp 15y agoThis isn't about 2 spaces vs 4 spaces. It's about 2 spaces in a language where blocks are defined only by indentation. That's a HUGE difference. I have no problem with 2 spaces in C/C++. I have no problems with Pythons use of white-space to define blocks. But the combination is deadly.
- mathrawka 15y agoI was hoping for a more in depth article, not something that squabbles over commas, semi-colons and indent space counts. I wanted to see more about how programs/modules are organized Yes, they mention how TJ puts an index.js so he can require everything in one line, but that was just a footnote it felt like.
- Plog 15y agoYou can find here (http://nodeguide.com/style.html http://nodeguide.com/style.html) another style guide written by Felix Geisendörfer, an early node.js core contributor. Personally, I think that the more important is that your all team has the same coding style and to be capable to adapt to another coding style when necessary and when you change of language. Generally it's better when your coding style looks like the one of the language creator.
- jroseattle 15y agoI noticed all the comments around the code are setup for YUIDoc. Is YUIDoc the standard in JS nowadays? I'm not familiar with alternatives otherwise.
- dionidium 15y agoI am torn between following the coding conventions that prevail in a particular community (a generally reasonable thing to do) and (as a long-time JavaScript user) disliking nearly every new JS convention the node developers have settled on.
- tomjen3 15y agoNo offense but that isn't their fault -- it is simply because Javascript is a bad language. No convention can solve things like var hoisting, this changing meaning in callbacks, etc, etc. So people come up with some really strange coding styles to try to route around them -- which good programmers will hate, even if they are not entirely sure why.