Programming paradigms

Cards (41)

  • Programming paradigm

    A style of computer programming
  • Major programming paradigms
    • Procedural
    • Object-oriented
    • Declarative
    • Functional
  • Procedural programming
    • Has a series of instructions that tell the computer what to do with the input in order to solve the problem
    • Widely used in educational environments, being relatively easy to learn and applicable to a wide variety of problems
    • Structured programming is a type of procedural programming which uses the programming constructs of sequence, selection, iteration and recursion
    • Uses modular techniques to split large programs into manageable chunks
  • Object-oriented programming (OOP)
    • Developed to make it possible to abstract details of implementation away from the programmer, make code reusable and programs easy to maintain
    • To a great extent taking over from procedural programming
  • Declarative programming
    • Supported by languages such as SQL, where you write statements that describe the problem to be solved, and the language implementation decides the best way of solving it
  • Functional programming
    • Functions, not objects or procedures, are used as the fundamental building blocks of a program
    • Statements are written as a series of functions which accept input data as arguments and return an output
  • Different types of problem require different types of language, and hundreds of different languages have been developed for different types of application
  • Assembly language was the first language to be developed after machine code, and the next step was the development of procedural languages in the 1900s
  • Procedural language
    • Has built-in data types such as integer, real or floating point numbers, character, Boolean and string
    • Typically has data structures such as array and record
    • Programmers can define their own abstract data types such as queue, stack, tree, or hash table
  • Stack (abstract data structure)

    Can only add an item to the top, and can only remove an item from the top
  • The programmer might decide there is a limit to how large the stack is allowed to grow
  • You can't remove an item from an empty stack
  • The important thing is that someone can determine the state of the stack
  • In Python, a stack could be implemented using a list
  • This data structure should not be implemented by every programmer who needs to use a stack
  • The key thing to know is the state and behaviour of the data structure
  • It is a waste of time for every programmer who needs to use a stack to have to decide how to implement it
  • Object-oriented approach
    Defining a class as the description of what the data looks like (the state) and what the data can do (the behaviour)
  • Object
    An instance of a class
  • Programming in an object-oriented language
    • Thinking in terms of the objects that will carry out the required tasks, rather than thinking about data structures and algorithms
  • Objects in a hospital system
    • patient
    • ward
    • doctor
    • nurse
  • Class
    • Has attributes such as name, number of beds, number of patients, location, type
    • Has behaviours such as admit patient, discharge patient
  • Inheritance
    Subclasses inherit properties from the superclass
  • Subclasses of Property
    • Property For Rent
    • Property For Sale
  • The subclasses have the same attributes as the superclass Property, and in addition, each has some attributes of its own
  • In the class diagram, inheritance is shown using an open-headed arrow
  • Datastructure

    A class coded in an object-oriented language
  • Datastructure subclasses
    • Stack
    • Queue
  • Stack and Queue
    • Inherit attributes isEmpty and isFull from the superclass Datastructure
    • Inherit methods add and remove from the superclass Datastructure
  • Defining the Datastructure superclass
    1. Declare private attributes size, isFull, isEmpty
    2. Define procedure new to initialise size
    3. Define procedure addItem to add an item
    4. Define function removeItem to remove an item
  • Attributes are generally described as private, meaning users cannot directly access them
  • Methods

    Functions return a value, procedures do not
  • Defining the Stack subclass
    1. Inherit from Datastructure
    2. Define function removeItem to remove item from end of stack
  • The attributes size, isFull, isEmpty and methods new, addItem are inherited by the Stack subclass from the Datastructure superclass
  • Polymorphism
    A programming language's ability to process objects differently depending on their class
  • Class of objects

    • Has behaviours or methods, all of which will be inherited by its subclasses
  • Example: Stack class
    1. Redefines the method removeItem defined in the superclass Datastructure to process objects in the class differently
    2. Both Stack and Queue objects carry out the method addItem in an identical way, adding the item to the end of the data structure
  • Encapsulation
    The attributes size, isFull, isEmpty are all defined in the superclass Datastructure and can only be accessed through the class methods
  • Constructors and inheritance
    • Inheritance is denoted by the inherits keyword, and superclass methods are defined with the keyword super
    • A procedure with the name new is a constructor
  • Creating a new object
    • To create a new object called mystack of size 20 which belongs to class Stack, the following statement would be written: mystack = new Stack(20)