LSP - Liskov Substitution Principle

Onward to a new principle!

How to create the best inheritance hierarchies? What traps will cause us to create hierarchies that don't conform to OCP? LSP to the rescue :)



LSP is a tough mf!

LSP - Liskov Substitution Principle

Subtypes must be substitutable for their base types.

So... is a Square a Rectangle?

You have been warned!

Rectangle has width and height. Of course in a Square both are equal. If we try to enforce this in Square we are violating Rectangle contract.
Inheritance represents a IS-A relationship between objects. However, this relationship pertains to behaviour so a Square doesn't behave as a Rectangle and, effectively, a Square isn't a Rectangle.
Don't try this at home.



LSP is one of the prime enablers of OCP. The contract of the base type has to be well understood, if not explicitly enforced by the code (TDD can also helps). 

Yes master :)

The term IS-A is too broad to act as a definition of a subtype. The true definition of a subtype is substitutability (does this word even exists? Yes), which is defined by either an explicit or implicit contract.

OCP - Open/Closed Principle

A new week, a new principle :) 

Ivar Jacobson said: "All systems change during their life cycles, so how can we create designs that are stable in the face of change and that will last longer than the first version?"
Nothing to fear Ivar
OCP to the rescue.

No, not this OCP


OCP - Open/Closed Principle

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
When a single change to a program results in a cascade of changes to dependent modules, the design smells of rigidity. If OCP is well applied, further changes of that kind are achieved by adding
new code, not by changing old code that already works.

By using abstractions we can achieve open for extension (behaviour of a module can be extended) and closed for modification (extending the behaviour does not results in changes to the rest of the code).




In old procedure style languages if we wanted to implement different shapes we would have some kind of struct or enum. And then we would repeat a switch statement or several ifs all across the application. With each new shape we would have to search for all places that must be changed.


With OCP in and objected oriented language this is converted to:
Izzz nice :D


But how to know which changes are likely? We do appropriate research, ask questions, and use our experience and common sense. After all that, we WAIT until the changes happen!

Common sense... fail

Fool me once... First we should write or code expecting it not to change. When it does (and it will) we implement the abstractions that protect us from changes of that king. In short we take
the first bullet and then make sure we are protected from any more bullets coming from that particular gun :)


Man you're not Robocop!

Using OCP will result in increased flexibility, re-usability and maintainability. However be warned: Resisting premature abstraction is as important as abstraction itself.

And this guy has a special set of skills...

SRP - Single Responsibility Principle

So let's begin our quest for hard SOLID as a rock code!

No, not The Rock

SRP - Single Responsibility Principle has a simple definition:


A class should have only one reason to change.

If a class has more than one responsibility, they become coupled. But what is a responsibility? 



Always grumpy


A responsibility is just a reason to change.

Through here we extract a corollary: 


An axis of change is an axis of change only if the changes occur.

Separate everything just "because yes" would smell of needless complexity.


Let's see an example:

Mixed responsibilities

Employee class has two responsibilities: business rules and persistence control. Business rules tend to change frequently, and although persistence may not change as frequently, it changes for completely different reasons.

Responsibilities separated using DAO Pattern
Class Employee becomes decoupled of persistence, using DAO Pattern. Fortunately the practice of TDD will usually force these two to be separated.

So remember, always separate class responsibilities... when needed... maybe?




P.S: From the man himself






PPP - Section II. Agile Design

Let'ss continue our adventure through PPP Section II. Agile Design


If we build software in tiny increments, when do we design?

Wax on, wax off Daniel-San

Exactly, step by step. In each iteration, and according to the new requirements, design and system architecture should evolve just enough to fulfill all needs.

So when to improve our design? Simple... when it smells.

 
The stench...


You know that the software is rotting when it starts to exhibit any of the following odors:

  • Rigidity: a single change causes a cascade of subsequent changes in dependent modules.


  • Fragility: a tendency of a program to break in many places when a single change is made.


  • Immobility: it contains parts that could be useful in other systems, but the effort and risk involved with separating those parts from the original system are too great.


  • Viscosity: it is difficult to do the right thing.


  • Needless complexity: Overdesign, it contains elements that aren't currently useful (YAGNI).


  • Needless Repetition: too much "Cut and paste".


  • Opacity: a module difficult to understand.



So why software rots? We know that not only requirements will change (that's a fact) many times they are the most volatile elements on the project.

And how can we stop it? Well by using good old SOLID Principles! Each will have its own post :) Principles will help us eliminate these bad smells. however they aren't a perfume to be scattered all over the system!

As professional software developers we should keep design as good as it can be (and the code as clean as possible). 


Let the healing cleaning begin!
Choose your weapon!