Introduction
This will be the first of a series of blogs covering the SOLID principles which were created by the legend that is Robert C. Martin.The SOLID principles concern the design of object-oriented programming in initial design and in code maintenance. These five principles are the golden checklist that a developer should become finely tuned with if they wish to write super maintainable code.
I strongly recommend reading this book, if you wish to dive more into these principles:
'Agile, Principles, Patterns and Practices in C#' by Robert C. Martin
You can get it here:
http://www.amazon.co.uk/Principles-Patterns-Practices-Robert-Martin/dp/0131857258
(This link may not actually be the best price - so do your shopping!)
My goal of these blogs is to take what I have learnt from this book - summarize the principles whilst applying my own examples and thoughts. I intend for these blogs to be short and concise and any feedback would be greatly appreciated.
These blogs might be useful to you if you are:
- New to OOP and never heard of the SOLID principles before.
- Needing a quick reminder of these principles.
Single-Responsibility Principle (SRP)
This is by far one of the simpler principles to understand but can turn out to be one of the hardest to implement. This principle mainly concerns the design of classes.Each class should represent a single responsibility and concept. The reason for this is so that classes are:
- Easy to understand
- Changes can be made without breaking functionality.
For example, below show's an interface for a class:
1 2 3 4 5 6 7 8 9 10 11 12 | namespace Solid { public interface IPlayer { void Shoot(); void Reload(); void AddToInventory(); void RemoveFromInventory(); void Walk(); void DriveCar(); } } |
This interface provides a contract for a player to a game.
The good OO developers among you will quickly notice that this interface is bad.. very bad.
First of all - imagine what the implementation to this class would look like. I can imagine plenty of fields and many, many lines of code. This means the code will be unreadable and will therefore be hard to maintain.
Secondly, it is not clear what this class represents. You might suspect that it represents some sort of walking bag that is brandishing a semi automatic, that is also capable of driving a Ford Mustang.
Well, let me tell you it isn't. Well, it could be.. it is an interface after all.
But in the context of the class itself, it is doing too much for it to be clearly defined as just a player.
I can identify at least three areas of responsibility here:
- Gun
- Inventory
- Transport
1 2 3 4 5 6 7 8 | namespace Solid { interface IGun { void Shoot(); void Reload(); } } |
1 2 3 4 5 6 7 8 | namespace Solid { interface IInventory { void Add(); void Remove(); } } |
1 2 3 4 5 6 7 | namespace Solid { interface ITransport { void Travel(); } } |
There is no reason why the Person class should contain the implementation for how a gun shoots and reloads. The person's relationship with a gun should be abstract. This means the classes are shorter and easier to understand.
I bet Clint Eastwood didn't need to be concerned with gunpowder, springs and triggers when shooting a dozen villains. He just needed to pull the damn trigger!
By creating these separate areas of responsibility, it means that the functionality between these classes is not coupled. It will be harder to break code in other areas of responsibility if a change needs to occur. Also, if I need to change how the Inventory works, it mean's that I don't have to recompile Transport and Gun.
My reason for doing this is that I felt walking and driving a car is a method of transport and so I reached out for the abstraction. What this means is that I can now create two classes that implement this interface called Walking and Car that both have the Travel method, but provide their own implementations for travelling.
This becomes extremely powerful when Dependency Injection is involved which means I inject implementation into an object. More of this in later blogs!
By creating these separate areas of responsibility, it means that the functionality between these classes is not coupled. It will be harder to break code in other areas of responsibility if a change needs to occur. Also, if I need to change how the Inventory works, it mean's that I don't have to recompile Transport and Gun.
A little extra
You may have noticed that I have introduced ITransport with a Travel method, and that I have removed any mention of DriveCar and Walk.My reason for doing this is that I felt walking and driving a car is a method of transport and so I reached out for the abstraction. What this means is that I can now create two classes that implement this interface called Walking and Car that both have the Travel method, but provide their own implementations for travelling.
This becomes extremely powerful when Dependency Injection is involved which means I inject implementation into an object. More of this in later blogs!
But only if change is imminent!
SRP suggests that you should only split a class into separate responsibilities if these classes are likely to change.One of the main reasons for splitting a class is the risk of the coupled two responsibilities. If in the future, you make a change that you think affects one of the responsibilities, it may actually break the other responsibility without you realising.
If a class has been badly designed to have two responsibilities, but it is unlikely to ever change; then there may actually be no point in changing it.
Conclusion
The main point here is that a class should represent one thing - a single responsibility. There is no limit to your abstraction but there can be a risk of over doing it and making classes too segregated when they don't need to be. So do what makes sense and don't get too carried away.
In the book, it points out that you should only split classes up if they are likely to change in the future. I am in two minds over this as I would be tempted to split the classes for the sake of readability. Also, although classes may seem unlikely to change, there is no way of being certain of the future.
However, if you are working under a deadline and there is a lot of risk with changing a class when it is probable that it won't change - then perhaps there is reason to leave it alone.
In the book, it points out that you should only split classes up if they are likely to change in the future. I am in two minds over this as I would be tempted to split the classes for the sake of readability. Also, although classes may seem unlikely to change, there is no way of being certain of the future.
However, if you are working under a deadline and there is a lot of risk with changing a class when it is probable that it won't change - then perhaps there is reason to leave it alone.


