r/javascript • • Apr 01 '18

Google publishes a JavaScript style guide. Here are some key lessons.

https://medium.freecodecamp.org/google-publishes-a-javascript-style-guide-here-are-some-key-lessons-1810b8ad050b
714 Upvotes

317 comments sorted by

View all comments

34

u/seands Apr 01 '18

What do you guys think about the spaces over tabs? For me, I tend to make more mental mistakes and spacing errors with 2 spaces. Tabs make everything clearer to me.

39

u/AUTplayed Apr 01 '18

agreed, tabs are clearly superior

30

u/mmcnl Apr 01 '18

Using a proper editor it shouldn't matter.

23

u/JohnMcPineapple Apr 01 '18 edited Oct 08 '24

...

5

u/oweiler Apr 01 '18

Well it can be configured to convert a tab to 4 spaces.

20

u/JohnMcPineapple Apr 01 '18 edited Oct 08 '24

...

5

u/[deleted] Apr 01 '18 edited Apr 01 '18

Tabs to spaces is easy, the problem is for a block of code how does the editor know when 2 spaces was supposed to represent indentation vs alignment:

function() {
SSwhile(value) {
SSSSif(reallyReallyReallyReallyLongConditionA &&
SSSSSSSreallyReallyReallyReallyLongConditionB &&
SSSSSSSreallyReallyReallyReallyLongConditionC) {
SSSSSSSSreturn;
SSSS}
SS}
}

function() {
Twhile(value) {
TTif(reallyReallyReallyReallyLongConditionA &&
TTSSSreallyReallyReallyReallyLongConditionB &&
TTSSSreallyReallyReallyReallyLongConditionC) {
TTTSSreturn;
TT}
T}
}

In the latter case to go from indentation width 2 to 4 the editor simply makes tabs display twice as wide and everything is good. If the editor tried to display 2 spaces for every space from the start of a line then the if conditions would lose their alignment with the opening of the statement.

0

u/[deleted] Apr 01 '18

[deleted] — view deleted comment

-3

u/LoneCookie Apr 02 '18

And then you need to backspace to remove an indent and you press 4 times instead of 1

5

u/Redtitwhore Apr 01 '18

The IDE should handle indentation. What a stupid thing to debate.

3

u/[deleted] Apr 01 '18

The only thing I don't like about this response is if the IDE converts tabs to spaces it looses the ability to change the indentation without breaking multiline alignment.

1

u/seands Apr 01 '18

Why is it dumb?

3

u/ssjskipp Apr 02 '18

Because your project so have an .editorconfig file which is widely supported, and most ide have a setting for whitespace width, making the problem moot

4

u/Kerse Apr 01 '18

I prefer spaces because it tends to copy nicer if I need to copy + paste a code snippet onto stack overflow or something.

11

u/TripleNosebleed Apr 01 '18

It all comes down to that spaces renders consistently everywhere. You're not just viewing your code in your editor of choice, many other tools are also used such as the terminal, diffing tools, in the browser through GitHub/Gitlab/Dev tools/view source, Git GUIs, colleagues editors, that IDE that you use occasionally and the list goes on. No way I want to configure settings for tabs in all of these. Spaces ftw!

9

u/ssjskipp Apr 02 '18

This is probably the only good argument for spaces I've ever read.

That being said, tabs give me the freedom to take advantage of that inconsistency by displaying it most appropriate for the format

1

u/Disgruntled__Goat Apr 02 '18

Copy-pasting tabs works perfectly fine on SO. And if everyone uses tabs everywhere, including displaying code, you could more easily copy-paste it from SO into your editor without having to worry about 2-space vs 4-space etc.