When to Choose Procedural Programming over OOP
by Alex
Procedural vs OOP

 

Hey,
Alex here.

Object-oriented programming has many advantages over procedural programming: it helps avoid “spaghetti code”, it makes your code more reusable, and it makes your app more solid and reliable.

On the other hand, OOP is also more complex and it’s more difficult to use it efficiently.
On top of that, PHP itself is not entirely OOP based like other languages, and sometimes it feels more natural to use a procedural approach.

In this email we are going to look at 3 cases where procedural programming can be a better choice than OOP.

 

#1: Stand-alone function or class method?

Let’s say that you want to implement a function, and you’re wondering if it’s better to create a class method or a stand-alone function.
A simple rule of thumb to help you decide is this:

If your function does not use any class property or method, then it can be a stand-alone function.

In other words, if the class method doesn’t use any of its own class assets, then you can move it outside of the class altogether.
Now, this is not a strict rule. It’s just a guideline, and there are many exceptions.
One of these exceptions is when the class provides the context for the function, but let’s talk about this in point #2.

#2: When you are using a class only for context.

Sometimes, you want to define a function inside a class just to give it the right context.
For example, let’s say that you have a User class, and a idFromName($name) method that returns a user’s ID from its name.

If this method does not use any class properties or methods, then there are no technical reasons for it to be inside the User class.
However, you may want to keep it inside the class to give it a specific context: this way, you know that this function is about users.

By doing this, you can also avoid adding a “user” prefix in the function’s name.

This seems like a valid reason to keep this function inside the class.
But if context is the only reason, you can achieve the same result by using Namespaces.
Instead of defining the function as a User class method, you can define it inside a Namespace like this:

Namespace Users;
function idFromName($name)
{
/* do your stuff… */
}

This gives the function its context, and you can keep using a short name because the namespace’s syntax makes it clear it’s about users:

$id = Users\idFromName($name);

#3: Is your class just a collection of variables?

Classes only make sense if you can take advantage of their functionalities.
But sometimes, classes are used just as a collection of properties.
For example:

class FruitsBasket()
{
public $apples = 0;
public $oranges = 0,
}

$basket = new FruitsBasket();
$basket->apples = 3;
$basket->oranges = 5;

This is useful to give the variables a context, but that’s it. You could obtain the same result using a namespace, or even with a simple array:

$fruitsBasket = array();
$fruitsBasket[‘apples’] = 3;
$fruitsBasket[‘oranges’] = 5;

Of course, you should use a class if you intend to use some OOP functionality, like setting the properties as private and using getters/setters methods to edit them.
But if all you need is a simple collection, you can avoid using a class in the first place.