piątek, 15 lipca 2011

Stay SOLID!

From time to time, in a break between fierce battles with code, requirements and time each of us starts asking questions more sophisticated than "how to write another freakin' routine". One of the most frequently repeated is how to check if a OO code is really well designed (and later implemented).
Let's forget for now about the "big" architecture and concentrate on the "small" one - single components/packages. Some advices and concepts (as DRY or GRASP) can be found here and there - for example in excellent book "Effective Java" by Josh Bloch. Additionally, tons of useful yet short articles are published by recognized authors as Martin Fowler or Robert "Uncle Bob" Martin.
The latter one created short, concise list of Object Oriented Design principles under the clever name - SOLID. In short, those are as follows:
  • S(RP) - Single responsibility principle - "an object should have only a single reason to change".
  • O(CP) - Open/closed principle - “ability to extend class behaviour without modifying it”.
  • L(SP) - Liskov substitution principle - "subtypes must be substitutable for their base classes, without modifying program's correctness".
  • I(SP) - Interface segregation principle - "create fine grained interfaces for specific purposes".
  • D(IP) - Dependency inversion principle - "depend on abstractions, not on concretions"
SOLID is few years old, but still managed to gain some attention, so web is filled with articles discussing all principles in details. One particularly interesting resource is old Uncle Bob blog post here. Also, Lost Techies has an article filled with Java examples.

Looking from experience's point of view, after some years of code crunching SOLID principles come intuitively. SRP, OCP are basic concepts understood quite early. DIP is adopted swiftly after meeting DI containers. LSP looks a bit more complex, but is in fact substantial when dealing with complex models with inheritance.
All in all, it's good to keep SOLID as a reference. Reviews can only benefit from using such simple yet useful checklist :-)