Since the dawn of time , the world has always been divided into two opposing ideas. I will explain. In the electrical industry the giants of their age Edison and Nicola Tesla championed two different ideas on how electricity should be conducted .Direct current vs Alternating current wars were very personal and the lengths that they went to in order to ensure their ideas carried the day were both extreme and daunting. This was the precursor to the birth of the information age.
In the programming sphere, the major debate has been centered around functional vs object oriented programming. These ideas are important because they represent different views on how to solve problems.In the case of functional programming, problems are solved by breaking them down into smaller parts called functions, with each function carrying out a very specific task.There is also the concept of pure functions, which is a pattern where functions: (a)Do not depend on external data.( b)Have no side effects (c) Give the same output with same inputs.
The major concept in object oriented programming is that of abstraction.The major question that object oriented aims to answer is , how do you represent real life objects like cars , human beings in the computing space. The answer to this is classes. A class contains methods which mimic real world functionality , a person walking or a car changing gears.What’s more there are key concepts like inheritance , polymorphism and encapsulation.
So why do we take time to learn all these concepts , think of a detective who has been called into a crime scene where the only evidence left is the suspect’s coat.No witnesses and no fingerprints, where should they begin?In the case of our poor detective they can start by measuring the length of the coat’s arms from end to end to get the suspect’s height.Now we know that the human height is on average 7 or 7.5 heads, this means we can get the size of our suspect’s head.We also know from human physiology that Length of face is equal to length of hand and that the length of foot is equal to length of forearm.There we have the suspect’s foot size hmm?We could go on and on getting to understand our suspect better just as our detective here.
We should not forget that these concepts help us to solve problems in the most efficient way possible, and that more than anything else should be the reason we learn them. More importantly we should be able to use them to solve problems practically using computational thinking among other problem solving techniques. The late Steve Jobs described computers as the bicycle of the mind, the reason being a bicycle can be up to 5 times more efficient than walking.
ENOUGH TALK SHOW ME THE MONEY!
Let’s crown this short theoretical class on functional vs object oriented techniques with some practical code.We shall use common lisp for functional programming and test out python 3.7’s new data classes for object oriented programming.
First we write a function to help us to store some music for our theoretical upcoming music app.
Our output for the code above running on Emacs should look like below
Now to our music App implementation using Python Data Classes newly introduced in python 3.7
In the age of APIs and multi system dependencies what does the future hold for pure functions? What of Aspect programming ? Does quantum computing completely change how we solve problems for example. As for which is better between functional and object oriented techniques , I prefer to think of them as tools , one a knife another a fork,you simply cannot compare the two without losing perspective. Depending on the nature of the challenge or your approach to the solution feel free to use the most effective one.
That’s all from me, Love and Light :)