The OOP Concept

Object-oriented programming (OOP) is a way of writing code based on concept of objects. Every object has its own arguments and functions.

If there is a need to code with OOP principles then we have to be in a continuous effort trying to describe everything into objects.

Before we continue on to how we have to think when we develop with OOP I would like to give you a very basic intro about some of the principles that we have to follow in order to build a better software which is maintainable and easy to improve with new functionalities.

It’s a good practice to separate the code into small blocks of code. Every single block of code must be responsible for one task only. To achieve this goal we can use functions.

A function is a block of code which executes a specific task. We have to keep in mind and not break the rule that a function must execute only one task. This is the way that the complexity of the code will be reduced making the code reusable. Also these small blocks of code mean fewer places for bugs to hide.

A function should not exceed 25 lines. It’s rare for a function to exceed 25 lines. We don’t need to count each line of function all the time. If a function exceeds the 1/2 of the screen it means that it may perform more than one tasks. So, we are able to break it in two or more functions.

If we don’t break the rule that a function must perform a single task then it’s almost certain that it will not exceed the top up limit of 25 lines.

The name of the function is an important fact as well. The name has to describe what the purpose of the function is with one, two ore more words. It such a great help for everyone to understand what a function does without the need to read a whole block of code.

I would like to continue to write about some good practices that everyone has to follow for a better quality of code. Even that, it would not be enough. The best way to learn more about that is by reading the book named Clean Code by Robert Cecil Martin.

Every developer must read this book. Before reading anything else. If you have not read the book then stop what you are doing and go for it. It’s the best book in order to set the right bases about programming.

After this short analysis on how to improve your code quality, this is the right time to talk about OOP. As I said, we have to reduce the feature that we want to build in order to describe it as an object.

In order to do that, we have to think that an object is an entity or a class. Every class contains attributes and functions.

So we have to describe and build those attributes and functions. Every function has to perform only one action of the object. Functions are able to handle these attributes and change them sometimes. Let me give you an example.

Supposing that we want to build a game with people and dogs.

A dog is an entity which has 4 legs. This is an attribute. Also the dog can execute tasks like barking, walking or running. These tasks are functions. And each function performs only one of these tasks. Thus, we can make a dog.

Now we want to make man. A man is an entity again with 2 hands and 2 legs. These are attributes as well. Some of his functions are to walk, run and talk. As before, each of the tasks that a man can perform is a function in our class.

This way of thinking seems to be easy at first but in case that we have a complex problem to solve then code’s complexity is equally important. Even though, it’s certainly an efficient style of programming to build big applications.

There are many reasons why we want to use OOP.

It’s a really efficient way to build a reusable code. It’s certainly not so easy. We have to follow some rules like the rules that I described before. Alternatively, we will end up with a spaghetti code written in a class.

Another important reason is that, developers are able to collaborate with each other more efficiently without so much communication. That means that they don’t need to waste their time to describe and analyze the way which something was built or what is the way that a new feature will be built.

To be successful, we need to use design patterns. Design patterns offer solutions to problems during the development. It’s a framework that helps developers solve common problems.

Moreover, design patterns are able to solve communication problems among developers. If design patterns are known among developers team, productivity and effectiveness are increased.

Writing code with design patterns it’s not so easy and requires a lot of practice and study. One of the best books is Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Ralph Johnson, Richard Helm, John Vlissides.

All of the above come true through some principles. These principles are encapsulation, inheritance and polymorphism. According to this book these principles are :

ENCAPSULATION

The result of hiding a representation and implementation in an object. The representation is not visible and cannot be accessed directly from outside the object. Operations are the only way to access and modify an object’s representation.

In other words :

Encapsulation is the process of combining data and functions into a class. Attributes of the class are kept private and there are public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.

INHERITANCE

A relationship that defines one entity in terms of another. Class inheritance defines a new class in terms of one or more parent classes.Thenew class inherits its interface and implementation from its parents. The new class is called a subclass or (in C++) a derived class. Class inheritance combines interface inheritance and implementation inheritance. Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.

POLYMORPHISM

The ability to substitute objects of matching interface for one another at run-time.

This was a quick review of what OOP is. It is really an immense subject, and we only just scratched the surface of it.

I will get back to the subject soon with more details when I present design patterns.