Wednesday, August 3, 2016

Design principle II

Coding is almost impossible to get it right the first time. Software is never written, it is always rewritten.

How to evaluate quality of design
first step is to let go of the ego. be unemotional
review design and code

KISS - keep it simple and stupid 
If a developer implements a complex logic by some simple code, he is good developer.
Complexity makes code hard to be changed. 
Simple keeps your focused.
Simple solves only real problem we know about. For the problems we don't know yet, don't have design/code on that. (overdesign, YAGNI, you aren't gonna need it, which means Always implement things when you actually need them, never when you just foresee that you need them.)
Simple fails less.
Simple is easy to understand
We know that the code developing has many iterations, refactoring, change requirement, etc. If in the first iteration the code is already complicated, then after second iteration it will be more complicated, then the third, more complicated. So we should keep our code as simple as possible in each iteration, which gives some space in next iteration or future change.
A good design is to chop the complex logic into several small pieces and each of them are simple. 

High cohesion and low coupling
cohesion is a piece code narrow focus on one thing or likely things and does it very well (Single responsibility).
Likely things stay together and unlikely things are away from each other.
for example, a module only deals with authorization, spring security. Or a module only deals with database access, DAO layer.

Coupling is dependency. We should keep dependency as less as possible.
Normally, depending on a class is tight coupling and depending on an interface is loose coupling. This is "D" of SOLID principle. Dependency inversion principle: one should Depend upon Abstractions. Do not depend upon concretions.

A good design is to have high cohesion and low coupling.

Inheritance increases coupling.

coding is a developer talking to machine. It should be meaningful, neat, easy to understand.

Here is my old blog about design principle.

No comments:

Post a Comment