COMP 310
Fall 2011

Lec02: Programming Paradigms

Home  Info  Owlspace  Java Resources  Eclipse Resources

Do you need to 'scream yet?  If so, then you need to go to the Engineering Ice Cream Social!   If not, you really need to go!  

TODAY (Wed. 8/24/11)  3:00 - 4:30 PM in the Engineering Quad. 
Be there, aloha!

 

Eclipse + Plug-ins Installation

Having installation problems?  The TA's are waiting to help you!

Major Programming Paradigms

Programming is a form of communication between a developer and a computer.  A programming language allows the developer to express their ideas and desires in such a way that the computer can understand them.  Just as in spoken languages, the same ideas can be expressed in many languages with advantages and disadvantages to every language.   Operatic arias sound better in Italian than in English, but English is more precise in its meaning.    So it is with programming languages--each has its strengths and weaknesses.  The paradigm you would use in a small imbedded system with limited computing resources is not the same as what you would use in an enterprise system that spans hundreds of servers across the globe.

Below is a table listing the major programming paradigms and what sorts of problems they are commonly used for as well as some common disadvantages encountered.   This table is in no way meant to be definitive or binding.   The issues surrounding any given programming task are rarely so clear cut.   There may be other reasons such as speed or memory constraints that override the advantages or disadvantages of one paradigm over another.   Plus, any given programming language is rarely purely adherent to one specific paradigm.  Most programming languages have aspects of all paradigms.   Scheme has constructs to create state and have side effects ala imperative programming, C++ tries to cover both procedural and OO programming, and Python is a mixture of functional and OO styles.

 

Paradigm View of the World Representative Language Useful for Disadvantages
Functional Programming Represents the world as mathematical functions Lisp, Scheme, Haskell
  • Algebraically representable calculations
  • Proveably correct algorithms
  • Theoretical representations
  • Stateless processes
  • Difficult to scale up
  • Type safety often lacking
  • Limited to mathematical abstractions
Procedural/Imperative Programming Represents computational processes as a set of sequential steps. C, Fortran, Pascal
  • Highly optimized calculations, either in speed or size
  • Sequential or iterative processes
  • Complex processes lead to "spaghetti code"
  • Does not scale well to large systems.
  • Poor expression of complex abstractions
Object-oriented Programming Represents the world as a system of interacting objects. Java,  C++, C#, Smalltalk
  • Large complex systems.
  • Highly flexible, extensible systems
  • Event-driven systems
  • Highly abstract systems
  • Overkill for small systems.
  • Slower due to dispatching overhead
  • Large memory requirements
Declarative (Logic/Constraint) Programming Represents the logic of a computation Prolog, SQL (examples)
  • Proveably correct algorithms
  • Logic-based problems
  • Problems characterized by definitions and/or constraints
  • Difficult to see what is actually happening computationally
  • Non-intuitive models required

There is NO "right" programming paradigm!   Use the best tool for the job!

The boundaries between paradigms are not sharp --real programs use a blend of paradigms.

The Best Programming Language?

"Debate over the most popular programming language can become an emotional, almost religious battle."
http://www.devtopics.com/most-popular-programming-languages/

The above lists are a little dated, so here are some more recent posts:

As the above article points out, even measuring the most popular programming language is the subject of great disagreement.   And then if you roll in what one means by the "best" language, you are surely in for a long, wild and heated debate.    Much of the time, a developer doesn't even have a choice, being hemmed in by political, economic, legacy and compatibility issues more than any theoretical desires.  

In the end, you choose the language that best fits the entire situation, which often emcompasses much more than just the computational needs of problem.  Perhaps one reason for Microsoft's .NET system's rise in popularity is the fact that it enables multiple languages to easily be used in a single project.   Technically, the Java Virtual Machine system is capable of this as well, but for probably more political and business reasons than anything, this capability has never developed much traction in the industry.

 

Why OOP in Comp310?

Examples of OO systems:

 

Blended Paradigms

Here's a snippet of code that uses all 4 paradigms at once:

Object anObject;  // An object to look for
IList myList;  // A list of objects.  IList is superclass of IEmptyList and INEList.

...   // Code to intialize the variables omitted.

// Search the list looking for an element that the supplied Comparator says is equal to the above object.
// Return true if found, return false otherwise.
boolean isInList = (Boolean) myList.execute( new IListVisitor() {

        // Empty list case:  nothing here, so return false always.
        public Object emptyCase(IEmptyList host, Object param) {
            return false;
        }
    
        // Non-empty list case: compare the element here with the above element
        // Return true if the same, return recursive result if not, i.e. keep looking. 
        public Object nonEmptyCase(INEList host, Object param) {
    	    if(0 == ((Comparator)param).compare(host.getFirst(), anObject)) return true;
    	    else return host.getRest().execute(this, param);
        }
    }, new MyComparator());
    

Object-oriented aspects:  Calling methods on intelligent objects, abstract list structure, polymorphic dispatching.

Functional aspects:  Algorithm treated as an entity, use of closures to access data, recursive algorithm.

Procedural aspects:  Use of conditional statement to make a branching decision.

Declarative aspects:  Defining the algorithm in terms of all possible cases without explicitly stating how each case is accessed.

 

 

 


© 2011 by Stephen Wong and Scott Rixner