Declarative and imperative programming languages

11.5.1.3 compare imperative and declarative programming languages 

Declarative and imperative programming languages

Ex. 0. "Levels of programming languages"

A programming paradigm is a fundamental style of computer programming. 

We consider two paradigms: imperative vs declarative.

Video "Programming Paradigms: Paradigms Overview"

Declarative languages

Declarative programming is a programming paradigm in which the programmer describes the required result or the logic of the problem without specifying every step of the control flow.

The programmer mainly specifies WHAT should be achieved, while the language implementation or system determines how to produce the result.

Programmers will create a knowledge base in logic programming. A knowledge base contains a series of related facts and rules.

Example of facts and rules,

Fact: female (jane)
Fact: female (mary)
Fact: female (susan)
Fact: male (john)
Fact: male (david)
Fact: male (george)
Fact: spouse (john, jane)
Fact: spouse (david, mary)
Fact: spouse(george, susan)
Rule: husband(A,B) IF spouse(A,B) AND male (A)
Rule: wife (A,B) IF spouse(A,B) AND female(B)

A query could then be written:

        wife(david,mary)? 

Examples of declarative languages: Prolog, HTML, Haskell, CSS, SQL,...

Imperative languages

An imperative language is when you say "how to get what you want".

Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. 

In imperative programming, you use assignment statements to locate some information in memory to use later. The wide use of looping statements allows executing sequences of statements. If you want to check whether some condition is met before performing some actions, you can use conditional branching statements. 

Example of program,

nums = ["камень", "ножницы", "бумага"]
var = []
for i in range(2):
    var.append(input())
if var[0] == var[1]:
    print("ничья")
elif var[0] == nums[0] and var[1] == nums[1] or var[0] == nums[1] and var[1] == nums[2] or var[0] == nums[2] and var[1] == nums[0]:
    print("первый")
elif var[0] == nums[0] and var[1] == nums[2] or var[0] == nums[1] and var[1] == nums[0] or var[0] == nums[2] and var[1] == nums[1]:
    print("второй")

Examples of imperative languages: C++, PHP, C#, Pascal, Python, Java, ... 

 

Difference between declarative and imperative programming

  Declarative Programming Imperative Programming

 

Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.

Imperative programming is a programming paradigm that uses statements that change the program’s state.
Main Focus Declarative programming focuses on what the program should accomplish. Imperative programming focuses on how the program should achieve the result.
Flexibility Declarative programming provides less flexibility. Imperative programming provides more flexibility.
Complexity Declarative programming simplifies the program. Imperative programming can increase the complexity of the program.
Categorization Functional, Logic, and Query programming fall into declarative programming. Procedural and Object-Oriented programming fall into imperative programming.
Order of instructions Execution is not clearly delineated. A program is made up of a clearly defined sequence of instructions to a computer.

Sources: differencebetween.com

Comparing the two approaches

Traditionally, most developers have used the imperative approach to write web services. This meant using service and operation contracts to create the service, along with service implementation. However, much custom development is neither purely declarative nor imperative. It contains a mixture of both, although it generally shows a preference for one or the other. The development of services certainly adheres to this pattern. For example, if a developer uses an imperative approach to implement a service, the logic may include if statements or other conditional logic, but the program may still include a SQL query, which is considered declarative.

The following table compares service development in terms of the imperative and declarative approaches.

Declarative Approach Imperative Approach

The control flow is easier for a business analyst to understand.

Small services are easier to implement.
It is difficult to create unit tests. It is easier to create unit tests.
Many developers must understand new technology. It is a familiar model for many developers.
Changes may require only that you deploy a new XAML file. Changes require that you deploy new assemblies.

Questions:

  1. What are declarative programming and imperative programming?
  2. How do declarative and imperative programming differ in complexity?
  3. Explain what "Execution has not clearly delineated" means.
  4. Give two examples of declarative PL and imperative PL.

Exercises:

Ex. 1 Fill in the gaps

Ex. 2 Define programming paradigms


Exam questions:

 

Категория: Programming languages | Добавил: bzfar77 (06.09.2020)
Просмотров: 18452 | Теги: programming language, imperative, declarative | Рейтинг: 4.3/17
Всего комментариев: 0
avatar