Code Advice "Column"
By Tor Norbye on Sep 03, 2005
I've discussed coding style a couple of times before in this blog, and it invariably generates a lot of interest and discussion. It seems I'm not the only one who cares a lot about these issues.
I recently tried to explain the coding style I've used in the Creator designer source base, to somebody else on the team. When I tried to provide references I came up short. There are several coding style guidelines - and I tend to follow the JDK one - but the problem with the official coding style document is that it leaves out a lot! For example, it does not address the Tabs versus Spaces issue (cowards!). I found some other good ones, but I disagree vehemently with some of their rules (like open braces on a separate line, and underscore suffixes and fields, and indentation level 2).
So, I intend to start blogging my opinions on how to write good code. Some will be controversial - especially my first "rule" regarding logging! Note that no rule is absolute - there are always exceptions and you need go consider the tradeoffs and apply good judgement.
I also shouldn't take "full credit". I have not invented most of these practices. Some I have learned from developers whose style I admire. Others have come about as the result of (sometimes heated) discussions with other programmers. And finally, some have I have learned from my own past mistakes.
Take everything with a grain of salt - but I hope you will find these entries, at least some of them, helpful.
Update 10/15/06: Here are the entries as of today:
- #1: Don't log, debug
- #2: No Vanity Tokens
- #3: No Tabs! Ever!
- #4: Don't capitalize acronyms
- #5: Debugging with Undo
- #6: Write debuggable code
- #7: Capitalize your enum constants
- #8: Reformat your source code!
- #9: Avoid null - use objects
- #10: Place brackets with the declaration type, not the name
- #11: Initialize fields using the property name
- #12: Use final on constructor and setter parameters
- #13: Don't spot-fix null pointer exceptions
- #14: Don't initialize fields to default values
- #15: Name getters properly!
- #16: Don't Encode Symbol Type in Variable Names!