Programming Paradigms

The word paradigm is used a great deal when talking about programming languages. What does it mean?

Definition

A programming paradigm is a style or “way” of programming. Some languages make it easy to write in some paradigms but not others.

Never use the phrase “programming language paradigm.” A paradigm is a way of doing something (like programming), and not a concrete thing (like a language). Now, it’s true that if a programming language L happens to may a particular programming paradigm P easy to express, then we often say “L is a P language” (e.g. “Haskell is a functional programming language”) but that does not mean there is any such thing as a functional language paradigm.

List of Common Paradigms

Some of the more common paradigms are

Paradigms are not meant to be mutually exclusive; you can program in a functional, object-oriented, event-driven style. Make sure to check out Wikipedia's entry on Programming Paradigms.

A Look At Some Major Paradigms

Imperative Programming

Control flow in imperative programming is explicit: commands show how the computation takes place, step by step. Each step affects the global state of the computation.

    result = []
    i = 0
start:
    numPeople = length(people)
    if i >= numPeople goto end
    p = people[i]
    nameLength = length(p.name)
    if nameLength <= 5 goto next
    upperName = toUpper(p.name)
    addToList(result, upperName)
next:
    i = i + 1
    goto start
end:
    return sort(result)

Structured Programming

Structured programming is a kind of imperative programming where the control flow is defined by nested loops, conditionals, and subroutines, rather than via gotos. Variables are generally local to blocks (have lexical scope).

result = [];
for i = 0; i < length(people); i++ {
    p = people[i];
    if length(p.name)) > 5 {
        addToList(result, toUpper(p.name));
    }
}
return sort(result);

Early languages emphasizing structured programming: Algol 60, PL/I, Algol 68, Pascal, C, Ada 83, Modula, Modula-2. Structured programming as a discipline is sometimes though to have been started by a famous letter by Edsger Dijkstra entitled Go to Statement Considered Harmful.

Object Oriented Programming

OOP is based on the sending of messages to objects. Objects respond to messages by performing operations. Messages can have arguments, so "sending messages" looks a lot like calling subroutines. A society of objects, each with their own "local memory" and own set of operations has a different feel than the "monolithic processor and single shared memory" feel of non object oriented languages.

result = []
for p in people {
    if p.name.length > 5 {
        result.add(p.name.toUpper);
    }
}
return result.sort;

The first object oriented language was Simula-67; Smalltalk followed soon after as the first "pure" object-oriented language. Many languages designed from the 1980s to the present have been object-oriented, notably C++, CLOS (object system of Common Lisp), Eiffel, Modula-3, Ada 95, Java, C#, Ruby.

Declarative Programming

Control flow in declarative programming is implicit: the programmer states only what the result should look like, not how to obtain it.

select upper(name)
from people
where length(name) > 5
order by name

No loops, no assignments, etc. Whatever engine that interprets this code is just supposed go get the desired information, and can use whatever approach it wants. (The logic and relational paradigms are generally declarative as well.)

Functional Programming

In functional programming control flow is expressed by combining function calls, rather than by assigning values to variables.

let(
  f, fun(
    people,
    if(equals(people, emptylist),
        emptylist,
        if(greater(length(name(head(people))), 5),
          append(to_upper(name(head(people))), f(tail(people))),
          f(tail(people))))),
    sort(f(people)))

Of course, there's usually syntactic sugar

let
    fun f [] = []
      | f (p :: ps) =
          if p.name.length() > 5 then p.name.to_upper()::(f ps)
          else (f ps)
in
    sort(f(people))

The real power of this paradigm comes from passing functions to functions (and returning functions from functions).

sort(
    filter((λs. s.length() > 5),
        map((λp. p.name.to_upper()), people)

Read Joel Spolsky's article on map and reduce.

With functional programming

Some people like to say

Exercise: Write the above example in Miranda, ML, and J.

Python has a neat little thing called list comprehensions that combine map and filter.

sorted([p.name.upper() for p in people if len(p.name) > 5])

Logic and Constraint Programming

Logic and constraint programming are two paradigms in which programs are built by setting up relations that specify facts and inference rules, and asking whether or not something is true (i.e. specifying a goal.) Unification and backtracking to find solutions (i.e. satisfy goals) takes place automatically.

Languages that emphasize this paradigm: Prolog, GHC, Parlog, Vulcan, Polka, Mercury, Fnil.

Exercise: Write the running example in Prolog.

Languages and Paradigms

One of the characteristics of a language is its support for particular programming paradigms. For example, Smalltalk has direct support for programming in the object-oriented way, so it might be called an object-oriented language. ML, Lisp, and JavaScript programs tend to make heavy use of passing functions around so they are called "functional languages" despite having variables and many imperative constructs.

A language purposely designed to allow programming in many paradigms is called a multi-paradigm language. Examples: Scala, D, Ada, Oz.