Imperative Programming vs Declarative Programming
Today we are going to learn about Imperative programing vs Declarative programming.
You’ve probably heard the terms imperative (or procedural) and declarative programming before, and if you’re anything like me, went looking for a precise definition of the two only to find some vague hand wavy descriptions that didn’t give you any concrete answers.
Over time I’ve realized that this is because it really isn’t black or white. Code snippet A can be more declarative than code snippet B, but maybe code snippet C is even more declarative than code snippet A.
Here’s my best shot at explaining how I think about imperative and declarative programming.
Imperative Programming:
Imperative programming is a style of programming that focuses on how something gets done. The code is usually overly concerned with building results in intermediate variables and managing control flow with loops and conditional statements.
Say we have a list of users and we want to fetch the email addresses of all users who have an email address on file.
An imperative solution in PHP could look like this:
function getUserEmails($users)
{
$emails = [];
for ($i = 0; $i < count($users); $i++) {
$user = $users[$i];
if ($user->email !== null) {
$emails[] = $user->email;
}
}
return $emails;
}
This probably seems innocent enough, and we’ve all written code that looks like this. But think about what this code is saying:
1. Create an empty array that we will use to build our result
2. Create a variable to store our counter, starting at 0
3. Check our counter variable to make sure it is still less than the number of users in the array
• If so:
1. Create a reference to the item in the array at the location matching our current counter value
2. Check if the email property of the user is equal to null • If not, add that user’s email address to the end of our result array 3. Increment our counter by one 4. Return to step 3
• If not, return our result array
Instead of trying to say “give me the emails of the users who have emails”, the solution focuses on implementation details about how many times to repeat chunks of code, accessing indexes on data structures, and managing counters.
Declarative Programming:
Instead of focusing on how the computer should do the work, declarative programming focuses on telling the computer what we need it to accomplish.
Compare the code above to the same operation in SQL:
SELECT email FROM users WHERE email IS NOT NULL
We didn’t have to write anything about loops, counters, or array indexes. We just told the computer what we wanted, not how to get it.
Under the hood, I’m sure the SQL engine must be doing some sort of iteration or keeping track of which records it’s checked or which records it hasn’t, but I don’t really know for sure.
And that’s the beauty of it: I don’t need to know.
PHP is a much different beast than SQL of course, and we’re not going to be able to recreate that exact syntax.
Hope you enjoyed this!
Bonus: You must love my most read article : How To Use Single Responsibility Principle in PHP/Laravel
And most GitHub star repo:
https://github.com/mohasinhossain/SOLID-Principles-in-PHP