Lecture 03 Programming Paradigms
Lecture 03 Programming Paradigms
A programming paradigm provides (and determines) the view that the programmer has of the
execution of the program. E.g. in object-oriented programming, programmers can think of
a program as a collection of interacting objects, while in functional programming a program
can be thought of as a sequence of stateless function evaluations. Different programming
languages advocate different programming paradigms. Some languages are designed to
support one particular paradigm (Smalltalk and Java support object-oriented programming
while Haskell and Scheme support functional programming), while other programming
languages support multiple paradigms (such as Common Lisp, Python, and Oz.)
The relationship between programming paradigms and programming languages can therefore
be complex since a programming language can support multiple paradigms. For example, C+
+ is designed to support elements of procedural programming, object-based programming,
object-oriented programming, and generic programming.
However, designers and programmers decide how to build a program using those paradigm
elements i.e. one can write a purely procedural program in C++, one can write a purely
object-oriented program in C++, or one can write a program that contains elements of both
paradigms.
Unstructured source code is notoriously difficult to read and debug, and so is discouraged in
programming languages that support any kind of structure. However, structure is not needed
in any programming language since program structures can always be implemented by a
combination of conditional statements and Goto statements.
Unstructured programming is still used in some scripting languages such as MS-DOS batch
files, older programming languages such as BASIC or FORTRAN. Despite Goto statements
(jumps) having a small performance benefit over procedure calls, current CPU architectures
have made the difference negligible. In fact, the improper use of such statements can be
harmful, either by obfuscating code and/or preventing good compiler optimization.
B. Structured Programming
Structured programming is often associated with a "top-down" approach to design. In this
way designers map out the large scale structure of a program in terms of smaller
operations, implement and test the smaller operations, and then tie them together into a
whole program.
Almost any language can use structured programming techniques to avoid common pitfalls of
unstructured languages and it is most famous for removing or reducing reliance on the GOTO
statement.
Low-level structure
At a low level, structured programs are composed of simple, hierarchical program flow
structures. These are regarded as single statements, and are at the same time ways of
combining simpler statements, which may be one of these structures, or primitive statements
such as assignments or procedure calls. The three types of structure identified by Dijkstra
were sequence, selection, and repetition:
High-level structure
Coders should break larger pieces of code into shorter subroutines (functions, procedures.
methods, blocks, or otherwise) that are small enough to be understood easily. In general,
programs should use global variables sparingly; instead, subroutines should use local
variables and take arguments by either value or reference. These techniques help to make
isolated small pieces of code easier to understand without having to understand the whole
program at once.
C. Procedural Programming
Procedural programming is a programming paradigm based upon the concept of the
procedure call. Procedures, also known as routines, subroutines, methods, or functions simply
contain a series of computational steps to be carried out. Any given procedure might be called
at any point during a program's execution, including by other procedures or itself
(Recursion).
The main program here coordinates calls to procedures
and hands over appropriate data as parameters.
Examples:
• Ada • ColdFusion
• ALGOL • COBOL
• BASIC • Component Pascal
• C • D
• C++ • Delphi
• ECMAScript a.k.a. ActionScript, • Occam
DMDScript, JavaScript, JScript • M
• Forth • Pascal
• Fortran • Perl
• F • PHP
• Lasso • PL/C
• Maple • PL/I
• Mathematica • Rapira
• MATLAB • Seed7
• Modula-2 • VBScript
• Oberon and Oberon-2 • Visual Basic
Advantages of OOP
OOP is claimed to promote greater flexibility and maintainability in programming, and is
widely popular in large-scale software engineering.
OOP is claimed to be easier to learn for those new to computer programming than
previous approaches.
The OOP approach is often simpler to develop and to maintain, lending itself to more
direct analysis, coding, and understanding of complex situations and procedures than other
programming methods.
E. Flow-Driven Programming
Flow-driven programming is a computer programming paradigm used by traditional
programs, which follow their own control flow pattern, only sometimes changing course at
branch points. This is in contrast to Event-driven programming, which is especially common
in applications which are asynchronous, such as a text editor, a kernel, or a GUI application.
F. Event-Driven Programming
Event-driven programming is a computer programming paradigm whereby unlike traditional
programs, which follow their own control flow pattern, only sometimes changing course at
branch points, the control flow of event-driven programs is largely driven by external
events.
Instead of waiting for a complete command which may order it to process information, the
system is pre-programmed with an event loop, to look repeatedly for information to process
(whether this might be the appearance of a file in a folder, a keyboard or mouse operation, or
a timer event) and then perform a trigger function to process it. Programming an event driven
system is thus a matter of rewriting the default trigger functions of the system, to match the
required behavior.
Homework
2. Download and read through the manual on how to use Dev C++ from
http://coltech.vnu.edu.vn/~duybt/ap/How_to_use_Dev-C.pdf
Homework (Chapter 5)
Write a program that calculates and returns the area and circumference of a circle and the
area and perimeter of a triangle. In both cases the user is allowed to specify dimensions.