Tuesday, December 19, 2006

Good Design and Coding


Understanding is as important as coding. Spend as much time thinking about your design as you do coding it. Never code around a design flaw. If you get stuck, always go back to the drawing board. But then follow through with your conclusions.
Flow and design:  Code is more than a series of statements. It is also a relationship of parts. Stepping through your code with a debugger is not how to understand what it does. You must understand the design as well. Don’t think like a debugger, or your code will be incomplete. Think abstractly as well as concretely.
Together belongs together; separate belongs apart. Good Design = High Cohesion + Loose Coupling. Parts that work closely together, keep close to each other. Parts that work separately, keep apart. The bad thing about global data is that it couples code in one part of the system to code in another part. Passing around a massive structure does the same thing. Even passing around a reference to a single variable does the same thing.  
Only relevant coding standards are effective. Indentation, brace style, spacing, blank lines, these all communicate information. Use a consistent style that tells future programmers how the design works.
Maintaining unused code is more expensive than rewriting it. You need to maintain unused code, modify it, tweak it. But you cannot test it, because it is unused. You cannot know whether unused code still actually works. This is an unnecessary burden. Delete unused code, even if you may need to use it again someday. You can always write it again if you need to.
Comments tell why, not what or how. Add comments to explain why the code does what it does. Never add a comment to describe what the code does or how it does it. Instead, use descriptive function and variable names. If you need a comment to tell what or how, your design is flawed.