DEV Community

Discussion on: 4 Spaces or 2?

Collapse
 
avalander profile image
Avalander • Edited

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)

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

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. :/

Collapse
 
nektro profile image
Meghan (she/her)

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

Thread Thread
 
avalander profile image
Avalander

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

Thread Thread
 
nektro profile image
Meghan (she/her)

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

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

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.

Collapse
 
temujins profile image
Sifatul Rabbi

@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