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

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments

Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.