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.
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.
rb88 Casino: Bonuses, Promotions & Bonuses - TopBet
ReplyDeleteSign up at the RB88 Casino and claim your welcome 1xbet bonus. planet win 365 Discover top rb88 casino promotions, game variety, live dealer, rb88