Structured and object-oriented programming
The difference between object-oriented programming (OOP) and structured programming (SP) can be illustrated with a work situation. The following allegory contains a mixture of definitions from (Brookshear, 2011, pp. 240-280), (Scott, 2006) and (Barnes and Kölling, 2012):
Key concepts and analysis, with examples, of the two approaches:
Imagine a warehouse storage. SP is also called a top-down design approach as you write everything in the correct sequence. Each set of tasks is bundled together in their own blocks. One such block could be driving to the storage for example. There might be many shelves of goods that you see when you open the door. Now you have to write everything you want to do, in the top-down order, because there is no-one to help you there.
In the OOP approach, when you knock on the warehouse door (the object), a storage manager (methods) opens up. He’s an expert on his warehouse and the locations of the different goods – he has many procedures on how to do tasks. If you ask him to throw away anything past a certain expiry date he will know where to find these items and perform this fast.
With the OOP your business is also very scalable. Should you need more warehouses, the old manager will train the new one to make sure he knows everything. You could say the manager 2 inherits the knowledge of manager 1. Should the new warehouse differ slightly from the old, information will be given to the new manager at start-up. This is called constructors.
Although your warehouses differ, they still have the same characteristics. Should you need a report on the stock, send out a command and they will all get back to you with the same report (class). The contents of the warehouses might differ but they can still use the same template for that report. When different objects are basically the same but have some special characteristics, this is called polymorphism. Exact details on what your warehouses contain is classified information, competition shouldn’t get to know. That’s why it’s locked in a safe, or encapsuled, from prying eyes.
Advantages and disadvantages:
There are considerations to be made when choosing between OOP and SP. You will need to consider the size of your business for example. Do you need a warehouse manager? In real life that would be a great expense if you don’t need it, in programming terms it requires more knowledge from the developer and it might take longer time to write. In an SP situation, you would go to the warehouse and only do what you needed, yourself.
However, if you have a lot of warehouses it would be good to outsource the storage to managers. Having experts on this might also be beneficial: OO approaches often have the opportunity to use components from software development packages, which means pre-made code that could help you in your work. Suddenly the warehouse manager knows how to help you even better with your storage.
In a SP method you could still have new functions but you would have to write it all yourself in the correct order, and if you have several warehouses, would you need to repeat code? Can you call them all up with one command and make them do an action simultaneously? With a big company, that is a large amount of code, it might be difficult to get an overview and see what’s going on. It would also impact execution speed and implementation cost (Scott, 2006, pp. 8).
Is OOP preferred, and if so, why?
Please find key concepts of OOP in bold text above. When it comes to popularity, the object-oriented paradigm is the most prominent in today’s software development. (Brookshear, 2011, pp. 247). Brookshear also claims that «it is because objects are well-defined units whose descriptions are isolated in reusable classes that the object-oriented paradigm has gained popularity.»
This ties in well with Scott’s (2006, pp. 471) reasons for OOP to be beneficial:
- Conceptual load. The developer has to focus on less information at one time.
- Fault containment. When you only program on parts of code at a time there is less code to make mistakes in. Also, if there is an error, there is less code to look through and it might not affect the whole of the program.
- Because the program is sectioned into components, different programmers can work on different tasks. Also, one component can be updated without the other component never being interrupted. However sometimes one component will need to know the limitations and construction of another component in order to know what is possible to execute.
Research shows that while the architecture between SP and OOP might be different, the knowledge component largely remains the same (Sircar, Nerur and Mahapatra, 2001, pp.457-471). A short example, from own knowledge:
Example of Ada definition of 2 variables in one sentence (SP):
VariableName1, VariableName2 : Datatype1;
Example of Java definition of 2 variables in once sentence (OOP):
datatype1 VariableName1, VariableName2;
Is OOP better to reflect on the human way of thinking and conceptualising?
In the examples of the warehouse above, I would say that OOP reflects a human way of thinking very well. In a work situation, we all have our jobs that we have responsibilities for, that we develop and we collaborate with other parts of the business. In a home situation, we also have our roles and our children inherit the parent’s traits but still have their own personality. I find this way of thinking very logical and I feel it is easy to understand the concept. Here is an illustration of an original object and copies:
Barnes, D. J. and Kölling, M. (2012) Objects first with Java : a practical introduction using BlueJ. Boston : Pearson, 2012; 5th ed. [EBSCO].
Brookshear, J. G. (2011) Computer science : an overview. Boston , M.A. ; Pearson / Addison-Wesley, 2011.
Scott, M. L. (2006) Programming language pragmatics. [electronic book]. San Francisco, Calif. : Morgan Kaufmann ; c2006; 2nd ed. [Online] [EBSCO].
Sircar, S., Nerur, S. P. and Mahapatra, R. (2001) ‘Revolution Or Evolution? a Comparison of Object-Oriented and Structured Systems Development Methods’, MIS Quarterly, 25 (4), pp.457-71.
Image source: By Milesjpool, (Own work) [CC BY-SA 4.0 (http://creativecommons.org/licenses/by-sa/4.0)], via Wikimedia Commons