We have discussed basic rules of writing code in the previous post. What else could a developer do to increase her performance?
She should learn basics of good object-oriented design! My observation is that many developers don’t believe that all those rules of object-oriented design (OOD) do matter. They apparently think that the rules were invented by various pundits to sell their books or teaching services on software development methodology.
In fact, the whole object-oriented design was invented to improve developers performance. I’ll show this in a minute.
Let’s suppose you follow the six rules I described in the previous post. What other factors inhibit software developer performance? Those are:
The more complex design is the more details you have to keep in mind which increases probability of making mistakes. For example, introducing a bug while using API that consists of just 3 methods is much less likely than when working with API consisting of 3000 methods, right?
If a component you work with is tightly coupled with 10 other components, you have to keep in mind details of all 11 components, right? This again increases probability of making a mistake.
Code duplication is like slow poison. Initially it is so simple to cut and paste a piece of code. But later you have to:
- Remember that the duplications exist
- Each time you change one instance, you need to find, to change and to test all other occurrences of the code fragment
- Those duplicated fragments gradually diverge as time goes by and at some moment you will find that a change that works correctly within one instance of the once duplicated code breaks another instance
All this multiplies time that a developer needs to develop and to maintain a code.
Object-oriented design is a way to address tight coupling and code duplication:
- Inheritance allows reusing a code without duplicating the code we want to reuse
- Polymorphism allows modifying a reused code in a clean way
- Encapsulation of data reduces coupling
- Good object-oriented design reduces complexity and makes the code easier to work with
So that to understand how good object-oriented design helps to improve developer’s performance, let’s take a look at e.g. a set of OOD principles known as SOLID:
- Single responsibility principle (SRP), the notion that an object should have only a single responsibility.
- Open/closed principle (OCP), the notion that software entities should be open for extension, but closed for modification
- Liskov substitution principle (LSP), the notion that objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program
- Interface segregation principle (ISP), the notion that many client specific interfaces are better than one general purpose interface
- Dependency inversion principle (DIP), the notion that one should depend upon abstractions; do not depend upon concretions
SRP suggests that an object must do just one single thing. It is simpler to use an object that represents just Car and nothing else than an object that represents Car and Coffee Grinder simultaneously, right?
OCP principle suggests that if you want to modify behavior of a class, instead of modifying the class itself better create its subclass which will implement the desired modified behavior. Reason for this is simple. If the class already exists, there may be a part of the code which expects it to behave in particular way. If you want to modify the behavior, you have to find all such places, understand how they will be affected, modify the properly, test modifications… Creating the subclass and using it where needed is much simpler and requires much less work.
LSP is just a way to verify if particular inheritance is correct. You would not make Car class a subclass of Road class, right? You feel that something is wrong with such inheritance and LSP explains what is wrong (Car can’t be used in cases when we need Road, e.g. one can’t put concrete tarmac on Car, right?). Wrong inheritance makes code much more difficult to understand and to use.
ISP just says that that several smaller classes are easier to work with than one big one.
DIP helps to improve developer’s performance in two ways. First of all it reduces coupling, as explained here. Second, the code designed with dependency inversion in mind is much easier to unit test, which is a huge advantage.
Overall, well designed code is much easier to develop and maintain.