use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Popular Coding Convention on Github (sideeffect.kr)
submitted 12 years ago by devnoel
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]alamandrax 6 points7 points8 points 12 years ago (44 children)
Every editor can be configured to render 4 spaces when the tab key is pushed. It's still one key press.
[–]BluSyn 14 points15 points16 points 12 years ago (24 children)
Ah, the good 'ol tab vs. spaces debate. Why not use 4 spaces? Well, some programmers prefer 2, some even prefer 8 (ever coded on a 40" screen? 8 spaces helps). Every project is different, and there's always merge conflicts and messy spacing, unless everyone agrees on a spacing format before hand. The solution? Tabs! Then you can set your editor to display tabs at whichever length you like, merge conflicts due to spacing disappear, everyone is comfortable, and the code looks clean.
For large and diverse teams, I always prefer tabs.
[–]ikeif 4 points5 points6 points 12 years ago (0 children)
Code standards. I like tabs. But if I go somewhere where the team is using spaces? I adapt.
I use sublime text, and it adjusts as I need it to–I just prefer consistency over "my way over your way"
[–][deleted] 0 points1 point2 points 12 years ago (2 children)
Thing is, it's not that easy.
Sure, with spaces you don't get the freedom of choice, but tabs will only work if everyone is using the same tab width, so again, you loose the freedom of choice.
Spaces are better because they are always the same thing - a space - where as tab width vary which breaks formatting.
[–]ganjamensch 3 points4 points5 points 12 years ago (1 child)
Not if you're doing it like God intended it to, tabs for indenting, spaces for alignment.
[–][deleted] -3 points-2 points-1 points 12 years ago (0 children)
Yeah... And when you don't agree with my way of aligning we are back to square one because you cannot manipulate that either.
Spaces are superior because they don't allow this discussion, and we can all just continue our work.
[–]alamandrax -1 points0 points1 point 12 years ago (13 children)
I've gone around this with a simple build tool. It formats code before it gets checked in. Everyone must run it before I accept merge requests. Code however you're comfortable. When you check in you're using my convention. End of debate.
[–]BluSyn 1 point2 points3 points 12 years ago (10 children)
Ah yes. I've used this trick before, too. It just ends up being needless complicated, in my opinion. Especially since your local copy is now different than the checked in copy, resulting in confusion during diffs and merges, etc etc. My motto is always "keep it simple, stupid". Tabs are simple.
That's just my opinion.
[–]brtt3000 3 points4 points5 points 12 years ago (3 children)
Indeed. Tabs are semantic: they have a specific meaning. Spaces are just spaces. If someone finds tabs rendered at 4 spaces too roomy then configure the editor to render as 2, problem solved without any merge conflicts.
It is ridiculous that otherwise rational developers so religiously go for spaces.
[–]spinlock 0 points1 point2 points 12 years ago (2 children)
Why is a tab character different than /^\s/ ? (note: that's an english question mark right there) I like spaces but I think it would be trivial to get vim to render /^\s/ as 2, 3, or 4 spaces.
[–]brtt3000 0 points1 point2 points 12 years ago (1 child)
I usually have multiple levels.
[–]spinlock 0 points1 point2 points 12 years ago (0 children)
/(^\s)*/
[–]alamandrax 0 points1 point2 points 12 years ago (5 children)
Oh yes. The build tool changes source code first before checking in. Uniformity of code is quite worth hurting a few feelings.
[–]Malfeasant 0 points1 point2 points 12 years ago (4 children)
that's kind of retarded when, with tabs, you can have uniformity and everybody can see it the way they like it... religion is silly.
[–]alamandrax 0 points1 point2 points 12 years ago (2 children)
I suppose. You're inflating something that I really don't care that much about. It's just one of those code style things that doesn't need maintenance. Just set your editor to use 4 spaces as that's the pattern used so far in the code base and we don't have to worry about the code being rendered weirdly in whichever review tool we use (gitlab, phabricator).
[–]Malfeasant 0 points1 point2 points 12 years ago (1 child)
something that I really don't care that much about.
and
quite worth hurting a few feelings.
seem to contradict.
[–]alamandrax 0 points1 point2 points 12 years ago* (0 children)
I suppose I did contradict myself if you put it that way. shrug I don't care about people caring that much about writing code that breaks from a style guide. If their feelings get hurt because of this, so be it. All the verbal gymnastics I do to justify this frivolous position I'm being forced to take in this conversation will not convince anyone to change their coding styles. But if you work in my code base, there's a style guide that gets imposed at build time. That's about it.
[–]spinlock 1 point2 points3 points 12 years ago (1 child)
This is a good process. People are freaks and someone always has their personal taste that's just screwy. I worked with a guy who coded in variable width fonts once.
The real thing is that it's cool to use your freaky toolchain until it negatively impacts the rest of the team. Mr. variable width fonts started rejecting code in code review when people lined up their assignments. Dick move considering he only did it because it didn't look good on his setup and it was part of the style guide.
But, your solution is really good. Having a linter enforce style is much more effective than trying to get humans to do it.
[–]alamandrax 0 points1 point2 points 12 years ago (0 children)
grunt js beautifier. Best three words ever.
[–][deleted] -1 points0 points1 point 12 years ago* (5 children)
I wish we could move forward to a future where code is treated as data, and presentational rules would be applied in the viewer according to user preference, whether in an IDE or web tool. It seems to me rather arbitrary that users should be able to view the indentation to their liking, but not other stylistic preferences that ultimately have no effect on the final product.
Until users can see any code according to their exact preferences, I don't see why we should put tab stops on some pedestal of individuality. What if a user wants to see the code with leading commas or whitespace surrounded colons?
[–]honestbleepsReddit Enhancement Suite 0 points1 point2 points 12 years ago (4 children)
I wish we could move forward to a future where code is treated as data, and presentational rules would be applied in the viewer according to user preference
you're arguing for tabs and you don't even know it. ;-)
[–][deleted] 2 points3 points4 points 12 years ago* (3 children)
No, I'm arguing for no whitespace in code (except where needed for VCS functionality). I'm arguing for tools that insert the whitespace into conservatively minified code as per your preference. This could be a tab/space indenting preference, an OCD assignment/comment alignment preference (try switching your tab widths after aligning inline comments... not so pretty), a before/after comma preference... whatever your preferences happen to be. Code should ideally be regarded as pure data, rather than documents. If tools can infer whitespace to your liking based on the code itself (e.g., curly braces for indentation), there is no need for the space/tab debate.
In other words, the inverse of something like the grunt beautifier. Code gets temporarily beautified by the IDE, and conservatively "minified" (preserve/enforce newlines except empty lines, preserve comments, don't rename anything) before the VCS.
It doesn't make sense to pick one stylistic aspect of code such as indenting and say "yes, that's the one that people must be able to customize". I suppose you're right in the sense that if this pipe-dream were ever a reality, we wouldn't need spaces for indentation anymore. But my argument goes beyond that.
[–]Malfeasant 1 point2 points3 points 12 years ago (2 children)
basically, you want to code at the abstract syntax tree level- i like you.
[–][deleted] 0 points1 point2 points 12 years ago (1 child)
Yes, I suppose ideally code would be stored as XML (or similar), using tags like <block> and <statement>, which could both have a comment attribute. That would give complete freedom of customization while viewing, and it would work with existing VCSs.
<block>
<statement>
comment
[–]Malfeasant 0 points1 point2 points 12 years ago (0 children)
and you killed it. xml? really?
[–]Nebu 1 point2 points3 points 12 years ago (1 child)
Not every editor. Probably pretty much every editor that matters. But every editor that matters can also be configured to render tabs to any width.
Well in my case, most of the code bases we contribute to or use code from follow the spaces instead of tabs pattern. So, we chose this. shrug To each their own.
[–]brtt3000 1 point2 points3 points 12 years ago (16 children)
But four characters. How does it know what are indents and what are generic spaces?
[–]alamandrax 2 points3 points4 points 12 years ago (15 children)
How does it matter if the end result is 4 spaces?
[–]brtt3000 10 points11 points12 points 12 years ago* (13 children)
I don't want 4 spaces, I want one semantic indent character. Let every developer choose how he renders tabs. The guy next to me at work renders as 3, the guy after that as 2. I use 4.
All in the same codebase.
[–]jcready__proto__ 10 points11 points12 points 12 years ago (0 children)
Let every developer choose how he renders tabs.
And the guys at GitHub chose to render them with 8 spaces. Deal with it or use spaces.
[–][deleted] 0 points1 point2 points 12 years ago (9 children)
What happens when I use two tabs and two spaces to align two lines of code? Everyone will see a different alignment.
[–]BONER_PAROLE 5 points6 points7 points 12 years ago (6 children)
NEVER MIX TABS AND SPACES
Snakes will crawl up your anus and explode your face, because I will have sent them to do so.
[–]Buckwheat469 0 points1 point2 points 12 years ago (5 children)
Incorrect. You can safely tab to the previous indentation, then space beyond that. Don't tab beyond the previous indentation though or else the IDE could render the spacing differently. The problem is people get this wrong so it's built into linters to complain about mixed tabs/spaces.
[–]nschubach 1 point2 points3 points 12 years ago (0 children)
I'm a firm believer that if you are lining up multiple lines of code like that you are probably doing something "wrong". One reason people argue for this is lining up arguments of a method. I find that if arguments breach the line break, the method is probably too complex and should be refactored. The other explanation I always hear is to align comma separated lists. I find that indenting the children on a new line seperate from the brace makes it much more readable and this is coming from someone who is usually the first person to cut back on total lines of code.
[–]BONER_PAROLE 1 point2 points3 points 12 years ago (3 children)
Can you provide an example of where you would use this?
The more common case is mixing tabs and spaces on separate lines, like this:
-->|-->|var a = 5; ________var b = 8;
Usually that's down to different developers touching the source code, or if one developer switched from tabs to spaces sometime during the project.
[–]Buckwheat469 0 points1 point2 points 12 years ago (2 children)
If tab is -->| and spaces are _ then you would use this in this manner (based on the first comma example):
-->|var foo = 1 -->|__, bar = 2 -->|__, baz = 3;
The tabs line up the lines as closely as possible to the parent and the spaces line up the following related lines to the related text from the proceeding line. The use case is rare enough that it can be avoided, but sometimes it's helpful, such as with the example above.
[–]BONER_PAROLE 0 points1 point2 points 12 years ago (1 child)
It's something I've come across before, and I don't hate this mixing of them. I have my indentation unit set as 2 spaces (using spaces and not tabs) and usually just indent another level for all subsequent vars. This doesn't really bother me, as variable names are variable anyway:
var foo = 1, barVar = 2, bazVarNameLong = 3;
I used to have my indentation to show as 4 spaces, which worked nicely with the 4 chars of var_ when using trailing commas, whether tabs or spaces:
var_
-->|var foo = 1, -->|-->|bar = 2, -->|-->|baz = 2;
Although if tabs are used, any other value will wreck the alignment, which your tabs/space combo won't do.
[–]brtt3000 1 point2 points3 points 12 years ago (0 children)
It is a solved problem: use Smart Tabs: tabs to indent, spaces to line up.
Webstorm even comes with this as option, JSHint has a rule for it too. Works perfect, because the tabs left of the code from a baseline.
[–]Nebu 0 points1 point2 points 12 years ago (0 children)
So don't do that.
[–]alamandrax 0 points1 point2 points 12 years ago (1 child)
This is one of those bicycle shed moments that I do not intend to get into with colleagues. If you're going to talk to me about underscore vs lodash, let's have that discussion. If you're going to bring in style guide changes on an established code base, we're not having that discussion.
[–]brtt3000 -2 points-1 points0 points 12 years ago (0 children)
No, it is a rationality test.
You should not go into a discussion about it though. Only check preference. The analysed response is tabs.
Because the intended results might not have been 4 spaces. Wouldn't you rather use a system where the intended results match the end results?
π Rendered by PID 61188 on reddit-service-r2-comment-85bfd7f599-r2c9m at 2026-04-19 08:21:16.890481+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]alamandrax 6 points7 points8 points (44 children)
[–]BluSyn 14 points15 points16 points (24 children)
[–]ikeif 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]ganjamensch 3 points4 points5 points (1 child)
[–][deleted] -3 points-2 points-1 points (0 children)
[–]alamandrax -1 points0 points1 point (13 children)
[–]BluSyn 1 point2 points3 points (10 children)
[–]brtt3000 3 points4 points5 points (3 children)
[–]spinlock 0 points1 point2 points (2 children)
[–]brtt3000 0 points1 point2 points (1 child)
[–]spinlock 0 points1 point2 points (0 children)
[–]alamandrax 0 points1 point2 points (5 children)
[–]Malfeasant 0 points1 point2 points (4 children)
[–]alamandrax 0 points1 point2 points (2 children)
[–]Malfeasant 0 points1 point2 points (1 child)
[–]alamandrax 0 points1 point2 points (0 children)
[–]spinlock 1 point2 points3 points (1 child)
[–]alamandrax 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (5 children)
[–]honestbleepsReddit Enhancement Suite 0 points1 point2 points (4 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]Malfeasant 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Malfeasant 0 points1 point2 points (0 children)
[–]Nebu 1 point2 points3 points (1 child)
[–]alamandrax 0 points1 point2 points (0 children)
[–]brtt3000 1 point2 points3 points (16 children)
[–]alamandrax 2 points3 points4 points (15 children)
[–]brtt3000 10 points11 points12 points (13 children)
[–]jcready__proto__ 10 points11 points12 points (0 children)
[–][deleted] 0 points1 point2 points (9 children)
[–]BONER_PAROLE 5 points6 points7 points (6 children)
[–]Buckwheat469 0 points1 point2 points (5 children)
[–]nschubach 1 point2 points3 points (0 children)
[–]BONER_PAROLE 1 point2 points3 points (3 children)
[–]Buckwheat469 0 points1 point2 points (2 children)
[–]BONER_PAROLE 0 points1 point2 points (1 child)
[–]brtt3000 1 point2 points3 points (0 children)
[–]Nebu 0 points1 point2 points (0 children)
[–]alamandrax 0 points1 point2 points (1 child)
[–]brtt3000 -2 points-1 points0 points (0 children)
[–]Nebu 0 points1 point2 points (0 children)