COMP 310
|
Lec02: Programming Paradigms |
Having installation problems? The TA's are waiting to help you!
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 |
|
|
Procedural/Imperative Programming | Represents computational processes as a set of sequential steps. | C, Fortran, Pascal |
|
|
Object-oriented Programming | Represents the world as a system of interacting objects. | Java, C++, C#, Smalltalk |
|
|
Declarative (Logic/Constraint) Programming | Represents the logic of a computation | Prolog, SQL (examples) |
|
|
"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.
Examples of OO systems:
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