Please, for the sake of “the next guy” (or yourself a month down the line) tab format your code. Especially if you aren’t that “great” a programmer and your 1240 line file is a mass of spaghetti if statements.
If you have a nasty mess of if statements, there is just no easier way than to tab format it. However, no tab formatting is better than a little tab formatting. Take this for instance:
if(this == that){
if(that == this){
Do this
Do that
if(this == that){
Do this
}else{
Do that
}
Do this
if(this)
Do that
if(that){
Do this
}
}else{
if(this == that){
Do this
Do that
}else{
if(this)
Do that
if(this)
Do that
if(this == that){
Do this
}else{
Do that
}
}
if (this == that)
Do that
}
if (this == that)
Do this
}
It’s a mess, but at least it’s readable. If you ever do anything that looks like the above, you have almost definitely done something wrong. You probably should have used some sort of multidimensional array, or stored some stuff in a DB and done a good SELECT statement.
But if the word “multi-dimensional array” sounds like a sci-fi weapon to you, and you can’t function without if statements, then please tab them.
If you don’t tab your code blocks consistently you will end up with this:
if(this == that){
if(that == this){
Do this
Do that
if(this == that){
Do this
}else{
Do that
}
Do this
if(this)
Do that
if(that){
Do this
}
}else{
if(this == that){
Do this
Do that
}else{
if(this)
Do that
if(this)
Do that
if(this == that){
Do this
}else{
Do that
}
}
if (this == that)
Do that
}
if (this == that)
Do this
}
I DEFY you to be able to read that code. It’s insanity, and will drive any sane person to the brink of delirium.
If you don’t tab them, that’s OK (but not optimal). It’s better than tabbing inconsistently. But please put brackets on EVERY if statement. Yes, all of them. Even if it’s just one line. I know you don’t have to, but just do it. There is just no good reason not to. One good reason to do it is that when you come back in a month and add a second line to that if statement, if you forget to add brackets at that point then that second line will always execute, and thus a bug is born. Make it fail safe.
Now, if you inherit some horrible quasi-tabbed code (like the bunch shown above) you do have some options.
A bad (but better than nothing) option is to open it up in an IDE, highlight it all and hit SHIFT+TAB until there is no formatting (then you could even go through and tab it all if you can figure it out).
A good option is PHP Developer (and many other IDEs) which will show you where the opening and closing brackets for a bracket set are. Aptana (which is basically eclipse) does not. Textpad does not (Although you can highlight an opening bracket and hit Ctrl+m to see the closing one). Dreamweaver does (I think. I believe it will actually re-format your code for you).
For those of you newbs out there learning to code for the first time, use an IDE like Textpad or Dreamweaver and set it to help you out with your formatting (STOP USING NOTEPAD!!!!). In Textpad this is as simple as clicking Configure>Preferences>Document Classes>[Click your programming language] then select “Automatically Indent Blocks”. If you use another IDE figure it out and then do it and be consistent. There is almost nothing I can think of style-wise that is more detrimental to success than tabbing your blocks of code. It’s one of the first steps to becoming a “good” programmer. I won’t say it’s what separates the men from the boys, but maybe the boys from the babys.
ChomperStomp
Hallelujah Button
Status-bar Calculator
Andy Harris' Books
Outer Spice Web Company
Paul Irish's Blog
The Daily WTF
yayQuery Podcast
You mean people actually spend time worrying about formatting their code? I guess I take Visual Studio for granted.
Yep. Not everyone has $300 to plop down for a WYSIWYG editor.
Some people still develop in notepad *shudder*.
If you are looking to invest in your IDE, then by all means purchase Dreamweaver or VS.NET
If you don’t have a large budget, then get something free (or almost free) like Textpad, PHPDeveloper or Aptana.
It’s hard to convince your company to lay down $300 per developer for VS when all you can really tell them is things they don’t understand like “code formatting” and “syntax highlighting” or even “line numbers”, especially when they could point to FREE alternatives.
You could of course make the case of increased productivity… But then you have to actually deliver and be more productive. So be careful what you wish for
I guess I didn’t have to use code formatting as my excuse. I’m a .NET developer – it’s really the only option. If syntax highlighting and line numbers are the only reasons you think you need VS then don’t waste your money. You can get those features with free IDEs, just like you said.
I’ve come across some pretty bad code formatting in my day. I’ll even watch someone code sometimes and they’ll not indent something or not space something correctly. It’s all I can do to not reach over them and fix it immediately. Of course, I actually liked to keep my room clean as a kid, too.
So, good point – you can code badly, but at least it’s with style!
Most IDEs have “auto-formatting”. For some reason I didn’t know about that when I wrote this post, but in Eclipse, for instance, you can just hit Ctrl+Shift+F and all that mess would have become instantly formatted correctly…