Developing Your Coding Skills
Measure code qualityConceptual Integrity
Loose coupling, high cohesion
Simplicity
Correctness
Readability, extensibility, flexibility, maintainability, reusability
Coding Principles
Consistency
Completeness
DRY(Don't repeat yourself)
KISS(Keep it simple, Stupid) principle
"You ain't gonna need it"
Single-Responsibility Principle (SRP)
A class should have only one reason to change.
Open/Closed Principle (OCP)
Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
If OCP is applied well, further changes of that kind are achieved by adding new code, not by changing old code that already works.
Figure out what may change, but resist premature abstraction.
Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types.
Subclass should not violate invariants defined in base class, subclass's preconditions should not be stronger than those of base class, and derived classes must conform to all the postconditions of the base.
A derivative that does less than its base is usually not substitutable for that base and therefore violates LSP.
Design by contract
Dependency-Inversion Principle (DIP)
A. High-level modules should not depend on low-level modules. Both should depend on abstractions.
B. Abstractions should not depend upon details. Details should depend upon abstractions.
Interface Segregation Principle (ISP)
Interface pollution
Clients should not be forced to depend on methods they do not use.
Separate Client groups mean separate interfaces
Practices
Actively Evaluate Trade-Offs - Make sure the investment will pay off
Code in Increments - Write code in short edit/build/test cycles.
KISS (Keep it simple, Stupid)
Develop the simplest solution that works. Incorporate patterns, principles, and technology only if you have a compelling reason to use them.
Substitute by Contract
Use inheritance for is-a; use delegation for has-a or uses-a
Composition over inheritance
Don't swallow exceptions, report them.
Logging
Logging is vital to help to analyze problems, design your log mechanism, message need to log, rotation mechanism, record useful/consistent log.
Provide Useful Error Messages
Sensitive to Code Smell
A code smell is a hint that something has gone wrong somewhere in your code.
Common code smells
Duplicated code, Long method/class, excessively long identifiers,
Feature envy: a class that uses methods of another class excessively.
Inappropriate intimacy: a class that has dependencies on implementation details of another class.
Refused bequest: a class that overrides a method of a base class in such a way that the contract of the base class is not honored by the derived class.
Lazy class / Freeloader: a class that does too little.
Contrived complexity: forced usage of overly complicated design patterns where simpler design would suffice.
Beware of Anti-patterns
Methodological anti-patterns
Copy and paste, Golden hammer/Silver bullet, Reinventing the wheel, Premature optimization,
Programming by permutation/accident: Trying to approach a solution by successively modifying the code to see if it works
Improbability factor: Assuming that it is improbable that a known error will occur
Object-oriented design anti-patterns
BaseBean, Call super, Circle-ellipse(Square-Circle) problem, Circular dependency, Constant interface, God object,Object cesspool,Object orgy, Sequential coupling.
Yo-yo problem: A structure (e.g., of inheritance) that is hard to understand due to excessive fragmentation
Programming anti-patterns
Accidental complexity,Action at a distance, Blind faith, Boat anchor,Busy spin/wait,Caching failure,Cargo cult programming,Coding by exception,Error hiding,Hard code, Loop-switch sequence, Magic numbers, Magic strings,Paramesh,Soft code,Spaghetti code
Lava flow: Retaining undesirable (redundant or low-quality) code because removing it is too expensive or has unpredictable consequences
Design Smell
Rigidity - difficult to change.
Fragility - easy to break.
Immobility - difficult to reuse.
Viscosity - difficult to do the right thing.
Needless complexity - Overdesign.
Needless repetition - Mouse abuse.
Opacity - Disorganized expression.
Resources
A Taxonomy for "Bad Code Smells"
http://en.wikipedia.org/wiki/Code_smell
Practices of an Agile Developer: Working in the Real World
Agile Software Development, Principles, Patterns, and Practices
How To Improve Programming Skills