ANU The Australian National University



____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

COMP2100/2500
Lecture 29: Comparison of Programming Paradigms

Summary

A discussion of object-oriented, procedural, scripting and other classes of programming languages; what they're good for, what they're not...

Outline

  1. Object-oriented languages.

  2. Comparison between Eiffel and Java.

  3. Procedural languages.

  4. Comparison between procedural and object-oriented languages.

  5. Scripting languages.

  6. Other paradigms.

  7. Functional programming in Haskell.


Introduction

In this course we have covered languages of three fundamentally different types:

There are other, radically different, programming paradigms.


Object Oriented languages

Object Oriented Languages Include:

Characteristic Features of OO languages:


Java and Eiffel: Differences within a Paradigm

class

   COUNTER

creation
   reset

feature {ALL}

   number: INTEGER
         -- number counted

   increment is
         -- count another one
      do
         number := number + 1
      end

   reset is
         -- reset to zero
      do
         number := 0
      end

end -- class COUNTER
public class Counter {

  private int num;

  public Counter() {
    num = 0;
  }

  /** number counted */
  public int number() {
    return num;
  }

  /** count another one */
  public void increment() {
    num++;
  }

  public void reset() {
    num = 0;
  }
}

Java and Eiffel: Feature Comparison

Major feature comparison:

Feature Eiffel Java
Multiple Inheritance + -
Generic Types + -
Design by Contract + -
Overloading - +

Java makes a distinction between the concepts of subtyping (using the implements keyword) and inheritance (using the extends keyword). A class can be a subtype of another class, without inheriting its implementation. Eiffel doesn't have this distinction.

Minor feature comparison:


Procedural languages

Procedural Languages Include:

Characteristic features of Procedural languages:


C Programming Example

Header: counter.h
typedef int counter;

/* reset to zero */
extern void 
reset(counter *c);

/* count another one */
extern void 
increment (counter *c);
Implementation: counter.c
#include "counter.h"
/* reset to zero */
void reset(counter *c) {
  *c = 0;
}
/* count another one */
void increment(counter *c) {
  *c = *c + 1;
}

Object Oriented and Procedural: Feature Comparison

Feature Object Oriented Procedural
Abstraction through subtyping + -
Reuse through Inheritance + -
Encapsulation of data + -*
Automatic Memory Management +** -

*: is supported by modular languages
**: not supported by all OO languages

But the news is not all good for OO languages:


Object-oriented languages are not completely replacing procedural

Procedural languages remain the more appropriate choice in application areas where:

For example:


Scripting

Scripting languages include:

Characteristic features of scripting languages:


When Would You Use a Scripting Language?

When the task

Scripting languages (particularly Perl) are now used extensively to generate dynamic web pages.


Other Programming Paradigms

There are other, radically different, ways to program:


Introduction to Functional Programming in Haskell

Data-types are defined separately from the functions that manipulate them. A generic stack type is defined as follows:

data Stack t = Empty | Push t (Stack t)

A stack is defined to be either the constant stack Empty, or the result of the constructor Push that takes an element and a stack. The following are all valid stacks:

Functions can now be defined by pattern matching on the different kinds of stack:

is_empty :: (Stack t) -> Bool
is_empty Empty    = True
is_empty (Push x s) = False
 
depth :: (Stack t) -> Int
depth Empty      = 0
depth (Push x s) = 1 + depth s

Function definitions need not be exhaustive: top and pop are not defined for empty stacks. (This avoids having to state preconditions like in Eiffel, write error-handling code or special cases...)

top :: (Stack t) -> t
top (Push x s) = x

pop :: (Stack t) -> (Stack t)
pop (Push x s) = s

Note that pop is not a procedure that modifies an existing stack, instead it is a function that makes a new stack from an old one. (We did something like this in Stack Version 3.)


Expression Trees in Haskell

data Expression = Constant Int |
                  Sum Expression Expression |
                  Product Expression Expression

value :: Expression -> Int
value (Constant n)  = n
value (Sum a b)     = (value a) + (value b)
value (Product a b) = (value a) * (value b)

toStringInfix :: Expression -> String
toStringInfix (Constant n)  = show n
toStringInfix (Sum a b)     = "(" ++ toStringInfix a ++
                              "+" ++ toStringInfix b ++ ")"
toStringInfix (Product a b) = "(" ++ toStringInfix a ++
                              "*" ++ toStringInfix b ++ ")"

toStringPostfix :: Expression -> String
toStringPostfix (Constant n)  = show n
toStringPostfix (Sum a b)     = toStringPostfix a ++ " " ++
                                toStringPostfix b ++ " +"
toStringPostfix (Product a b) = toStringPostfix a ++ " " ++
                                toStringPostfix b ++ " *"

Evaluation of Functional Programming

The expression tree just presented offers equivalent power to a Visitor pattern style implementation of expression trees in Eiffel.


The Big Picture

There are a variety of significantly different programming paradigms:

By the time you graduate you will have had more exposure to other language families. By that time you should be able to consider what programming paradigm best suits a given problem.

____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

Copyright © 2005, Jim Grundy & Ian Barnes, The Australian National University
Version 2005.1, Wednesday, 25 May 2005, 12:15:00 +1000
Feedback & Queries to comp2100@cs.anu.edu.au