
Programming Paradigms in the 21st Century
Explore various programming paradigms such as imperative, functional, declarative, and automata-based programming in the 21st century. Learn about different programming styles and their main traits, examples, critiques, and applications in modern programming languages.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
ON BEYOND OBJECTS PROGRAMMING IN THE 21 PROGRAMMING IN THE 21TH THCENTURY CENTURY COMP 590-059 FALL 2021 David Stotts Computer Science Dept UNC Chapel Hill
How to Properly Wear a Mask
Programming Paradigms (source) Related paradigm(s) Paradigm Description Main traits Critique Examples Programs as statements that directly change computed state (datafields) A style of imperative programming with more logical program structure Derived from structured programming, based on the concept of modular programming or the procedure call Direct assignments, common data structures, global variables Edsger W. Dijkstra, Michael A. Jackson Fortran IV, Basic, C, C++, Java, Kotlin, PHP, Python, Ruby Imperative Structograms, indentation, no or limited use of goto statements C, C++, Java, Kotlin, Pascal, PHP, Python Structured Imperative Local variables, sequence, selection, iteration, and modularization Structured, imperative C, C++, Lisp, PHP, Python Fortran 77 Procedural Objects, methods, message passing, information hiding, data abstraction, encapsulation, polymorphism, inheritance, serialization-marshalling Treats datafields as objects manipulated through predefined methods only Common Lisp, C++, C#, Eiffel, Java, Kotlin, PHP, Python, Ruby, Scala, JavaScript[8][9] Object- oriented Procedural Wikipedia, others
Programming Paradigms (source) Related paradigm(s) Paradigm Description Main traits Critique Examples Treats computation as the evaluation of mathematical functions avoiding state and mutable data C++, C#,Clojure, Coffeescript, Elixir, Erlang, F#, Haskell, Java (since version 8), Kotlin, Lisp, Python, R,[4] Ruby, Scala, SequenceL, Standard ML, JavaScript, Elm Lambda calculus, compositionality, formula, recursion, referential transparency, no side effects Functional Declarative Control flow is determined mainly by events, such as mouse clicks or interrupts including timer Event-driven including time- driven Main loop, event handlers, asynchronous processes Procedural, dataflow JavaScript, ActionScript, Visual Basic, Elm Defines program logic, but not detailed control flow Fourth-generation languages, spreadsheets, report program generators SQL, regular expressions, Prolog, OWL, SPARQL, Datalog, XSLT Declarative Treats programs as a model of a finite state machine or any other formal automata State enumeration, control variable, state changes, isomorphism, state transition table Automata- based programming Abstract State Machine Language, YACC / Bison, Lex / Flex Imperative, event-driven
Sample Fortran II Program C AREA OF A TRIANGLE WITH A STANDARD SQUARE ROOT FUNCTION C INPUT - TAPE READER UNIT 5, INTEGER INPUT C OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUT C INPUT ERROR DISPLAY ERROR OUTPUT CODE 1 IN JOB CONTROL LISTING READ INPUT TAPE 5, 501, IA, IB, IC 501 FORMAT (3I5) C IA, IB, AND IC MAY NOT BE NEGATIVE OR ZERO C FURTHERMORE, THE SUM OF TWO SIDES OF A TRIANGLE C MUST BE GREATER THAN THE THIRD SIDE, SO WE CHECK FOR THAT, TOO IF (IA) 777, 777, 701 701 IF (IB) 777, 777, 702 702 IF (IC) 777, 777, 703 703 IF (IA+IB-IC) 777, 777, 704 704 IF (IA+IC-IB) 777, 777, 705 705 IF (IB+IC-IA) 777, 777, 799 777 STOP 1 C USING HERON'S FORMULA WE CALCULATE THE C AREA OF THE TRIANGLE 799 S = FLOATF (IA + IB + IC) / 2.0 AREA = SQRTF( S * (S - FLOATF(IA)) * (S - FLOATF(IB)) * + (S - FLOATF(IC))) WRITE OUTPUT TAPE 6, 601, IA, IB, IC, AREA 601 FORMAT (4H A= ,I5,5H B= ,I5,5H C= ,I5,8H AREA= ,F10.2, + 13H SQUARE UNITS) STOP END (3-way) if IA is < 0, goto 777, ==0 goto 777, > 0 goto 701
Sample Fortran IV Program C AREA OF A TRIANGLE - HERON'S FORMULA C INPUT - CARD READER UNIT 5, INTEGER INPUT C OUTPUT - C INTEGER VARIABLES START WITH I,J,K,L,M OR N READ(5,501) IA,IB,IC 501 FORMAT(3I5) IF(IA.EQ.0 .OR. IB.EQ.0 .OR. IC.EQ.0) STOP 1 S = (IA + IB + IC) / 2.0 AREA = SQRT( S * (S - IA) * (S - IB) * (S - IC) ) WRITE(6,601) IA,IB,IC,AREA 601 FORMAT(4H A= ,I5,5H B= ,I5,5H C= ,I5,8H AREA= ,F10.2, $13H SQUARE UNITS) STOP END
Sample Fortran IV Programs C AREA OF A TRIANGLE - HERON'S FORMULA C INPUT - CARD READER UNIT 5, INTEGER INPUT, ONE BLANK CARD FOR END-OF-DATA C OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUT C INPUT ERROR DISPAY ERROR MESSAGE ON OUTPUT 501 FORMAT(3I5) 601 FORMAT(4H A= ,I5,5H B= ,I5,5H C= ,I5,8H AREA= ,F10.2, $13H SQUARE UNITS) 602 FORMAT(10HNORMAL END) 603 FORMAT(23HINPUT ERROR, ZERO VALUE) INTEGER A,B,C 10 READ(5,501) A,B,C IF(A.EQ.0 .AND. B.EQ.0 .AND. C.EQ.0) GO TO 50 IF(A.EQ.0 .OR. B.EQ.0 .OR. C.EQ.0) GO TO 90 S = (A + B + C) / 2.0 AREA = SQRT( S * (S - A) * (S - B) * (S - C) ) WRITE(6,601) A,B,C,AREA GO TO 10 50 WRITE(6,602) STOP 90 WRITE(6,603) STOP END
Sample Fortran 77 Program ! ! Program to calculate the sum of up to n values of x**3 ! where negative values are ignored. ! IMPLICIT NONE INTEGER I,N REAL SUM,X,Y READ(*,*) N WRITE(*,*) N SUM=0 DO I=1,N READ(*,*) X WRITE(*,*) X IF (X.GE.0.0) THEN Y=X**3 SUM=SUM+Y END IF END DO WRITE(*,*) The sum of positive cubes: ,SUM END
Sample Basic Programs 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 30 INPUT "How many stars do you want: "; N 40 S$ = "" 50 FOR I = 1 TO N 60 S$ = S$ + "*" 70 NEXT I 80 PRINT S$ 90 INPUT "Do you want more stars? "; A$ 100 IF LEN(A$) = 0 THEN GOTO 90 110 A$ = LEFT$(A$, 1) 120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30 130 PRINT "Goodbye "; U$ 140 END 10 LET N=10 20 FOR I=1 TO N 30 PRINT "Hello!" 40 NEXT I 50 END 10 PRINT Hello! 20 GOTO 10
Sample APL Program (source) Conway s Game of Life Alan Kay on APL APL Has been called a write-only language And so you know
Bit of Fun just for fun Timeline of Programming Languages
What are Issues with OO? Goals: OO methods/models were intended to increase reuse increase modularity How do we do support each in OO PLs and models? Objects allow calling code (methods) without having to mod it or know it (reuse) Packages (modularity)
What are the failings in OO? Goals: OO methods/models were intended to increase reuse increase modularity How do we do fail to support each? Sheer size modules can get much larger than we can keep in mind (and therefore easily and effectively reason about) Encapsulation allowed, not required (breaks modularity, breaks abstractions) Not easy to separate out different concerns in a module/class, not easy to keep a class cohesive
Encapsulation in Java Example . . . yes, we can do it public class EnCapso { private String fName; private String lName; private int age; private double salary; EnCapso (String fn, String ln, int a, double s) { this.fName = fn; this.lName = ln; this.age = a; this.salary = s; } public int getAge() { return this.age; } public void setAge(int a) { this.age = a; } public double getSalary() { return this.salary; } public void setSalary(double num) { this.salary = num; } public String getName() {return this.fName + " " + this.lName;} public void raiseSalary(double inc) { this.salary += inc; } }
Encapsulation in Java Example . . . but, we don t have to do it public class NoCapso { String fName; // public since not declared private String lName; // public int age; // public double salary; // public NoCapso (String fn, String ln, int a, double s) { this.fName = fn; this.lName = ln; this.age = a; this.salary = s; } public int getAge() { return this.age; } public void setAge(int a) { this.age = a; } public double getSalary() { return this.salary; } public void setSalary(double num) { this.salary = num; } public String getName() {return this.fName + " " + this.lName;} public void raiseSalary(double inc) { this.salary += inc; } }
Encapsulation in Java Can reach into the object even with getters setters, etc. public class EncapDemo { public static void main (String[] args) { EnCapso pEN = new EnCapso("Anne", "Smith", 27, 23456.78); NoCapso pNO = new NoCapso("Mark", "Jones", 45, 45678.91); System.out.println("Encapsulated object, Class EnCapso "); System.out.println(pEN.getAge()); //System.out.println(pEN.age); // cant do it due to encapsulation pEN.raiseSalary(15000.00); //pEN.salary = pEN.salary + 21000.00; // cant do it, cant reach in System.out.println(pEN.getSalary()); //System.out.println(pEN.salary); // cant do it System.out.println("Non Encapsulated object, Class NoCapso "); //System.out.println(pNE.getAge()); // the correct way, call public getter System.out.println(pNO.age); // wrong, reaching into object pNO //pNO.raiseSalary(21000.00); // correct way, call a public method pNO.salary = pNO.salary + 21000.00; // wrong, reaching into object pNO //System.out.println(pNO.getSalary()); // correct way, call public getter System.out.println(pNO.salary); // wrong, reaching into object pNO } }
Killing Modularity in Java public class EncapDemo { public static void main (String[] args) { EnCapso pEN = new EnCapso("Anne", "Smith", 27, 23456.78, 15.50); // register hours worked for (int i=1; i<=6; i++) { pEN.addDay(i*2); } // get pay info System.out.println(pEN.totHours()+" hours"); // good way System.out.println(pEN.totPay()+" dollars"); /* // bad way to get pay info double nHours = 0.0; for (int d=0; d<pEN.nDays; d++) { nHours += pEN.hours[d]; } double totPay = nHours * pEN.rate; System.out.println(nHours+" hours worked"); System.out.println(totPay+" dollars paid"); */ } }
Killing Modularity in Java public class EnCapso { private String fName; private String lName; private int age; private double salary; double[] hours; //double hours = 0.0; int nDays; double rate; EnCapso (String fn, String ln, int a, double s, double r) { this.fName = fn; this.lName = ln; this.age = a; this.salary = s; this.hours = new double[31]; this.nDays = 0; this.rate = r; } public double totHours() { double tot = 0.0; for (int i=0; i<this.nDays; i++) { tot += this.hours[i]; } return tot; //return this.hours; } public double totPay() { return this.totHours() * this.rate; } public void addDay(double h) { this.hours[nDays] = h; nDays++; //hours += h; nDays++; } public int getAge() { return this.age; } public void setAge(int a) { this.age = a; } public double getSalary() { return this.salary; } public void setSalary(double num) { this.salary = num; } public String getName() { return this.fName + " " + this.lName; } public void raiseSalary(double inc) { this.salary += inc; } }
Ok move on AOP Aspect Oriented Programming
Assignment Due Thursday (no uploads) 1) Follow the AJC Install on Windows instructions and get AspectJ working on your laptop (or other machine) 2) Try some simple programs 3) You may work alone, or in groups of up to 3. In group work, the group produces a solution and one group member submits it with a note saying who it is for 4) Nothing to submit for this, but next Assignment will be an AspectJ program to submit
END END