I've developed the following coding standard during the years when I've been responsible, on and off, for reviewing code by other programmers. It's suitable for most text-based programming languages and data specification formats.
Rules
Indent upon nesting
Where control structures or data objects nest, indentation helps a reader to keep track of the nesting level.
Indentation should render properly in all relevant IDE/editor configurations. Nobody should see stuff in the inner scope closer to the left than the outer scope. Seriously, spend 10 minutes to understand tabs vs spaces issues.
Use profanity judiciously
Profanity makes code easier to write, but harder to read and modify. Profanity signals a potential hazard, but distracts attention from the details – the opposite of a good warning. With profanity, less is more.
Be careful with comments, more careful with identifiers, and still more careful with the ones going into symbol tables. Except when mandated by an explicit requirement, under no circumstances should profanity appear in a program's output, documentation or configuration files, or be a required part of its input.
Clean up
Bits of code that can't possibly serve a purpose are a sign of neglect, scaring some readers and depressing others. Delete.
Semi-commented-out stuff spread around in moments of panic can be hard to clean up during the bad mood that breeds it. But that bad mood will haunt you as long as you keep bumping into that stuff. So.
Deviations
Concise nested statements or data object specifications can take one line and so need no indentation: if(error) quit.
Indenting upon nesting is neither mandated nor recommended in languages and formats where it is not customary. For example, code following a branch instruction or the <html> opening tag.
It's nice when generated code is indented, but sometimes the white space uses up bandwidth and sometimes you get fed up with propagating the nesting level through the code generator. Generated code is mostly read by whoever wrote the generator so it's up to you.
For profanity to appear in a program's interaction with the user, no explicit requirement is needed when it is a common practice in the given branch of the industry.
Some code won't be very tidy when you're really in a hurry – so be it.
Conclusion
Adopting the Iron Fist Coding Standard will give you the benefits of a mature, field-tested system of coding guidelines while saving the cost of creating one from scratch.
123 comments ↓
I think the whole indenting – space issue is just a red herring, and a smelly one at that.
If you want to inforce a tab or indenting between editors and different IDE's you're better off to get the IDE to insert spaces instead of tabs.
Though some people may cry out because of this, but if you have to read a web page about the advantages or reason for tab/space and alignment just stick with spaces instead it just wastes a huge amount of time.
The whole tab->Indent goes back to the old printer days where the tab was used as a special character to move the printer head a set margin on the page. Of course enginners dont talk to other enginners or there is not a standards body design for this stuff. So we have a situation where indent now is 3 spaces vs 4 spaces and the whole mess that comes with it.
Use space or automatically insert a space when you press [tab] and be done with it. It makes copy and pasting code between projects easier and to a point the physical screen representation is what will be printed in a email or printed on paper.
Done.
More to the point (Rant ahead)
I think a lot of these problems come from the old technologies that will forsaken be with us to our dieing days.
For example, the wonderful world of a new line.
In Win32, its Carriage Return + Line Feed
In Linux/Unix, its just Line Feed the carriage return is assumed to be there and removed because of redundancy.
In Apple, it was Carriage Return, then later on with the move to OSX it was converted over to Line Feed.
Then we have the programming languages.
In C could run on any operating system, the designers got the C Runtime to output the necessary New Line Character hex values for that format.
So, they took the 'Line Feed' hex value 'n' and renamed it in the C language 'New Line Character', in which case depending on the C run-time or where the hell it was compiled it would convert the 'new line character' to the co-responding 'new line' characters for that platform eg.. PC,OSX,Linux
Where Am I going with this. Well, its just a complicated mess of old conventions that was used in the old days of electronic type writers and major IBM printers that used some 'insert' convention that due to market forces became adopted into technology. Such you have the same *&&* situation with Tab vs Spaces all freaken over again. It wastes a HUGE mount of time, where it DOES not matter.
Just use, auto insert Tabs to Spaces and have a company wide policy how alignment should work using spaces within the organization. It means no matter what text editor you open it up in, the formatting of the text will be the same, where if you use (Tab) you will be burnt for all eternity of time.
Tabs may be fine too. Even a mix of tabs and spaces may be fine when done wisely: see http://www.emacswiki.org/emacs/SmartTabs . YosefK, great as usual, took care to include the only sensible requirement for indentation and *not* restrict it to some drivel like "Tabs considered harmful", "Spaces are WRONG" and the like.
All three variants (tabs, spaces, both with SmartTabs) are just great. Spaces look the same way everywhere, and it's great. Tabs cater to preferred indentation width of each reader, and it's great in another way. Smart tabs separate indentation and alignment logically, allowing non-block alignment AND flexible indentation settings at the same time. For each of these things to work, IDE/editor used by each developer has to support it, or indentation will sometimes break. There is nothing more "automatic and simple" about expanding tabs than about converting tabs to spaces or indenting with smart tabs. When a lot of different IDEs are in use (or someone just HAS to use an IDE with a dumb editor for its unique features), spaces-only may be the "lowest common multiple" supported everywhere; "spaces and no tabs" is the right thing in this environment, but not necessary The Only Right Thing Forever.
As of pseudo-commented-out dead pieces of code and the like: unfortunately, novices used to start programming without version control and discover it only after some years of managing to get away without it. VCSless experience makes them paranoid about irreversible code changes, and it takes some conscious effort to get over it. It seems that version control is more common / more mainstream these days (even non-programmers track interesting projects with VCS), so I hope that future programmers won't be afraid of code deletion from the very start.
Actually I prefer tab expansion as recommended in the page I linked to but bug someone with an iron fist to expand their beloved tabs when everything looks right everywhere it should I would not.
Commented out code sometimes serves as a useful comment ("or we could do it that way, see, in which case…" or "if you want to compute this also, which we now don't need, this is how you do it") so you may want to keep it regardless of revision control, but then there are those dirty little snippets that could never be useful if commented back in.
The reason I'd do
if (error)
quit();
instead of
if (error) quit();
is one can more easily put a breakpoint on the line with the quit() and this conforms to how a breakpoint is put for multiline conditionals.
So would I. Wouldn't mandate it though.
And JWZ is right with his space solution within his solution space. The problem is that there is a kind of tab-lovers he've never listened to (maybe he never run into them?). I'm not of this kind, but I've heard and tried to understand what they propose — and it contradicts to JWZ's `what people care about' list, vehemently.
This kind of tab lovers are after *stopping* fighting wars over 2/4/6/8 indentation by making indentation logical and allowing each developer to use his own preference simultaneously. The idea that some madman *wants* text to look different for different readers eludes space-lovers completely.
Using JWZs "religious" metaphor, w.r.t logical tabs, he looks like a medieval monk never running into a proponent of secular state. "Look, we don't have to fight religious wars anymore, we prefer peace and the rule of law all over the place (oops)." The monk could say something like this: "what people care about is whether they get Shariath or Torah or Biblical law enforced. Then go forth and disestablish secular state, before deciding real issues" …I'd probably side with the monk, after all, (and with JWZ on spaces), *but* I expect the secular tab-lover to have some objections :)
There's another consideration: the ability to skip over an indentation level/delete one indentation level with a single keystroke – free in most editors if an indentation level is implemented as a single hard Tab character. We're talking deep stuff.
I had the pleasure of writing code with a guy who not only commented out useless bits of code and left them there but also decided it's a good idea to indent them one step BACKWARDS…
I also think that a good practice is to limit nesting statements in general and [in-function] logic branches specifically.
How do you feel about color schemes? :P
I saw more than one otherwise great programmer who didn't indent upon nesting – hence the explicit rule.
Limiting nesting statements is generally good just like limiting the amount of calories per meal or something – never felt the need to mandate anything here though.
Color schemes – like syntax highlighting? Feel great about those, colors are scientifically proven to cheer people up or so I heard – obviously never felt the need to mandate anything here though.
With coding guidelines, less is more.
A tidbit about deviations:
When measuring test code coverage single line statement
if(x) panic();
vs
if(x) {
panic();
}
will improve the numbers without sacrificing quality of the test
This reminds me why I don't particularly like code coverage measurements.
* respect what has come before
* be respectable
Joel Spolsky said something like that, IIRC.
The profanity link did not explain why "it is easier to write", but it sure is funny.
Profanity was shown to increase the ability of subjects to sustain pain. The link shows, mostly, pieces of code that were clearly painful to write, and profanity in the comments and identifiers, most likely, made writing it easier.
Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that I
get in fact enjoyed account your blog posts. Any way I'll be subscribing to
your augment and even I achievement you access consistently rapidly.
Respect to website author , some wonderful entropy.
Cheers, great stuff, I enjoying.
I am glad to be one of the visitors on this great website (:, appreciate it for posting .
I'm pleased with the way that yosefk.com deals with this kind of topic! Generally to the point, sometimes polemic, without fail well-written and also thought-provoking.
Good Morning, bing lead me here, keep up great work.
I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.
I must say got into this website. I found it to be interesting and loaded with unique points of view.
Cheers, great stuff, Me enjoying.
I am glad to be one of the visitors on this great website (:, appreciate it for posting .
I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.
Good Morning, happy that i saw on this in yahoo. Thanks!
very cool post, i actually enjoyed this web site, carry on it
Deference to op , some superb selective information .
Enjoyed reading through this, very good stuff, thankyou .
Morning, here from baidu, i enjoyng this, I come back soon.
Intresting, will come back here once in a while.
Enjoyed examining this, very good stuff, thanks .
This is awesome!
Enjoyed reading through this, very good stuff, thankyou .
stays on topic and states valid points. Thank you.
Respect to website author , some wonderful entropy.
Found this on bing and I’m happy I did. Well written article.
I like this site because so much useful stuff on here : D.
I like this website its a master peace ! Glad I found this on google .
Enjoyed examining this, very good stuff, thanks .
Enjoyed reading through this, very good stuff, thankyou .
I like this site because so much useful stuff on here : D.
Thanks for sharing your thoughts about how to get help in windows 10.
Regards
Intresting, will come back here later too.
At this time it appears like Movable Type is the preferred blogging platform available right now.
(from what I've read) Is that what you are using on your blog?
google took me here. Cheers!
Great article to check out, glad that bing took me here, Keep Up awsome job
First off I would like to say fantastic blog! I had a quick question that I'd like
to ask if you do not mind. I was curious to find out how you center
yourself and clear your head prior to writing.
I've had trouble clearing my mind in getting my ideas out.
I do take pleasure in writing but it just seems like the
first 10 to 15 minutes are usually lost simply just trying to figure out how to
begin. Any recommendations or tips? Many thanks!
You got yourself a new rader.
This is a topic which is near to my heart… Best wishes!
Where are your contact details though?
Enjoyed examining this, very good stuff, thanks .
Nice post. I learn something totally new and challenging on sites I stumbleupon on a daily basis.
It's always exciting to read through articles from other authors and use something from other sites.
I love reading through and I believe this website got some genuinely utilitarian stuff on it! .
Great, bing took me stright here. thanks btw for this. Cheers!
This does interest me
I really enjoy examining on this internet site , it has got good goodies .
Hello there! Do you know if they make any plugins to help
with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not
seeing very good gains. If you know of any please
share. Cheers!
Very interesting points you have remarked, appreciate it for putting up.
Greate post. Keep writing such kind of information on your page.
Im really impressed by it.
Hello there, You've performed a fantastic job. I will certainly digg it and individually recommend
to my friends. I am confident they'll be benefited from
this web site.
I constantly spent my half an hour to read this web site's articles or reviews every day along
with a mug of coffee.
I simply had to say thanks yet again. I do not know the things I would have made to happen in the absence of those ways contributed by you relating to such a theme. It previously was a real terrifying circumstance in my circumstances, but noticing your specialized fashion you processed the issue forced me to jump with gladness. I’m happy for your information and then believe you comprehend what a powerful job that you’re getting into educating some other people through the use of a web site. I know that you have never come across any of us.
This is the perfect website for everyone who wishes to understand this topic.
You realize a whole lot its almost hard to argue with you (not that I personally will need to…HaHa).
You definitely put a fresh spin on a subject that's been discussed for
a long time. Wonderful stuff, just great!
I have been browsing online greater than three hours as of late,
but I by no means found any attention-grabbing article like yours.
It is lovely worth sufficient for me. In my view, if all website
owners and bloggers made good content as you probably
did, the internet will probably be much more useful than ever before.
Like the website– very user-friendly and lots to see!
Wow, this post is pleasant, my younger sister is analyzing
these things, so I am going to inform her.
With every new installment, existing characters are even tweaked and refined.
It is the first compilation of Mario games on the Wii U and includes New Super Mario Bros.
These are actually fantastic ideas in concerning blogging. You have touched some fastidious things here.
Any way keep up wrinting.
Hi, I do think this is an excellent blog.
I stumbledupon it ;) I am going to come back yet again since i have saved as a favorite it.
Money and freedom is the best way to change, may you be rich and continue to guide other people.
cheap viagra paypal payment
4. Once a player is done placing his tiles
on the scrabble board, his score is then calculated as described in the previous section. He
is then allowed to replace the tiles he has played.
Adding one or more tiles at the beginning of a word to form a new word.
Adding tiles at a perpendicular angle to an already played word, using one of the tiles
of the said word. Placing the tiles parallel to an already
played word; where the tiles of the new word touch the tiles of the old word, other new words should be formed.
Putting a single tile to an already played word. 6. A player may challenge his opponent's
word before the scores have been tallied. This is the only
time that the dictionary may be opened. If the challenged word is proven unacceptable, the
challenged player takes back his tiles and loses his turn. A scrabble game ends when the
tile bag is empty and a player places his last tile on the board.
It also ends when all players have passed in two consecutive turns.
At this point, all scores are tallied. If a player has unused
tiles at the end of the game, the value of his tiles are subtracted
from his total score. The player who first used up all his tiles gets the value
of all the unused tiles of his opponents added to his own score.
The person who gets the highest score wins the board game.
I am sure this piece of writing has touched all the internet
visitors, its really really nice piece of writing on building up new
web site.
only here viagra pharmacy
I simply must tell you that you have an excellent and unique site that I must say enjoyed reading.
Hello friends, fastidious piece of writing and fastidious urging commented at this place,
I am actually enjoying by these.
I conceive this web site holds some real superb information for everyone : D.
I was looking at some of your articles on this site and I believe this internet site is really instructive! Keep on posting .
Intresting, will come back here later too.
This is nice!
What's up to every body, it's my first pay a visit of this weblog; this blog contains remarkable and truly excellent stuff for visitors.
You got yourself a new rader.
Intresting, will come back here more often.
I have interest in this, danke.
Thanks for this web. I definitely agree with what you are saying.
I like this website its a master peace ! Glad I found this on google .
You got yourself a new follower.
Awesome, this is what I was browsing for in yahoo
Hello, here from bing, me enjoyng this, i will come back soon.
Cheers, great stuff, Me enjoying.
We stumbled over here by a different web address and thought I
might as well check things out. I like what I see so i am
just following you. Look forward to looking into your web page yet again.
yahoo bring me here. Cheers!
Me like, will read more. Cheers!
Very interesting points you have remarked, appreciate it for putting up.
I am glad to be one of the visitors on this great website (:, appreciate it for posting .
stays on topic and states valid points. Thank you.
Respect to website author , some wonderful entropy.
Parasite backlink SEO works well :)
I was looking at some of your articles on this site and I believe this internet site is really instructive! Keep on posting .
On Line Viagra Super Active Hydrochlorothiazide 25mg Direct Order Wellbutrin Without Prescription [url=http://demalan.com]viagra[/url] Drug Stores That Carry Esomeprazole Original Cialis Online Bestellen Buy Prescription Worm Medication
I simply must tell you that you have an excellent and unique web that I really enjoyed reading.
Very interesting points you have remarked, appreciate it for putting up.
Yeah bookmaking this wasn’t a risky decision outstanding post! .
I am not rattling great with English but I get hold this really easygoing to read .
I like this page, useful stuff on here : D.
I like this site, useful stuff on here : D.
I was looking at some of your articles on this site and I believe this internet site is really instructive! Keep on posting .
I am glad to be one of the visitors on this great website (:, appreciate it for posting .
These are truly impressive ideas in about blogging.
You have touched some good things here. Any way keep up wrinting.
Gentamicin [url=http://leviprices.com]levitra 40 mga for sale mexico beach[/url] Cialis Sur Ebay Priligy Generico
What's up, its fastidious paragraph concerning media print, we all
be familiar with media is a wonderful source of
data.
Alex9, this drop is your next bit of info. Do transceive the agency at your convenience. No further information until next transmission. This is broadcast #4401. Do not delete.
Unquestionably believe that which you said. Your favourite justification appeared to be on the web the easiest thing to be aware
of. I say to you, I definitely get irked while other folks consider
issues that they plainly don't realize about. You managed to
hit the nail upon the highest and also defined out
the entire thing without having side-effects , folks can take
a signal. Will likely be back to get more. Thanks
I am really impressed with your writing skills
as well as with the layout on your blog. Is this
a paid theme or did you customize it yourself? Either way keep up the nice quality
writing, it's rare to see a great blog like this one today.
Hello there, just became alert to your blog through Google, and found that
it's really informative. I'm gonna watch out for brussels.
I'll be grateful if you continue this in future.
A lot of people will be benefited from your writing. Cheers!
I like this site, some useful stuff on here : D.
I need to to thank you for this good read!! I certainly
loved every bit of it. I have you book-marked to look at new things you post…
plenty of fish natalielise
Next Day Express Delivery For Viagra Buy Viagra Online In Usa Online Pharmacxy [url=http://purchasecial.com]cheap cialis online[/url] Viagra In Canada Without Prescription buy accutane 20mg Priligy 60 Mg Prix
Great goods from you, man. I've understand your stuff previous to and
you're just too wonderful. I actually like what you've acquired here, certainly like what you are saying and the way in which you
say it. You make it enjoyable and you still take care of to keep it wise.
I can't wait to read much more from you. This is actually a terrific website.
I conceive this web site holds some real superb information for everyone : D.
Hi to every one, the contents present at this site are actually amazing for people
experience, well, keep up the good work fellows.
Deference to op , some superb selective information .
very nice post, i actually love this web site, carry on it
I conceive this web site holds some real superb information for everyone : D.