Object-Oriented Programming
Writing clean code is always been a difficult task and that's because many of us lack the knowledge of object-oriented programing.
In this article lets discuss how can we write some clean code by applying OOP in our code, so let's get started.
The following are some steps to be followed when we go about writing code.
Let's take them one by one.
Identifying Classes :
We need to first identify business entities from the requirements / Spec, this is important because these are going to become your classes and their behaviors.
NOUNS - Will be your classes
VERBS - Will be their behaviors
These not going to be straight forward hence be careful, these are also going to help to identify your properties of the classes.
Separating responsibilities :
This is a step where most of the developer's miss to align with, this is a very important step, a class shouldn't be doing too many things, this maximizes coupling ( interdependency ) and reduces cohesion.
We should be aiming to minimize coupling and maximize cohesion, this is going to make our code lean, relevant and unit testable.
Establishing Relationships :
Once we complete separating responsibilities we will be left with classes yet relationships yet to be established, we shall establish the connections in this step.
Classes may be connected with different types of relationships like Uses a, Has a and Is a
Uses a: This a collaboration relationship in which a class collaborated with another class does some computation.
Has a: This is a Composition relationship in which a property of the class is made up of another class.
Is a: This is a relationship which is made up of ( Inheritance ) where a child class is a subset of the main parent class.
Leveraging reuse :
One important quality of a good Object Oriented code is code reusability, code has to crafted by taking these into consideration.
We need to identify if there are common codes and consider moving those into a commonplace like a utility class.
We need to check if there are any Is a relationship ( Inheritance ), In this, we can move the commonly shared code to a parent class and make the child classes inherit from it, thus leading to code reusability.
Happy coding !!
Bro this is just awesome. Put in simple words. Superb bro. Keep going. Keep doing good. Awesome.