Skip to main content

My Views on Code Indentation

I have read many, many articles about the whole tab vs. space indentation thing. Personally, I don't necessarily agree with most of them. They will require the coder to use a specific indentation size and stick with it, even forcing that on other coders.

First off, let me outline my method for indenting code. Then I will explain the reasons and advantages/disadvantages.

When I indent code, I will use tabs, but only at the beginning of a line. To align something in the rest of the line, I will use spaces. If a line spills to the next line(s), I will indent that line two tabs further.

Rationale:

Tabs
Why tabs? First off, they're compact in the file (1 byte each). This is really insignificant with current disk sizes, but still. If you indent in spaces, then your file will be larger (unless you indent with one space).
Another advantage of tabs is that a tab is a tab. It doesn't specify by how many spaces the code is indented, but rather by how many tabs it is indented. That means if a coder likes 2-space indentation, they can set up their editor to display tabs as two spaces. Or 4 spaces, or even the crazy (?) Microsoft 8 space method. With tabs, you are not forcing your indentation style on other coders. Two different coders can view the same exact (byte-by-byte) file with 2, 3, 4, 8 (whatever, go nuts) spaces per tab.
One more advantage: the Tab key. Why would pressing a tab key insert spaces? There's a space bar for that. It makes logical sense that a Tab key would insert a tab.
One last advantage is that it's easier to delete the indentation. If your Tab key automatically inserts 4 spaces, then you need to hit Backspace 4 times to get rid of one press of the Tab key! At least for me, that's pretty inconvenient.

Spaces
If something inside of the line needs to be aligned to make code more readable, spaces should be used instead. The reason? If it needs to be lined up, you need to specify exactly how many spaces are needed. That way, two editors with different tab sizes will still properly align the code inside of the line.
This isn't about forcing your indentation size on other coders, it's about making your code readable, which means your code has to be aligned exactly, regardless of how many spaces are specified as one tab.

Multi-line Lines
One logical line can occupy multiple physical (not in the literal sense) lines in the file. When that happens, the rest of the physical lines after the first should be indented two extra tabs in. Why? It's easy to see that that line is a continuation of the previous one. If it was one extra tab, it could get confused with a block of code that gets indented one tab in. Obviously if it was no extra tabs, it wouldn't be set apart in any way and you'd be stuck wondering how someone could miss so many semicolons! More than two doesn't make sense, as anything past two extra tabs will just shorten your available horizontal space without adding any more clarity to the code.
Some people I've seen like to indent their code up to a parenthesis or some other significant mark. With tabs, of course, that's not possible because of the varying tab sizes. What about spaces after the initial indentation with tabs? Wouldn't it be the same as aligning with spaces? That is a good point, and can be done when the significant marker is not very far in the line, but what happens when that parenthesis or whatever marker is far into the line? You end up limiting your horizontal space at the expense of making your code a little more organized. From here, it's a cost-benefit analysis. How much horizontal space would you be willing to give up for a certain amount of organization? In the end, I've found that indenting with two extra tabs gives enough clarity to the code without sacrificing a lot of horizontal space.

Sometimes, I feel that spending more time on this issue is just a waste, but I feel that there should be a single indentation style that coders can agree upon to make all their lives easier. Can you imagine if someones editor automatically replaced all the tabs in a file with spaces? In a revision control system, it would look like every single line changed!
That said, I'm sure that my method isn't the Holy Grail of indentation. Like all methods, it can be refined and worked on until most people agree with it.

Anyway, that was my two-cents about code indentation. Do you agree or disagree with it? Anything you would change? I would really like to hear some feedback about this.

Comments

  1. rb88 Casino: Bonuses, Promotions & Bonuses - TopBet
    Sign up at the RB88 Casino and claim your welcome 1xbet bonus. planet win 365 Discover top rb88 casino promotions, game variety, live dealer, rb88

    ReplyDelete

Post a Comment

Popular posts from this blog

Linux on XPS 15 9550/9560 with TB16 Dock [Update:3/29]

Finally got a laptop to replace my fat tower at work - Dell XPS 15 9560. I was allowed to choose which one I wanted and chose the XPS for its Linux support since Dell ships developer edition XPS's running Ubuntu so I figured Linux support would be better than other manufacturers. At first they got me the model with the 4K screen but my monitors are 2K and multi-dpi support in Linux is virtually non-existent and even hi-dpi support on its own is pretty terrible. So I got it exchanged for the model with the regular 1080p screen (which happened to also be the updated 9560 model), which works much better. I'm very glad to report that pretty much everything works, including the TB16 desktop dock, with just a bit of settings tweaking. This post is to help anybody considering getting this setup or looking for help getting things working. For now, I am running Kubuntu 16.04 with KDE Neon installed. List of things I explicitly tested and work: WiFi, Bluetooth Thunderbolt charging ...

Converting single-touch events to mouse events

Many times, there are various interactions that take place in a web app based on mouse movement, clicking, dragging, etc. With a mouse, this is fairly simple, as there is a single point, and events like mousedown, mouseup, and mousemove can be used to discover the state of the mouse at any given time. On mobile browsers, however, it's a different story. Most phones support at least some kind of multi-touch, and the mousedown and mouseup events are not fired (there is, after all, no button). In addition, mousemove will only be applicable when dragging. Instead, mobile browsers dispatch touchstart, touchmove, and touchend events. Moreover, these events keep track of all touch points, not just the first or last one, and as such make handling them a bit more difficult. For the purposes of an application that only requires tracking clicks and drags, it is more convenient to translate these touch events into mouse events without having code duplication. This can be accomplished quick...

Broadcom Bluetooth Driver: Installer or Virus??

My primary computer at the moment is an HP dv6 laptop (with Broadcom 2070 Bluetooth chip). Upon getting my laptop, I immediately wiped the hard drive and slapped on a fresh copy of Windows 7. Installed the drivers from HP, and everything was rolling along nicely... Fast-forward 4 months... Now I'm having some issues with Bluetooth communication between my phone (Android) and my laptop. So I reinstall the Bluetooth driver... The installation took a long  time, but eventually errored-out, and quit. Of course, I had left the computer unattended during this, and came back to an almost-empty hard drive (!!!!). Restored for backup, some minor data loss, no problem... I thought I had snagged a virus in the download (even though it was direct from HP's site). Redownload, scan with multiple scanners, run it ....... SAME RESULT!!! Giving up on the official installer, I just took the drivers that the installer unpacked, and manually installed them... That didn't solve my original pr...