Writing Flexible Code

Software engineering can become a monstrous task, no matter your end goal. In this world where things are changing at an increasingly accelerated rate, it is important to adjust and remain functional. In order to write code that will be easy to maintain, Bob Martin and Michael Feathers have created SOLID. An acronym that describes the five principles that will help developers create flexible code.

Single Responsibility Principle

The single responsibility principle tells us that every class should only have one responsibility. A responsibility is anything that is a task which can be accomplished in a single step. Sometimes, it can be difficulty to distinguish one responsibility from another, but doing so will give you many small classes and methods that are easy to understand and have a unique purpose.

Open/Closed Principle

The open/closed principles tells us that classes should be “open for extension, but closed for modification.” This drives the product to be more compartmental while being compatible. We should strive to write objects that are open to new functionality without needing modifications to the base class.

Liskov Substitution Principle

The Liskov substitution principle is in my mind one of the simpler ones to understand. It states that you should be able to replace a parent object with another object that inherits from that parent without any issues. A simple classic example is the Square < Rectangle class. If you have methods that require a rectangle object, that method should also be able to accept a square object without any problems.

Interface Segregation Principle

The interface segregation principle states that “no client should be forced to depend on a method it does not use. Not to worry too much about this, if you are using a dynamic language like Ruby.

Dependency Inversion Principle

Dependency Inversion Principle tells us that high level objects should not depend on low level objects. For example, if you have a house object that has doors and windows as objects. Changing doors and windows should not have any effect on the house object.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.