Code Quality
There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. - C.A.R. Hoare
A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away. - Antoine de Saint-Exupery
Coupling and Cohesion
Code Metrics
- Code Metrics (note: we will stick to metrics that relate to readability and maintenance rather than business or run-time efficiency metrics)
- Number of lines of code (per method, per class)
- Cyclomatic complexity - wikipedia
- Dependency graph example (originally from http://www.ndepend.com/SampleReports/OnNUnitNew/ComponentDependenciesDiagram.png)
Code Smells
Anti-patterns
Refactoring
- What is refactoring? - restructuring existing computer code without changing its external behavior
- Why Refactor?
- Refactoring improves the design of software -without refactoring, a design will "decay" as people make changes to a software system
- Refactoring makes software easier to understand - because structure is improved, duplicated code is eliminated, etc.
- Refactoring helps you find bugs - refactoring promotes a deep understanding of the code at hand, and this understanding aids the programmer in finding bugs and anticipating potential bugs
- Refactoring helps you program faster - because a good design enables progress
- When to refactor?
- Outcomes from tools that do static code analysis
- When you know the initial implementation was poorly planned or was rushed
- When your boss says so
- Choosing what to refactor and how to do it
- Files with poor metrics from code analysis
- Code smells
Technical Debt
Other thoughts and opinions on code quality, some with code samples