Major Programming Paradigms
There are several kinds of major programming paradigms:
- Imperative
- Logical
- Functional
- Object-Oriented
It can be shown that anything solvable using one of these paradigms can be solved using the others; however, certain types of problems lend themselves more naturally to specific paradigms.
Imperative
- Introduction:
The imperative programming paradigm assumes that the computer can maintain through environments of variables any changes in a computation process. Computations are performed through a guided sequence of steps, in which these variables are referred to or changed. The order of the steps is crucial, because a given step will have different consequences depending on the current values of variables when the step is executed.
- Imperative Languages:
Popular programming languages are imperative more often than they are any other paradigm studies in this course. There are two reasons for such popularity:
- the imperative paradigm most closely resembles the actual machine itself, so the programmer is much closer to the machine;
- because of such closeness, the imperative paradigm was the only one efficient enough for widespread use until recently.
- Advantages
- efficient;
- close to the machine;
- popular;
- familiar.
- Disadvantages
- The semantics of a program can be complex to understand or prove, because of referential transparency does not hold(due to side effects)
- Side effects also make debugging harder;
- Abstration is more limitted than with some paradigms;
- Order is crucial, which doesn't always suit itself to problems.
Logical
- Introduction:
The Logical Paradigm takes a declarative approach to problem-solving. Various logical assertions about a situation are made, establishing all known facts. Then queries are made. The role of the computer becomes maintaining data and logical deduction.
- Logical Paradigm Programming:
A logical program is divided into three sections:
- a series of definitions/declarations that define the problem domain
- statements of relevant facts
- statement of goals in the form of a query
Any deducible solution to a query is returned. The definitions and declarations are constructed entirely from relations. i.e. X is a member of Y or X is in the internal between a and b etc.
- Advantages:
The advantages of logic oriented programming are bifold:
- The system solves the problem, so the programming steps themselves are kept to a minimum;
- Proving the validity of a given program is simple.
Sample Code of Logical Paradigm.
Functional
- Introduction
The Functional Programming paradigm views all subprograms as functions in the mathematical sense-informally, they take in arguments and return a single solution. The solution returned is based entirely on the input, and the time at which a function is called has no relevance. The computational model is therefore one of function application and reduction.
- Languages
Functional languages are created based on the functional paradigm. Such languages permit functional solutions to problems by permitting a programmer to treat functions as first-class objects(they can be treated as data, assumed to have the value of what they return; therefore, they can be passed to other functions as arguments or returned from functions).
- Advantages
The following are desirable properties of a functional language:
- The high level of abstraction, especially when functions are used, supresses many of the details of programming and thus removes the possibility of commiting many classes of errors;
- The lack of dependence on assignment operations, allowing programs to be evaluated in many different orders. This evaluation order independence makes function-oriented languages good candidates for programming massively parallel computers;
- The absence of assignment operations makes the function-oriented programs much more amenable to mathematical proof and analysis than are imperative programs, because functional programs possess referential transparency.
- Disadvantages
- Perhaps less efficiencey
- Problems involving many variables or a lot of sequential activity are sometimes easier to handle imperatively or with object-oriented programming.
Sample Code of Functional Paradigm.
Object-Oriented
- Introduction
Object Oriented Programming (OOP) is a paradigm in which real-world objects are each viewed as seperate entities having their own state which is modified only by built in procedures, called methods.
Because objects operate independently, they are encapsulated into modules which contain both local environments and methods. Communication with an object is done by message passing.
Objects are organized into classes, from which they inherit methods and equivalent variables. The object-oriented paradigm provides key benefits of reusable code and code extensibility.
- Features & Benefits
A new class (called a derived class or subclass) may be derived from another class (called a base class or superclass) by a mechanism called inheritance. The derived class inherits all the features of the base class: its structure and behavior(response to messages). In addition, the derived class may contain additional state (instance variables), and may exhibit additional behavior (new methods to resond to new messages). Significantly, the derived class can also override behavior corresponding to some of the methods of the base class: there would be a different method to respond to the same message. Also, the inheritance mechanism is allowed even without access to the source code of the base class.
The ability to use inheritance is the single most distinguishing feature of the OOP paradigm. Inheritance gives OOP its chief benefit over other programming paradigms - relatively easy code reuse and extension without the need to change existing source code.
The mechanism of modeling a program as a collection of objects of various classes, and furthermore describing many classes as extensions or modifications of other classes, provides a high degree of modularity.
Ideally, the state of an object is manipulated and accessed only by that object's methods. (Most O-O languages allow direct manipulation of the state, but such access is stylistically discouraged). In this way, a class' interface (how objects of that class are accessed) is separate from the class' implementation (the actual code of the class' methods). Thus encapsulation and information hiding are inherent benefits of OOP.
Sample Code of Object-Oriented Programming Paradigm.