How to Indent Code
Indenting Your Code -- Don't Be That Guy
The Rules:
- Indent only at the beginning of the line
- Use a (real) tab or spaces to indent
- Preferably a tab, it has a deep history related to indention and in software it tends to mean "indent"
- Be consistent
- No space character shall immediately follow a tab character to indicate a full level of indention
- If using spaces to indent, use the same number of spaces to represent an indention level
- No tab shall come after a non-tab character on a line
- This supercedes the rule about consitency -- formatting data, especially comments, not related to indention should be done with spaces
Questions and Answers:
- Q: Why Tabs over Spaces ?
- A: Tabs, when used properly, can allow the user to view the code with their own indention preferences without requiring them to reformat the code. The tab character can also be used to represent the semantic concept of "indent".
- Q: Why No Tabs after non-Tab ?
- A: The implementation of a tab-stop requires it to set the carriage or cursor to pre-set columns. If a tab character occurs after a non-tab character, the expansion of a tab when rendering will be different for different tab-stop-widths. It also makes no sense when treating "tab" as a semantic indent, since you should only be indenting at the beginning of the line.
- Q: Why No Spaces after Tabs ?
- A: Spaces following a tab character represent indentation.