UNIT1 - Programming Week [10] –Programming Paradigms 1 Lesson Learning Outcome Pass Merit Distinction LO2 Explain the characteristics of procedural, objectoriented and event driven programming, conduct an analysis of a suitable Integrated Development Environment (IDE) P2 Give explanations of what M2 Analyse the common procedural, object-oriented features that a developer has event driven paradigms are; access to in an IDE. their characteristics and relationship between them. D2 Critically evaluate the source code of an application which implements the program paradigms, in terms of the code structure and characteristics. 2 What is a Programming Paradigm? ▪ Programming paradigm is a style of programming which has characteristic features. ▪ One can say these are the foundations on which programming languages are designed and implemented. ▪ Some languages can be classified into multiple paradigms. ▪ Some paradigms are concerned mainly based on execution model of the language. ▪ Other paradigms are concerned mainly with the way that code is organized. ▪ Yet others are concerned mainly with the style of syntax and grammar. 3 Common Programming Paradigms Imperative Programming (Procedural Paradigm) Object Oriented Programming Event Driven Programming Declarative Programming Logic Programming Functional Programming 4 Imperative Programming ▪ Imperative = Procedural = Structured Programming ▪ Computer use reusable memory ▪ Programs runs by changing the state of the machine via Assignment ▪ Control flow is explicit, uses Sequence, Selection, Repetition ▪ These can be more efficiently translated to machine language ▪ Structured Programming is a refined imperative style with 2 added features Fortran, Algol, ▪ No GOTOs ▪ Variables have lexical scope due to modules Pascal, Basic, C 5 Object Oriented Programming ▪ Programs are represented as a Group of Objects. ▪ Objects exhibit their own behaviour. ▪ In imperative style Data are Passive, Procedures are Active. ▪ O-O paradigm, data is combined with procedures to give objects. For example in the imperative paradigm, one would write a procedure which prints the various kinds of object in the program. In the O-O paradigm, each object has a print-method, and you "tell" an object to print itself. ▪ Objects are grouped in Classes. ▪ Classes are arranged in a Hierarchy – Sub classes inherit features from Super classes Simula 67, Smalltalk, java, C++, C# , Modula 3, Ruby, Python 6 Declarative Programming ▪ Also called Non Imperative or Non Procedural ▪ Control Flow is implicit ▪ Programmer states only what the result should look like, not how to obtain it. SELECT Name, Salary FROM Employee WHERE DepName = ‘Sales’ ORDER BY Salary DESC No Loops No Selections ▪ SQL – Structured Query Language No Assignments is good example for this NO ALGORITHMS 7 Logic Programming ▪ This is also Declarative Programming ▪ What is known about the domain is defined as ▪ Facts ▪ Inference Rules ▪ Solution is found by the Interpreter by ▪ Unification – Principle of substitution ▪ Backtracking – Return to prior step and continue 8 PROLOG - Example /* Some rules */` is_digesting(X,Y) :- just_ate(X,Y). is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y). /* Some facts */ just_ate(mosquito,blood(john)). just_ate(frog,mosquito). just_ate(stork,frog). ? is_digesting (Who, mosquito) Who = frog Who = stork 9 Functional Programming ▪ This is also Declarative Programming ▪ No Variables and therefore No States ▪ Everything is defined as Functions ▪ No state changes – Immutable ▪ Thus preserve Referential Transparency – Guarantees that a function return the same result for the same parameters in each and every call, ▪ LISP and Haskel are functional language examples. 10 Event-driven Programming Event-driven programming is a programming paradigm in which the flow of program execution is determined by events - for example a user action such as a mouse click, key press, or a message from the operating system or another program. 11 Key Terminology The event source is an object on which the event occurs. Source provides information of the occurred event to it's handler via an Event object. This provides information of the occurred event. The listener is in effect a loop which waits for an event to occur. A collection of routines that defines the responses to the Event(S). In some implementations Listener and Handler is the same 12 How to create an Event-driven Program ▪ Step 1: The first step in developing an event-driven program is to write a series of subroutines, or methods, called event-handler routines. ▪ Step 2: The second step is to bind event handlers to events so that the correct function is called when the event takes place. Graphical editors combine the first two steps: double-click on a button, and the editor creates an (empty) event handler 13 How to create an Event-driven Program ▪ Step 3: Final step is to write the main loop. Most eventdriven programming environments already provide this main loop, so it need not be specifically provided by the application programmer. 14 New Trends in Languages ▪ Based on the main features we call it an Imperative, Object Oriented or Functional language. ▪ Very few languages implement a paradigm 100%. When they do, they are pure. ▪ It is incredibly rare to have a “pure OOP” language or a “pure functional” language. ▪ A lot of languages will facilitate programming in one or more paradigms. ▪ In Scala you can do imperative, object-oriented, and functional programming quite easily 15 Lesson Summary ▪ What is a Programming Paradigm ▪ Common Programming Paradigms ▪ Imperative Programming ▪ Object Oriented Programming ▪ Declarative / Logic Programming ▪ Functional Programming ▪ Event-driven Programming ▪ New trends in languages 16