SOLID refresher

Single Responsibility

This principle says a module should have precisely one reason to change.

Encapsulate sources of high uncertainty first. If it’s highly likely to change, make sure it can change separately from other things.

Open Closed

My previous post goes into more detail on this principle. To summarise:

  • Don’t get carried away with this one and use it as an excuse to implement things “in case they’re needed in the future”.
  • Do take a few moments to consider what might change in future, and shape the thing you’re about to implement to prepare for it

Liskov Substitution

“Don’t lie with inheritance”.

I’d generalize to:

Reluctance to lie should be proportional to the difficulty of cleaning up that lie when its burden is too great.

Interface Segregation

From Wikipedia: “no client should be forced to depend on methods it does not use.[1]

The lower down in your dependency tree the interface is, the more important it is to segregate it.

Dependency Inversion

Depend on abstractions (interfaces) rather than concretions.

Don’t hide your dependencies, use constructor injection of the interfaces your object needs. The topic of Dependency Injection frameworks often comes under this heading, but probably deserves a separate post. If you do use one, make sure that only the very highest layer of your application knows about it.


About GrahamTheCoder

I'm a C# developer based in Cambridge hoping blogging will achieve some or all of the following. Help me organise my thoughts. Practice writing things other than code. Give me a place to refer people to when I'm trying to make a long winded point. I welcome comments and constructive criticism, and hope to look back at my written opinions in the future and laugh at my own naivety.
Aside | This entry was posted in Principles and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s