— Software Engineering — 2 min read
here's my effort of bringing up the concept of Traditional Procedural Programming vs Object-Oriented Programming in the form of a story.
There were 2 associates - Harvey and Louis( Yes, I have been watching Suits these days :P)
And Jessica, their managing partner, gave them the following problem to solve-
1There are 2 kinds of Associates in the Firm - 2FirstYearAssociates and SecondYearAssociates34Both of them have the following functions to perform -56- minor research 7- prepare documents
Jessica asked Harvey and Louis to solve this problem in an efficient way, and the solution that's most efficient and scalable will be sent over to the client.
Now, Louis decided to go on with the Procedural programming approach, and Harvey insisted on following the Object-Oriented programming.
Louis divided the work to be done in the form of procedures/functions. He created two functions - minorResearch()
and prepareDocuments()
and wrote the following code for the problem-
1void minorResearch(string associate) {2 //3 //45}67void prepareDocuments(string associate) {8 //9 //1011}
Now, Harvey wanted to come up with something that would solve the problem in an efficient and scalable way.
He was aware that OOPs is best whenever we need to code real-life situations, so he created two classes - FirstYearAssociates
and SecondYearAssociates
and defined functions for both of them.
1class FirstYearAssociates {2 void research() {34 // DO MINOR RESEARCH56 }78 void prepareDocuments() {9 //10 //1112 }1314}
1class SecondYearAssociates {2 void research() {34 // DO MINOR RESEARCH56 }78 void prepareDocuments() {9 //10 //1112 }1314}
Both of them went to Jessica and pitched their solutions. Louis pointed out that he solved the problem faster and with fewer lines of code. However, Harvey justified that his approach is scalable and can handle future problems as well.
So Jessica came up with the updated problem statement
1The firm now has ThirdYearAssociates as well,2 and they perform the following functions -34- major research 5- prepare documents6- assist Senior Associates
Now, Louis got tensed and thought he was going to lose. He had to make changes in his existing code, which made it quite messy, but he was eventually able to come up with the following solution-
1void research(string associate) {23 if associate == "FirstYearAssociate" || associate == "SecondYearAssociate":4 // do minor research56 if associate == "ThirdYearAssociate":7 // do major research89}1011void prepareDocuments() {12 //13 //1415}1617void assistSeniorAssociates() {1819 //20 //2122}
While all Harvey did, was just adding one more class for Third Year Associates-
1class ThirdYearAssociates {2 void research() {34 // DO MAJOR RESEARCH56 }78 void prepareDocuments() {9 //10 //1112 }1314 void assistSeniorAssociates() {15 //16 //1718 }1920}
Now to note, Louis had to make changes in his original code and will have to do that every time in case the specification changes. While Harvey just wrote a new class without making any changes in the previous code.
So do you think Louis gave up? No, he argued that Harvey has written useless and duplicated code which was not even required, and he simply did it to get that promotion.
Jessica asks Harvey to explain the allegation thrown upon him. He then explains that he is well aware of the fact that the code is repeated and the classes have a lot in common, so he has already created a base class for all the associates with their common features -
1class Associate {23 void research() {4 //5 }67 void prepareDocuments() {8 //9 }1011}
Harvey emphasized on the fact that in the end, all of them are Associates
, and all of them do research and prepare documents, so he abstracted out the common features.
And derived the classes - FirstYearAssociate
, SecondYearAssociate
, and ThirdYearAssociate
from the base class Associate
itself.
Even further, if any class needs to implement any special behaviors, then it can simply override these methods, just like we have been doing for the research
method all this time:
1void research() {2 // DO MINOR RESEARCH3}
1void research() {2 // DO MAJOR RESEARCH3}
Well !!! Who do you think won? ;)
But now to sum it up in a generalized way.
In procedural programming, the program is divided into small parts called functions. In object-oriented programming, the program is divided into small parts called objects ( instance of a class).
Adding new data and function is not easy in procedural programming, while it is comparatively easier in OOPS.
In procedural programming, overriding is not possible, while it is possible for OOPS.
Procedural programming is based on the unreal world, while OOPS is made for the real-world.
C is an example of procedural programming, whereas C++ and Java are Object-Oriented Languages.
If you liked the write-up, leave your feedback and suggestions :)