Cis 470 Presentation - Unknown

25
Comparison of Procedural and OO Languages History Procedural OO LISP Clojure

Transcript of Cis 470 Presentation - Unknown

Page 1: Cis 470 Presentation - Unknown

Comparison of Procedural and OO Languages

● History● Procedural● OO● LISP● Clojure

Page 2: Cis 470 Presentation - Unknown

History of Programming (abridged)

● Hard-wired● Binary Code – control sequences instructing cpu● Assembly Languages – mnemonics & symbolic

labels● Procedural – first “high-level” languages● Object-Oriented – use of data structures called

“objects”

Page 3: Cis 470 Presentation - Unknown

Third Generation Programming Languages

● First generation: machine level (binary), no compilation or assembly

● Second generation: Assembly languages, assembled and processor specific

● Third generation – interpreted/compiled and expanded support for data types and computer takes care of nonessential details rather than the programmer. Includes procedural and OO.

Page 4: Cis 470 Presentation - Unknown

Procedural Programming

● Specifies step-by-step procedure program goes through to reach goal state.

● Based on the idea of Procedure Call:– Routines, subroutines, methods, functions

– A particular procedure can be called at any point in a program.

● Breaks down a task into collection of variable, data structures, and subroutines.

Page 5: Cis 470 Presentation - Unknown

List of Procedural Languages

● LISP● Basic● C● C++● Cobol● Fortran● MATLAB● Tcl-Tk

Page 6: Cis 470 Presentation - Unknown

Object-Oriented Programming

● Uses objects to design applications and computer programs.

● Objects – data structures consisting of data fields and methods

● data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance

Page 7: Cis 470 Presentation - Unknown

Common OO Programming Techniques

● Data abstraction - focusing on important characteristics and filtering out the unwanted characteristics.

● Encapsulation - provides the explicit boundary between an object's abstract interface and its internal implementation details. (Public, private, etc)

● Messaging - processes or objects can send and receive messages to and from other processes.

Page 8: Cis 470 Presentation - Unknown

List of Object-Oriented Languages

● JAVA● VB.NET● C#.NET● Python

Page 9: Cis 470 Presentation - Unknown

Hello World in Different Languages

● Procedural● OO

Page 10: Cis 470 Presentation - Unknown

LISP

(DEFUN HELLO-WORLD ()

(PRINT (LIST 'HELLO 'WORLD)))

Page 11: Cis 470 Presentation - Unknown

BASIC

10 PRINT "HELLO WORLD"

Page 12: Cis 470 Presentation - Unknown

C

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

printf("Hello, world\n");

return EXIT_SUCCESS;

}

Page 13: Cis 470 Presentation - Unknown

C++

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

printf("Hello, world\n");

return EXIT_SUCCESS;

}

Page 14: Cis 470 Presentation - Unknown

JAVA (as procedural)

public class Hello {● public static void main(String []args) {● System.out.println("Hello World");● }● }

Page 15: Cis 470 Presentation - Unknown

JAVA (as OO)

class Greeting {

void greet(Named target) {

System.out.println("Hello, " + target.getName() + "!");

}

}

interface Named {

String getName();

}

Page 16: Cis 470 Presentation - Unknown

JAVA (as OO cont.)

class World implements Named {

String getName() {

return "World";

}

}

class Main {

public static void main( String[] args ) {

Greeting greeting = new Greeting();

greeting.greet(new World());

}

}

Page 17: Cis 470 Presentation - Unknown

Python

print "hello world"

Page 18: Cis 470 Presentation - Unknown

LISP

● Procedural

● LISP: LISt Processing

● Lisp was traditionally used for AI because it supports the implementation of software that computes with symbols very well. Symbols, symbolic expressions and computing with those is at the core of Lisp.

● Typical AI areas for computing with symbols were/are: computer algebra, theorem proving, planning systems, diagnosis, rewrite systems, knowledge representation and reasoning, logic languages, machine translation, expert systems, and more.

Page 19: Cis 470 Presentation - Unknown

LISP (cont.)

● Most prevalent forms today:– Scheme - developed at the MIT AI Lab, had a

significant influence on the effort that led to the development of Common Lisp.

– Common Lisp - includes CLOS (Common Lisp Object System), an object system that supports multimethods and method combinations. It is extensible through standard features such as macros.

– Clojure - runs on the Java Virtual Machine

Page 20: Cis 470 Presentation - Unknown

Clojure

● Clojure enables programmers to use techniques normally associated with OO:

– Encapsulation

– Polymorphism

– Inheritance

Page 21: Cis 470 Presentation - Unknown

Encapsulation

● hiding of implementation detail, usually done at the class level. A class has public methods, private implementation details, and various other scopes in between.

● Clojure accomplishes encapsulation in three ways:

– Closures: A closure closes over (remembers) the environment at the time it was created. This is more general than the simplistic model offered by private, protected, public, friend, etc. in OO languages. Enables creation of arbitrary encapsulation strategies.

– Namespaces: groups a set of related data and functions which can be assigned as public or private.

– Immutability: In OO languages, another purpose of encapsulation is to prevent one object modifying private data used by another object. In Clojure data structures are immutable.

Page 22: Cis 470 Presentation - Unknown

Polymorphism

● The ability to choose a different method implementation based on the type of the caller:

Flyer a = new Airplane();

Flyer b = new Bird();

a.fly();

b.fly();

● Multimethods - more general than polymorphism, not limited to type

Page 23: Cis 470 Presentation - Unknown

Inheritance

● Allows creation of a derived type that reuses the behavior of a base type.

class Person {

String fullName() { /* impl details */ }

}

class Employee extends Person {

AddressBookItem companyDirectoryEntry() { /* impl details */ }

}

● In Clojure:

(defn full-name [p]

(str (:first-name p) " " (:last-name p)))

(defn company-directory-entry [p]

[(full-name p) (:extension p)])

Page 24: Cis 470 Presentation - Unknown

Object Orientation is Overrated by Rich Hickey (http://clojure.org/rationale)

● Born of simulation, now used for everything, even when inappropriate

● Encouraged by Java/C# in all situations, due to their lack of (idiomatic) support for anything else

● Mutable stateful objects are the new spaghetti code

● Hard to understand, test, reason about● Concurrency disaster

Page 25: Cis 470 Presentation - Unknown

Object Orientation is Overrated by Rich Hickey (http://clojure.org/rationale) (cont.)

● Inheritance is not the only way to do polymorphism

● "It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures." - Alan J. Perlis

● Clojure models its data structures as immutable objects represented by interfaces, and otherwise does not offer its own class system.

● Many functions defined on few primary data structures (seq, map, vector, set).

● Write Java in Java, consume and extend Java from Clojure.