ICT|程式編寫範式 Programming Paradigms

Low-level language

Faster execution due to direct addressing on memory address / More efficient on hardware control [DSE 2022 P2D Q1(f)]

First generation language (1 GL)

  • Example: Machine language
  • The codes of machine language consists of a series of binary numbers (0 or 1)

Programs written in machine languages can be executed by the CPU directly

It is difficult to write and maintain machine language

Machine dependent
e.g. programs written for a particular type of computer cannot be executed on other types of computers

Second generation language (2 GL)

  • Example: Assembly language
  • Assembly language uses mnemonics to represent the machine language instructions
  • Since mnemonics are short codes that are easier to remember and use than binary codes, this makes assembly language easier to write than the machine language
  • Assembly language is often used in programming related to hardware, such as driver programs, and also the kernels of operating systems etc.

High-level language

Machine independent [DSE 2022 P2D Q1(f)]

Easy to write, understand, debug and maintain [DSE 2022 P2D Q1(f)]

Third generation language (3 GL)

  • Example: C / Pascal
  • The codes of third generation lanaguages are more similar to human languages, this makes the codes more readable and easier for programmers to write and maintain

Fourth generation language (4 GL)

  • Example: SQL (Structured Query Language)
  • Forth generation language is developed to provide an even higher level of abstraction than third-generation languages
  • Programmers can write complex programs / write less steps and instructions without worrying about the low-level details of the hardware or software

Fifth generation language (5 GL)

  • Example: Prolog
  • Fifth generation languages allow programmers to specify problems in more human language ways and to provide high-level abstractions for complex problem-solving
  • They are less focused on the syntax of programming and more focused on the problem-solving process (i.e., What to solve)

Evolution of programming languages

Procedural (imperative) programming language

  • Example: Pascal / C / Fortran

Characteristics

  • Consists of a main program/a sequence of instructions to be executed in the order designed by the programmer[DSE 2018 P2D Q2(e)]
    (i.e., Programmers need to specify the details of the logic steps of commands)
  • It focuses on how a problem can be solved

Easier to trace program logic [DSE 2019 P2D Q3(c)(i)]

Source codes can be written with fewer restrictions [DSE 2019 P2D Q3(c)(i)]


Declarative programming language

  • Logic e.g., Prolog
  • Query e.g., SQL

Characteristics

  • Focuses on what should be solved for a problem
  • Programmers do not need to specify the algorithm / logic, they just need to state what are required to solve

It focuses on setting the goal (“what to solve”) and the problem is solved by relations (associating facts and rules), but not on specifying how to solve. [DSE 2012 P2D Q4(c)(ii)]


Object-oriented programming language

  • Example: C# / Python

Characteristics

  • A program is presented by objects, classes and methods with the use of encapsulation, inheritance, polymorphism [DSE 2018 P2D Q2(e)]

Higher reusability: Because of inheritance, the attributes and methods of new classes can be inherited from the existing classes[DSE 2019 P2D Q3(c)(ii)]

Information hiding: the details of the internal implementation (e.g., how the class/ behaviours is implemented) can be hidden [DSE 2019 P2D Q3(c)(ii)]

Wide range of libraries are provided [DSE 2019 P2D Q3(c)(ii)]

Object-oriented programming

Definition

  • A program written by object-oriented language is presented by objects, classes and methods with the use of encapsulation, inheritance, polymorphism [DSE 2018 P2D Q2(e)]

Examples of object-oriented languages

  • Python [DSE 2017 P2D Q3(d)(i)]
  • C#
  • C++

Examples of non-object-oriented languages

  • Fortran [DSE 2017 P2D Q3(d)(i)]
  • Assembly language

Objects, classes, attributes and methods

What is a class?

  • It represents the set of attributes (properties) or methods (behaviors) that are common to all objects
  • Class is the blueprint for an object
  • Attributes are variables of a program
  • Methods are functions of a program

Consider the class of a car.
Wheels / Speed limits attributes
Increase_speed() / Reduce_speed() methods

What is a object?

  • It is the building blocks of object-oriented programs
  • It is an instance of a class
  • It contains data values, attributes and methods that are applied to the object

Characteristics of object-oriented programming

Encapsulation

  • Encapsulation means that attributes and methods are stored in the same object / class

Abstraction

  • Information hiding: the details of the internal implementation (e.g., how the class/ behaviours is implemented, or the access to certain attributes and methods) can be hidden

Inheritance

  • Inheritance means that one class can derive or inherit the attributes and methods from other classes
  • Reusability: the classes can be reused
  • class can be reused to create different classes through inheritance
  • shortening the development time

Polymorphism

  • Methods can be used differently under different situations, but they share the same name
  • The new method can override the original method

Criteria of selecting programming languages

  • Scale and modularity
  • Reusability
  • Portability
    Some languages (such as Java and Python) are cross-platform programming language
    Note: Cross platform languages means that source code can be compiled on different platforms without major changes
  • Execution efficiency
    The execution efficiency of C / C++ usually is higher than Python because of more hardware control
  • Functional strengths
    Some programming languages (such as SQL) is designed for database, which may not be suitable for developing programs
  • Readability
  • Utility libraries and development tools
  • End-user interaction
  • Familiarity
  • Licensing fee for using libraries (other reasonable costs)

Modularity [DSE 2015 P2D Q2(c)(i)]

Definition

  • System is divided into smaller modules so that each module can be developed by different programmers simultaneously

Reducing development time

Modules can be reused (higher reusability)

Program libraries [DSE 2018 P2D Q2(e)(ii), DSE 2021 P2D Q3(d)(iii)]

  • Program libraries are external references to other programs

Program libraries usually contain fewer bugs

Providing many suitable, ready-to-use subprograms
→ It shortens development time

Saving storage space
Since multiple programs can use the same library, and only the references (addresses) are stored in executable codes
→ this reduces the duplication of codes in the hard disk


封面圖:Image by nikitabuida on Freepik