Q2. Explain different programming paradigms, their shortcomings etc. with appropriate examples.
Ans. By paradigm one means a way of thinking or doing things.
Paradigm means organizing principle of a program. It is an approach to programming.
Since the invention of the computer, many programming approaches have been tried such as procedural programming, modular programming, structural programming etc. the primary motivation in each cash has been the concern to handle the increasing complexity of programs that are reliable and maintainable.
Let us discuss these different programming paradigms and key language mechanisms necessary for supporting them.
- Procedural Programming
A program in a procedural language is a list of instructions where each statement tells the computer to do something. The focus is on the processing, the algorithm needed to perform the desired computation. This paradigm is:
Decide which procedures you want;
use the best algorithm you can find.
Languages support this paradigm by providing facilities for passing arguments to functions (subprograms) and returning values from functions (subprograms).
In procedural paradigm, the emphasis is on doing things. What happens to the data? Data is after all, the reason for a program’s existence. The important part of an inventory program isn’t a function that displays or checks data, it is the inventory data itself. Yet data is given second-class status while programming.
- Modular Programming
With the increase in program size, a single list of instructions becomes unwieldy. Thus a large program is broken into smaller units that is, functions (sub-programs). The idea of breaking a program into functions can further be extended by grouping a number of functions together into a larger entity called a module, but the principle is similar: grouping of components that carry out specific tasks.
A set of related procedure with the data they manipulate is called a Module.
This programming paradigm, also known as ‘data-hiding principle’ states:
Decide which modules you want;
Partition the program so that data is hidden in modules.
Where there is no grouping of procedures with related data, the procedural programming style suffices. The techniques for designing ‘good procedures’ still apply on each member procedure of a module.
In modular programming, since many modules (or functions) access the same data, the way the data is stored becomes critical. The arrangement of the data can’t be changed without modifying all the functions that access it.
Another problem associated with procedural and modular programming is that their chief components – functions etc. do not model the real world very well. For instance, a procedural program for library maintenance aims at the operations Issue, Return etc. whereas the real world entities are Books. But ‘Books’ are given second-class status in the program.
- Object-Oriented Programming
To understand this most recent concept among programming paradigms, let us take an example. We have to prepare lot of dishes that involve baking; for instance, cake, biscuits, pie, dhokla, pastries, buns etc. We have to write programs for it. Using procedural programming paradigm, we’ll have to write separate programs for every recipe and each program will be containing instructions for operating oven. Ovens are used to make a lot of different dishes. We don’t want to create a new oven every time we encounter a new recipe. Having solved problem once, it would be nice to be able to IreuseI the solution in future programs. But the procedural programming paradigm suffers from this drawback. Though functional software ruse is theoretically possible, but it hasn’t worked every well in practice. The large amount of interaction between conventional functions and its surroundings makes reusing them difficult.
However, if the same problem is solved using Object-Oriented approach, it’ll not only be less complex but also make software reuse feasible and possible. The object-oriented approach views a problem in terms of objects involved rather than procedure for doing it.
Object is an identifiable entity with some characteristics and behavior.
For instance, we can say ‘Orange’ is an object. Its characteristics are: it is spherical shaped, its colour is orange etc. its behavior is: it is juicy and it tastes sweet-sour. While programming using OOP approach, the characteristics of an object are represented by its data and its behavior is represented by its functions associated. Therefore, in OOP programming object represents an entity that can store data and has its interface through functions.
The above mentioned problem of baking dishes, in OOP approach, will be viewed in terms of objects involved (that is, OVEN) and its interface (that is, the functions representing its working). This not only results in simpler program design but also reduces software cost.
To understand this concept more clearly, let us consider another example. Let us simulate traffic flow at a red light crossing.
As you know, procedural programming paradigm focuses on the procedures or the working action.
Using procedural programming paradigm, the above said problem will be viewed in terms of working happening in the traffic-flow that is, moving, halting, turning etc. The OOP paradigm, however, aims at the objects and their interface. Thus in OOP approach, the traffic-flow problem will be viewed in terms of the objects involved. The objects involved are: cars, trucks, buses, scooters, auto-rickshaws, taxis etc.
With this elaborations, the concept must be clear enough to proceed for the OOP paradigm definition. But before that let us understand another very important term class.
A Class is a group of objects that share common properties and relationships.
In the above example, cars have been identified as objects. They have characteristics like: steering wheel, seats, a motor, brakes etc. and their behavior is their mobility. Car, however, is not an object, it is a class (compare with the definition). Opel Astra is an object that belongs to the class ‘car’. ‘Car’ is a subclass of another class ‘automobiles’ which again is a subclass of ‘vehicles’. ‘Object’ is an instance of ‘class’. For example, we can say ‘bird’ is a class but ‘parrot’ is an object.
Now, after understanding the basic terminology, we can define object-oriented programming (OOP) paradigm as:
Decide which classes and objects are needed;
Provide a full set of operations for each class.