Jump to content

Oh indentation indentation...

Mira Yurizaki

I've been reading up on some style guides and trying to apply a universal indentation of sorts so I can move on with my life across languages in this regard (of course, if the source already exists, I'll begrudgingly use their style). As far as I can find, the big names use the following indentation sizes

  • Google uses two spaces. It's also recommended in JavaScript to use two for space saving reasons since source code is sent to the client as is (but isn't that what a minifier is for?)
  • HTML, Python, and pretty much most of the coding world seems to like four spaces.
  • Linus Torvalds demands eight spaces (and calls anyone who prefers two or four heretics), as it makes the indentation much clearer and serves as warning you're nesting too much.

So what's your take on this? I'm used to using two spaces, but for some reason four spaces is looking mighty attractive. But then I'm all "I get less space if I adhere to the 80 column standard everyone still uses.

Link to comment
Share on other sites

Link to post
Share on other sites

I've personally always used 4 spaces for indentation. It's default in 95% of IDE's and text editor's for a reason. That's interesting that Google uses 2. FWIW, Microsoft use 4. I'm not sure about any of the other big players. In Android dev presentations led by Google employees, I've never seen them use 2.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Tazman192 said:

I've personally always used 4 spaces for indentation. It's default in 95% of IDE's and text editor's for a reason. That's interesting that Google uses 2. FWIW, Microsoft use 4. I'm not sure about any of the other big players. In Android dev presentations led by Google employees, I've never seen them use 2.

To quote at least https://google.github.io/styleguide/cppguide.html#Spaces_vs._Tabs

Quote

Use only spaces, and indent 2 spaces at a time.

We use spaces for indentation. Do not use tabs in your code. You should set your editor to emit spaces when you hit the tab key.

Though I did find their Python style guide uses four spaces, probably to comply with PEP8

Link to comment
Share on other sites

Link to post
Share on other sites

I often use 2 in JavaScript, and didn't always do that. 4 spaces just felt like too much, and I didn't like it. It's as simple as that. Honestly, whatever you prefer is what you should use. I also discovered this: https://github.com/feross/standard, which has an interesting set of style rules, but I don't use it, just use their reasoning for choosing that size.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

I've always used tabs. However I've always used coding focused editors, so I'm sure it put spaces in for me. I think the tabs were probably ~4 spaces or so. Anything smaller and it's hard to discern where the indentation is. 4 spaces seems enough to be able to find the beginning and matching end for a loop just by a quick glance. 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, corrado33 said:

I've always used tabs. However I've always used coding focused editors, so I'm sure it put spaces in for me. I think the tabs were probably ~4 spaces or so. Anything smaller and it's hard to discern where the indentation is. 4 spaces seems enough to be able to find the beginning and matching end for a loop just by a quick glance. 

python checkers will pull you up on white space over tabs so I've always used tabs. Each nested part of code is one tab in from the last.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

I don't use indentations what now? I just put my programming code in a single line.

 

:(:(

Link to comment
Share on other sites

Link to post
Share on other sites

I use 4 since that is what I taught that when learning C in a Unix environment. Funny enough, the text editors I used had tab as 8 spaces.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

You forgot to list tabs as an option. Some people like to use tabs because many editors can be customized to display tabs how you want without effecting the plain text. So if someone on your team likes 2 spaces, you like 4, and another likes 8, you can all view it the way you want with tabs.

 

That seems pretty appealing although I've never really given it a chance. I've been using 4 spaces for as long as I can remember.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, M.Yurizaki said:

I have a feeling Linus's influence got a hold of them if it was vim or emacs...

I haven't used emacs but vim, nano, and pico all have 8 spaces.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

I just hit Tab in any IDE or text editor I'm working in, if it compiles I'm happy :)

CPU - Ryzen 7 3700X | RAM - 64 GB DDR4 3200MHz | GPU - Nvidia GTX 1660 ti | MOBO -  MSI B550 Gaming Plus

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, madknight3 said:

You forgot to list tabs as an option. Some people like to use tabs because many editors can be customized to display tabs how you want without effecting the plain text. So if someone on your team likes 2 spaces, you like 4, and another likes 8, you can all view it the way you want with tabs.

 

That seems pretty appealing although I've never really given it a chance. I've been using 4 spaces for as long as I can remember.

Tabs doesn't provide consistency though. Especially since a lot of professional coding groups still say you should keep code within 80 columns, if you have different tab spaces, the person who coded with 8 space tabs will run into line break issues if they opened up a file from someone who coded in 2 space tabs.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, M.Yurizaki said:

 

I've been reading up on some style guides and trying to apply a universal indentation of sorts so I can move on with my life across languages in this regard (of course, if the source already exists, I'll begrudgingly use their style). As far as I can find, the big names use the following indentation sizes

  • Google uses two spaces. It's also recommended in JavaScript to use two for space saving reasons since source code is sent to the client as is (but isn't that what a minifier is for?)
  • HTML, Python, and pretty much most of the coding world seems to like four spaces.
  • Linus Torvalds demands eight spaces (and calls anyone who prefers two or four heretics), as it makes the indentation much clearer and serves as warning you're nesting too much.

So what's your take on this? I'm used to using two spaces, but for some reason four spaces is looking mighty attractive. But then I'm all "I get less space if I adhere to the 80 column standard everyone still uses.

I hit tab and let whatever IDE I'm using handle it for me. Normally they are set to convert a tab to four spaces, which looks pretty nice to my eyes.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, gabrielcarvfer said:

If a line in special is much longer than the rest, break it into smaller lines and that is it.

That's probably why lots of people still adhere to it. If you're breaking 100 columns, you might be doing something wrong.

 

Like how Linus will tear you a new one if you have more than two nested things (I try not to take his coding style to heart, but my goodness he's like the drill instructor from Full Metal Jacket when it comes to coding style)

Link to comment
Share on other sites

Link to post
Share on other sites

Just do what your organization refers to in their style guide. You'll have to change based on where you work. It's not asking for much!

 

Just configure your editor to make tab equal 2 spaces or 4 spaces or \t.

Current: i7 990x / GTX 950 / 12 GB RAM / Micro ATX

Waiting for the next amazing Mini ITX case!

Link to comment
Share on other sites

Link to post
Share on other sites

I normally hit tab twice(1 tab=4) and that just helps me understand that 2 things are connected more easily

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×