4 Spaces or 2?
Jesse M. Holmes

Jesse M. Holmes @wolfhoundjesse

About: I follow new coders. Brass-bander. ADHD Avenger. Lover of giant hounds. Believer in the impossible.

Location:
Annapolis, Maryland, United States
Joined:
Jul 27, 2018

4 Spaces or 2?

Publish Date: Jan 30 '19
10 44

Let's pretend we aren't going to argue Tabs vs. Spaces. Is 4 spaces really more legible than 2 spaces? It seems to be the default everywhere I look, and I suspect it has to do with Tab size.

I've always preferred two spaces.

Comments 44 total

  • jan paul
    jan paulJan 30, 2019

    2

  • Tyler V.
    Tyler V.Jan 30, 2019

    My preference for 4 spaces comes from starting out with Python.

    Since starting to use Prettier in VS Code, I've been having issues with the setting being changed to 4 spaces in the interaction with the Rainbow Indent extension, and as such have been moving to using 2 space instead.

    I don't think either is "more readable" than the other. When there's a bunch of indent widths, even the rainbow indent/bracket colorizer isn't enough to stop me from needing to use my finger/piece of paper to figure out where the matching curly brace is/should be.

  • Mikael Klages
    Mikael KlagesJan 30, 2019

    I doubt anybody has done actual research on which is more legible, though I assume whatever one is used to will be the most legible to them.

  • Grzegorz Ziemonski
    Grzegorz ZiemonskiJan 30, 2019

    Tabs, people, use tabs. Then you can choose however many spaces you wish.

    • Jesse M. Holmes
      Jesse M. HolmesJan 30, 2019

      In VSCode there are options like setting the tabs equal to a number of spaces, or you can even have tab insert spaces, I believe.

      In other words, I’m not sure what you mean. 😂

      • Avalander
        AvalanderJan 30, 2019

        If the line starts with the tab character, I can configure VS Code to show it as either two or four (or any other number) spaces, so the same code can have different indentation space in your machine than in mine.

        If I configure VS Code to set two spaces every time I press tab, it is unfortunately not possible for somebody who prefers four spaces indentation to visualise the code that way.

        • Jesse M. Holmes
          Jesse M. HolmesJan 30, 2019

          This is the first time any part of this discussion has made sense to me. 🤩

    • Jesse M. Holmes
      Jesse M. HolmesJan 30, 2019

      I started contributing to a project last night, and after asking this question an issue appeared a few hours ago that is pretty relative to your point!

      Tabs are breaking YAML

      • edA‑qa mort‑ora‑y
        edA‑qa mort‑ora‑yJan 31, 2019

        This is unfortunate that YAML chose not to accept tabs. There's no practical reason not to allow it.

    • Meghan (she/her)
      Meghan (she/her)Feb 4, 2019

      or use vs code and still be able to pick the number of spaces you want

  • Andrew Bone
    Andrew BoneJan 30, 2019

    2 spaces are king!

  • Avalander
    AvalanderJan 30, 2019

    I prefer 4 spaces because they take up more screen space, which means that I can fit fewer indentation levels before it starts to feel uncomfortable, so in general it means that I start addressing some potential spaghetti code issues earlier.

    (although tabs are better because anybody can configure their editor to show as many or as few spaces as they want to, I had to say it)

    • edA‑qa mort‑ora‑y
      edA‑qa mort‑ora‑yJan 31, 2019

      The argument about "how many spaces" is really an argument in favour of tabs.

      It goes beyond how many as well, since the moment you use spaces, you start having people align code with them as well. :/

      • Meghan (she/her)
        Meghan (she/her)Feb 4, 2019

        vs code can auto format an entire file to use the number of spaces one desires, and always uses spaces

        • Avalander
          AvalanderFeb 4, 2019

          Yeah but at my work we use two spaces, can I configure vscode to display four spaces but actually save the file with two?

          • Meghan (she/her)
            Meghan (she/her)Feb 4, 2019

            not that I'm aware of. you'd just have to change it before you commit

            • edA‑qa mort‑ora‑y
              edA‑qa mort‑ora‑yFeb 4, 2019

              Auto-formatting causes trouble with source control. You end up having large patch-sets than desired. It can be hard to locate what was actually changed in a file.

      • Sifatul Rabbi
        Sifatul RabbiOct 25, 2021

        @avalander
        You can use prettier cli to format the files. Install prettier dependency and add this script in package.json of any javascript/typescript projects
        {
        "scripts" : {
        "format:two": "prettier --tab-width 2 --write \"src//*.ts\" \"test//.ts\",
        "format:four": "prettier --tab-width 2 --write \"src/
        /.ts\" \"test/*/.ts\"
        }
        }

        then run:
        $ npm run format:two
        or
        $ yarn run format:two

        for more info you can checkout prettier official website

  • Daniel J Dominguez
    Daniel J DominguezJan 30, 2019

    8

    I used to prefer 4, but I figured, if I find myself indenting too much, then it should probably be refactored. Sort of learned about this by reading about the Linux style guide.

  • Pavel Vergeev
    Pavel VergeevJan 30, 2019

    I use 4 for Python code (it's PEP8) and 2 for html/css/js.

  • Jens Becker
    Jens BeckerJan 30, 2019

    3 spaces

    const test = () => {
       console.log("three is the best");
    }
    
    Enter fullscreen mode Exit fullscreen mode
  • JavaScript Joel
    JavaScript JoelJan 31, 2019

    We will argue 4 vs 2 spaces until we are blue in the face. God forbid someone to suggest a compromise of 3 spaces, for they will be murdered before they finish their sentence.

    3 spaces is the most logical compromise. But because humans are illogical, we'll never see a 3 spaced code file.

    • Avalander
      AvalanderJan 31, 2019

      I bet you set the TV volume to odd numbers.

      • Andrew Bone
        Andrew BoneJan 31, 2019

        I think you'll find the volume should be changed in increments of 5... 😉

        • Three Dogs
          Three DogsMay 3, 2020

          The reason volume is adjusted in increments of 3dB is bc changes less than 3dB are imperceptible to the human ear.

      • JavaScript Joel
        JavaScript JoelJan 31, 2019

        I use the Fibonacci sequence to set my volume.

    • dev-head
      dev-headFeb 2, 2019

      we tried that at a previous gig; made everyone unhappy and uncomfortable. if it's code, IMHO, it's a tab (4 spaces!); if it's markup or documentation or config, two is perfect.

    • Meghan (she/her)
      Meghan (she/her)Feb 4, 2019

      how dare you suggest we do something that is not a multiple of 2?? /s

  • Lars Klopstra ⚡
    Lars Klopstra ⚡Jan 31, 2019

    Most often I use 2 spaces, but it really depends on the language. And I auto refactor tabs to spaces ;)

  • Quentin Sonrel
    Quentin SonrelJan 31, 2019
  • Curtis Fenner
    Curtis FennerJan 31, 2019

    I prefer four because its much easier to quickly scan for what braces match and where blocks end and begin.

    Deeply nested code is usually to be avoided, so I don't miss the space is takes up.

  • Jaaki
    JaakiFeb 1, 2019

    Oh god, that question will break us all.

    Golden Ratio, the deeper you go, the more space, as well as above and below.

    Until your code turns around on itself, then you're next level...

    Does depend on the font though, monospace, bold etc.

  • miguel
    miguelFeb 2, 2019

    I always use 3 spaces!

  • Mihail Malo
    Mihail MaloFeb 3, 2019

    Am I the only person that Bracket Pair Colorizer just annoys and distracts?
    I'm not sure if I should set violent colors or something, but any time I try it it only harms me.
    I just have "editor.renderWhitespace": "boundary" and the default indent guides and it seems enough for me.
    Ditto for Indenticator and indent-rainbow.
    indent-rainbow also doesn't autodetect the tab settings, which are a builtin VSCode thing, why this :v

  • morgana
    morganaFeb 3, 2019

    Either works for me.

    It's when I have to open up some C or Java that has mixed spacing that I start spewing profanities.

Add comment