Styling tip: Tabs for indentation, spaces for alignment

Tabs. Spaces. Indentation. Alignment.
http://www.lb-stuff.com/tabs

Example:
__ is for tab
. is for space

object : {
_key..............:.value
_something........:.value2
_somethingelse....:.value3
}

Developers are pretty divided on whether or not you should align this way.

2 Likes

The correct way, according to my infallible opinion. 2 Spaces > Tab > 4 spaces. Don’t ever use more than 2 spaces, I hate it when code looks like this:

var person = {
        firstName:"John",
        lastName: "Doe",
        fullName: function () {
                return this.firstName + " " + this.lastName;
        }
}
person.fullName();  

The only time you can use tabs is if you specifically set your editor to replace a tab with 2 spaces. Then it is ok to use tabs. Never use spaces to align equals and colons. Use a plugin to do that for you like this:

These are my opinions. The ONLY way to properly format your code. Only use Visual Studio Code IDE. Sublime and Atom sucks. Just use 2 spaces. Just use Visual Studio Code. DO NOT USE SEMICOLONS EVER!!!

So it depends on the team you’re working with, but the benefit of tabs is that it allow contributors to use their own spacing settings in their editor. No matter what, go with what your team wants.

Do not use semicolons ever? For javascript? If that’s what you’re saying, I disagree. The JavaScript engine corrects your code if you’re missing semicolons. If that’s the case, might as well leave them in there since that’s how the code is interpreted.

You’re in good company with your opinion on semi-colons. Both Google’s style guide (Google JavaScript Style Guide) and the airbnb style guide (GitHub - airbnb/javascript: JavaScript Style Guide) require them.

from the second link:

Why? When JavaScript encounters a line break without a semicolon, it uses a set of rules called Automatic Semicolon Insertion to determine whether or not it should regard that line break as the end of a statement, and (as the name implies) place a semicolon into your code before the line break if it thinks so. ASI contains a few eccentric behaviors, though, and your code will break if JavaScript misinterprets your line break. These rules will become more complicated as new features become a part of JavaScript. Explicitly terminating your statements and configuring your linter to catch missing semicolons will help prevent you from encountering issues.

I was being sarcastic. These type of programming holy wars (tabs or spaces, semicolons or not - and yes a lot of people prefer not using semicolons, which ide, visual studio or atom, etc.) accomplish pretty much nothing and end up being endless bikeshedding. My suggestion was to avoid the argument altogether and just use a tool that formats for you:

Or use Eslint and airbnb guide and just stick with it.

That way, your code is always consistent, you can work with your team flawlessly, and you don’t waste your time deciding which style is best for you (I wasted 3 weeks trying to decide if I wanted to use semicolons or not, and I still have no preference).

1 Like

Not here to debate, but to inform one way of doing it so that it doesn’t cause someone a headache in their first job. I didn’t realize that some people use spaces for alignment until a lead dev at my job pointed it out. I thought the spaces were a styling mistake from developers having differing styling habits, so I was messing up the code for other developers by using tabs for alignment as well.

object : {
_..........key:.value
_....something:.value2
_somethingelse:.value3
}

Let’s just throw in Obj-C formatting, alligning around colon :

NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
id __block token = [center addObserverForName:@"OneTimeNotification"
                                       object:nil
                                        queue:[NSOperationQueue mainQueue]
                                   usingBlock:^(NSNotification *note) {
                                       NSLog(@"Received the notification!");
                                       [center removeObserver:token];
                                   }];
1 Like

“Any way that you align your code is fine, as long as it’s consistent.”

And then the internet shows you this:
image

2 Likes

I miss the good ole days, where the only debate is increment by 10 or 5. :slight_smile:

https://i.pinimg.com/474x/a7/70/ff/a770ff8f4cef18c449d52d0d62725796--basic-programming-programming-languages.jpg

2 Likes