Elements of Programming Style
With apologies to Strunk and White ...
Why Bother?
•A program is a sort of publication. It is meant to be read by the programmer, another programmer (perhaps yourself a few days, weeks or years later), and lastly a machine.The machine doesn't care how pretty the program is - if the program compiles, the machine's happy - but people do, andthey should.
--Rob Pike
This is a short list of notes from me on what should be the elements of well written programs
Typography
•Consistent Indentation
•Consistent use of braces
Comment the Code
•Prologue
•Epilogue
•Every Logical Block
Functions And Variables
•Names conveys Semantics, not data types
•Names should not be too short, or too long
•Initialize ALL variables to default values
Constants
•Never hardcode constants in the code
•Separate them out in a file or class
Single Return Statement
•Leads to more optimized code
•Avoids subtle bugs
•Easier to maintain code over time
Error Handling
•Use Meaningful Exceptions
•Stick with one type of error handling
Resource Management
•Use garbage collectible (GC) resources
•Finally block to dispose non-GC resources
Refactoring
•Continuously refactor your code
•Small is beautiful … i.e. length of function






Cool,
Keep up the good work,
Thanks for writing, most people don't bother.
Reply to this