Programming Concepts: Programming paradigms

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 3 - ⇑ Programming Concepts ⇑

Programming paradigms Object-oriented programming (OOP) →


Paradigm - a typical example or pattern of something; a pattern or model


You should have experience of programming solutions to problems. This experience is probably in a language such as: VB.NET, PHP, Python, Java or Pascal and you should be able to quickly understand what code like this would do:

  1. dim name as string
    
  2. dim age as integer
    
  3. console.writeline("please insert your name")
    
  4. name = console.readline
    
  5. console.writeline("Hello, " & name & " please insert your age")
    
  6. age = console.readline()
    
  7. console.writeline("Well, well " & name & " it looks like you are " & age & " years old")
    

So far you have been using two types of programming paradigms or set of concepts defining how a language works, these are called Procedural-orientated programming and Structured programming techniques. Taking a look at the example above, structured languages move from the program line by line, starting at 1, then 2, then 3. We have also made use of procedural paradigm features such as when the code meets a procedural name such as console.writeline("..") on line 5, then the program jumps off, executes the code corresponding to console.writeline("..") and returns to execute line 6. In this course you will be learning the following types, there are more

  • Procedural-orientated programming
  • Structured programming techniques
  • Functional programming
  • Logic Programming
  • Event-driven programming
  • Object-orientated programming

Structured programming techniques[edit]

Structured programming techniques involve giving the code you write structures, these often involve writing code in blocks such as:

  • Sequence - code executed line by line
  • Selection - branching statements such as if..then..else, or case.
  • Repetition - iterative statements such as for, while, repeat, loop, do, until.

It also involves breaking down problems into smaller problems until routines can be written that execute single tasks. The routines operate on data passed to them through parameters and these routines can be re-used and are often packaged into libraries to save time in development.

  1. dim x as integer
    
  2. x = 7
    
  3. If x = 2 then
    
  4.   x = x + 4
    
  5. else
    
  6.   x = x - 2
    
  7. end if
    
  8. console.writeline(x)
    

Procedural-oriented programming[edit]

Sharing the same features as structured programming techniques, Procedural-oriented programming implement procedures/subroutines to execute common functionality

  1. dim x as integer
    
  2. x = 7
    
  3. x = doubleIt(x)
    
  4. console.writeline(x)
    

Functional programming[edit]

In functional programming programs define mathematical functions. A solution to a problem consists of a series of function calls. There are no variables or assignment statements, but instead there are lists and functions that manipulate these lists. An example of a functional programming language is Haskell.

Logic programming[edit]

A logic program consists of a set of facts and rules. A knowledge base is built up about a specific subject and an inference engine uses the knowledge base to answer queries which are presented in the form of a goal. Logic programming is often used for artificial intelligence systems.

Event-driven programming[edit]

Event Driven programming refers to your standard Windows Form idea, the program waits in a loop until an event(e.g. the click of a button, or a keystroke). The program then runs the code associated with this event and then returns to its loop, providing that the code did not instruct it to close.

If more than one event occurs, code is queued by the program and run in the order the events were triggered.

Object-oriented programming[edit]

Object-oriented programming takes the techniques used in structured programming further and combines routines and the data they use into classes. The data items stored for a class are called fields and the routines that operate on these fields are called methods.

To use a class a programmer can declare instances of the class, which are called objects. This is known as instantiation.

Instantiation - defining an object based on a class


Class definition - a pattern or template that can be used to create objects of that class


A new class can be based on an existing class and it then inherits all the fields and methods of the existing class. The programmer can then declare other fields and methods that are specific to the new class. The new class is known as a subclass or derived class of the existing class and the existing class is known as a superclass or parent class.

Encapsulation - combining a record with the procedures and functions that manipulate it to form a new class



Exercise: Programming paradigms

List 4 different programming paradigms

Answer :

  • Procedural-orientated programming
  • Structured programming techniques
  • Event-driven programming
  • Object-orientated programming

What is meant by a programming paradigm

Answer :

a model or pattern; a template, defining how a programming language functions

Describe how Event-driven languages work

Answer :

the program waits in a loop until an event(e.g. the click of a button, or a keystroke). The program then runs the code associated with this event and then returns to its loop, providing that the code did not instruct it to close.

Describe 2 events that you might expect a computer game to handle

Answer :

  • button presses
  • network communication, e.g. someone sending you a message

Describe 2 events that you might expect a word processor to handle

Answer :

  • keyboard input
  • mouse buttons