8
hinst
3y

A question to JS developers...
Why 2 spaces indentation?

Comments
  • 12
    To annoy the hell out of us 4 space devs.
  • 4
    Why not? C is syntactically valid on a single line. A lot of languages are. Why bother with whitespace at all?
  • 4
    Also, an argument brought to you by the people insisting on 80 character line lengths and those that think spaces are better than tabs ("tab" isn't a fixed width, you can configure your editor to display whatever indentation you want).
  • 4
    I like both two and three. Four is just too wide, and eight is insane.

    Also, tabs ruin console output, and ruin vertical alignment too unless they’re mixed.
  • 5
    Code goes out of screen all the time 😂
  • 3
    Because of functions pyramids
  • 1
    I just use one tab
  • 0
    I had no idea all these complex indentation rules existed
  • 1
    So the callback hell doesn't look as bad-
  • 0
    😧🥺
  • 1
    Configure your IDE to deal with that for you and don't care about personal stakes of what the code should look like in the repo.
    If you are not agreeing with an already set coding standard, it is probably not a good idea to mess with something basic like leading spaces. If there isn't, make your case..
  • 1
    Why not? 4 spaces is most frequently too wide anyway.
  • 1
    @Root agreed. Three is better.
  • 0
    so I don't have to press space 4 times but only 2 times. Efficient development 😏
  • 1
    @iiii Three is not a number that any developer should ever use.
    It’s not a power of two. Hell, it‘s not even even!
    It’s just wrong 😬
  • 0
    @Lensflare But at the second level, you have 6 spaces... And that is the first perfect number.

    You wouldn't never get that when using only 2 spaces.
  • 1
    Any amount if spaces is wrong!
    The only valid option is tab. Spaces are for people who don‘t know better …
    Change my mind 😂
  • 0
    @Lensflare that's where you're wrong
  • 1
    @mojo2012 oh, hello, zoomer
  • 0
    @mojo2012 all hope for you is lost my child. You won‘t see the light of spaces. Only the the darkness of tabs.

    However, tabs are better than 3 spaces, that‘s where I agree. But only because tabs are technically 0 spaces and 0 is an even number and even a power of 2.
  • 1
    The answer seems to be ease of reading:

    "As to "practical" advantages, I guess it may be easier to view and edit files if you don't waste too much screen real-estate with deep indentation. From that perspective 2 character indentation is better than 4 or 8 character indentation."

    from: https://stackoverflow.com/questions...

    Also, this:

    "Two spaces is popular in JS because "callback hell" makes the average program have many more indents than the average C or PHP program. Popular in HTML for the same reason. You can get away with 8 space indents if you're never going to be indented more than three indents.."

    from: https://reddit.com/r/javascript/...
  • 1
    @CaptainRant I also see that Google applies a 2 space indents in their open-source code style guideline.
    Yet Microsoft uses a 4 space indent in C#.

    But again when using Visual Studio (maybe other IDE too) you can use smart indenting, four-character indents, tabs saved as spaces settings.

    This will make the question pointless and just a preference of the coder and only uses tabs.

    I have a project and I used a different IDE with lets say 1 tab = 3 spaces.
    An other dev used 1 tab = 2 spaces and in git when he edits the file, it will change all the indents to 2 spaces. What a nightmare...
  • 1
    @CaptainRant speaking about callback hell, doesn’t JS have async/await now?
  • 0
    @Lensflare same thing in a different flavor
  • 0
    @iiii how? I mean one of the things that async await solves is flattening the code by making it look like it is synchronous instead of having completion callbacks.
  • 0
    Tbh I don’t do a lot of manual formatting bc the ide does it automatically when I save lol
  • 1
    @Grumm I gathered all that information already but I didn't want to provide an opinionated answer, but rather a documented one. Though, neither the ECMAScript specification nor Google's guidelines seemed to provide a real reason why they made this choice.
  • 0
  • 0
    @Lensflare isn't it synchronous if you're anyway waiting for everything?
  • 0
    @iiii you seem to be not familiar with that 😄

    The code is waiting at each "await", yes. But it is still asynchronous in the same way as completion callbacks are. It is kind of syntactic sugar which hides the callbacks and makes it look synchronous.
    How it works exactly depends on the language but the idea and the syntax is always very similar.
    The purpose is to avoid nested callbacks, increase readability and prevent mistakes where you would forget to call the completion handler.
  • 1
    @Lensflare yes but async js doesn't all of a sudden fix the humongous amounts of js that has been laying around for 30 years since Netscape invented it.
  • 0
    @DarkMukke yeah but if the question is how many spaces should one use when writing code, then async/await is kinda relevant here. Because the main argument for less amount of spaces seems to be callback hell.
  • 0
    @Lensflare yep, you don't start using semicolons because tcl/tk now supports it, the coding standard has been no semicolons for decades, same argument for js, async or not
Add Comment