Programming Paradigms

Programming Paradigms
Programming paradigms are styles of programming. It is a way to think about
programming , and reflects from the way you write your code. A lot of
languages support multiple paradigms. This includes Ada, C++, C#, Clojure,
Common Lisp, D, E, ECMAScript (which is the standard form of Javascript and
Actionscript), Go, and Haskell. This list could continue, but it would go on
for quite a bit. Its hard to give an thorough definition without showing the
examples so lets get into some of the most common known programming paradigms.

1. Imperative
Imperative programming is a paradigm that uses statements that change
the program’s state. It consists of commands for the computer to perform and
focuses on describing how a program operates. The earliest imperative languages
were machine languages of the original computers. Instructions were very simple,
and that made hardware implementation easier. FORTRAN was one of the earliest
languages that removed the obstacles presented by machine code when creating
complex programs. FORTRAN was developed in 1954 by John Backus at IBM. In the
1960s, you had languages like COBOL, ALGOL, BASIC, and MUMPS. And in the 70s
you had C.There are 2 minor paradigms within this paradigm in itself as well.

2. Procedural
Procedural programming stems from Imperative programming, and is based on
the concept of procedure calls known as routines, subroutines, or functions.
The reason imperative and procedural languages are similar is because
they both make explicit references to the state of the execution environment.

3. Object-Oriented
The second minor paradigm to come out of Imperative programming is Object-
Oriented programming. Object-Oriented programming is based on the concept of
objects which contain data in the form of fields, attributes, or properties,
and code, in the form of procedures. There are two types of Object-Oriented
programming. Class-based languages contain previously defined classes and
objects instantiated by the classes. Prototype-based languages contain objects
and no classes exist. The prototype of an object is just another object to
which the object is linked. Main concepts around this paradigm are
encapsulation (also known as information hiding), composition, inheritance,
and delegaion. You can refer to the current classes tutorials in the C++
section to get a generalization of how they work together.

4. Functional
Functional programming is a bit different then the others. It is a style of
building the structure and elements of computer programs that treats computation
as the evaluation of mathematical functions and avoids changing state and
mutable data. It has its origins in lambda calculus. You can see how this is
a bit different then imperative-style paradigms, which operate on changing
states with statements , for example a simple assignment. Common languages that
follow this are : Common Lisp, Scheme, Clojure, Wolfram, Racket, Erlang, OCaml,
Haskell, as well as F#. Programming in a function style can be also done within
languages like Perl, PHP, C++ (since C++11), and Kotlin.

#include <functional>
#include <iostream>

/* Define a template function */
template <typename T>
void PrintValue(T value) {
  std::cout << value << std::endl;
}

int main(void) {
  /* A function wrapper to a function */
  std::function<void(int)> func_a = PrintValue<int>;
  func_a(2015);
  return 0;
  }

5. Generic
Generic programming is a style which algorithms are written in terms of
“types to be specified later” that are then instantiated when needed for
specific types. The first form of generic programming appeared in 1973 with the
languages ML, which stems from Lisp. ML permits writing common functions or
types that differ only in the set of types on which they operate when used. This
generally reduces duplication. Genericity is implemented and supported
differently in various programming languages; the term “generic” has also been
used differently in various programming contexts. For example, in Forth the
compiler can execute code while compiling and one can create new compiler
keywords and new implementations for those words on the fly.
It has few words that expose the compiler behaviour and therefore naturally
offers genericity capacities that, however, are not referred to as such in
most Forth texts. A good example of this is the use of templates in c++:

template <typename T>
class List {
  // Class contents.
};

List<Animal> list_of_animals;
List<Car> list_of_cars;

6. Metaprogramming
Metaprogramming is a programming technique in which computer programs have
the ability to treat other programs as their data. It means that a program can
be designed to read, generate, analyze or transform other programs, and even
modify itself while running. In some cases, this allows programmers to minimize
the number of lines of code to express a solution, in turn reducing development
time. It also allows programs greater flexibility to efficiently handle new
situations without recompilation. There are a couple of different types of
Metaprogramming. These include : Automatic , Reflective, Template and Macro
based metaprogramming. Here is an example of template metaprogramming in C++:

template <unsigned int n>
struct factorial {
	enum { value = n * factorial<n - 1>::value };
};

template <>
struct factorial<0> {
	enum { value = 1 };
};

// Usage examples:
// factorial<0>::value would yield 1;
// factorial<4>::value would yield 24.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s