A recent discussion with a friend broached the subject of code formatting. This is one of those subjects where I truly believe no happy medium will ever be met between major parties. Some people like certain methods, and others just like the opposite. That's how its been for ages, merely a matter for personal preference and pointless debate. This point in no way intends to change that, but if you come off thinking my chosen formatting method is the way to go, I'm glad I could help you.
Before we get into this, let me make a statement about my personal views on the
importance of formatting your code. When I teach people to code, I do what I can to stress over and over again that you
must format your code. If you take the mere seconds of extra time it takes to ensure that your code is consistently tabbed, spaced, etc, those who might read your code in the future will forever love you.
Formatting A-La AndyI will be outlining my formatting for PHP, the all-popular and fluffy web scripting language. The majority of the principles I use for my PHP code carry over to the other languages I use with little to no modification. If you have any questions, of course feel free to let me know.
The first big thing with my formatting style, is to use tabs. Use tabs, not tabs as spaces, and ensure that your tabs are 5 characters (spaces) in size.
Next you'll want to be sure that you always leave an empty line between your lines and the code that goes between them. Also, always begin your first level of code (where a level is defined as the top level of 'nested' code in your file) at a one tab indent:

With comments, I try to use only two different types in PHP, but will sometimes feel a little frisky and change it up slightly. For large comment blocks that are used as descriptions or copyright notices, etc, I use the C style comments /* */, and for single line comments I use the // style comments. I rarely use the # style comments because really they're just another way of doing the // comments, and who needs redundancy!

Another big part of my formatting style is grouping. I like to do my best to group code, meaning that if I'm setting some variables up that are related, I'll put them on lines directly after each other, but as soon as I am doing something different, I will add an empty line between the two parts so there is a clear definition of the workflow 'group'.
When dealing with parenthesis "( )", I space them out only in a few instances, mostly when several can be inside of others (most logical statements, such as if() and while()) or when it is a function declaration. Everywhere else (with very few exceptions), I squash the parenthesis against the characters they are enclosing, as well as the characters they originate from. The next example covers most of what I've just said...

You'll also notice, when making nested code, I use one tab per nest. Since I start out with all of my code 1 tab indented already, that means the first nest of code will be indented 2 tabs, the second 3 tabs...so on and so forth. Also, with logical operators I use brackets on the following line at the same indentation as the logical statement resides on, I think it makes matching up the brackets with their logical statement a lot easier.
Finally, I can't stress this enough...but you should NOT leave anything on empty lines. If there is a blank line, let it be blank instead of filled with tabs. It takes about 2 keystrokes to hit ctrl+home and then backspace, its not too much to ask when you're trying to be neat with your code.
Those are the basics of my methods, and I don't expect anyone (except those that are coding on my projects) to follow them. As stated earlier, if they help you, awesome and glad I could be of assistance. If they don't help you, sorry to have wasted your time. :p
- Andy
Labels: Development