Programming Paradigms

If all you have is a hammer, everything looks like a nail.

                                                                      - Abraham Maslow?

Paradigms

·       In the context of programming, a paradigm is a model for problem solving.

·       More specifically, it's a toolbox for building programs.

·       A paradigm dictates what solutions look like.

·       Programming languages support one or more programming paradigms.

·       Programmers use to one type of toolbox can become habitualized to solving problems in one type of way.

Declarative versus Imperative Paradigms

·       Examples of problem domains include healthcare, e-commerce, human resources (HR), logistics (shipping, inventory control), education, gaming, etc.

·       Programming is the art of mapping real world problem-domain concepts onto abstract solution-domain concepts.

·       The length of a programming arrow in the diagram represents the cognitive distance between concrete problem domain concepts and abstract solution domain concepts. We call this distance the semantic gap. The bigger the gap, the more work for the programmer.

·       The length of a compiling arrow in the diagram represents the compiler complexity—the amount of work the compiler must do to translate a program into an assembly language program.

·       Imperative paradigms include structured programming, procedural programming, and modular programming. (More on these later.)

·       Languages supporting imperative paradigms (C, Pascal, Fortran, etc.) are command-oriented. A program is a sequence of commands for controlling a virtual computer. Commands update data structures (variables, arrays, lists, trees, graphs, etc.) Procedures (functions) are programmer-defined commands. Compilers for these languages are simple because these concepts are abstractions of the concepts available in assembly languages (i.e., basic commands for updating memory and controlling the flow of execution).

·       However, the semantic gap is large because computer world-- the world of functions and data structures-- is quite different from a hospital world containing doctors, nurses, and medical records, from an HR world containing employees, positions, and benefits packages, from a gaming world containing warriors, monsters, and weapons.

·       Declarative paradigms include logic programming and object-oriented programming. (I treat functional programming separately.)

·       In grammar an imperative statement is a command: "sit down", "be quiet", "get to work." A declarative statement asserts a fact or belief: "2 + 2 = 5", "today is Tuesday", "let x = 10".

·       The statements of a declarative language are descriptions of the way things are in some problem domain. In object-oriented languages (C++, Java, Scala, etc.) solution-domain objects represent problem-domain objects. Programmers focus of defining the properties and behaviors of these objects. In a logic programming language like Prolog, programmers simply write down facts and rules about some problem domain.

·       The semantic gaps for declarative languages are small because the solution space concepts are abstractions of problem domain concepts. However, these languages require much more complex compilers.

Other Paradigms

·       In the functional paradigm a program is a set of side-effect-free function declarations:
                        def square(x: Double) = x * x

·       However, the distinction between functions and data is erased. Functions can be viewed as algorithms or data:
                        Functions = Data

·       This means that functions can be the inputs and/or outputs of higher order functions. Programmers can therefore write meta-functions:
                                                          meta(function spec) = a function implementing the spec

·       To avoid problems in multi-threaded programs associated with synchronizing access to shared variables (and for other reasons), the functional paradigm takes the radical step of banning variables!

·       Languages supporting the functional paradigm include Lisp, Haskell, Miranda, ML, and Scala.

·       A new paradigm is emerging that I like to call the functional-object-oriented paradigm. Languages that support this paradigm (Scala, Java 8+) are object-oriented languages that include function objects or lambdas. These are objects that represent functions. They can be treated like objects (data) but they can also be executed like algorithms.

Details

·       Imperative Programming

·       Functional Programming

·       Object-Oriented Programming

·       Logic Programming