Declarative and imperative programming languages

12.5.1.1  compare declarative and imperative programming languages 

Declarative and imperative programming languages

Remember

A programming paradigm is a fundamental style of computer programming. 

We consider two paradigms: imperative vs declarative.

Declarative languages

Declarative programming is when you say what you want

Declarative languages are often used by intelligent systems. Declarative languages are very different to the other types of language because of the way that the language is structured. Programmers will create a knowledge base. A knowledge base contains a series of related facts and rules.

Example of facts and rules,

Fact: spouse (john, jane)
Fact: spouse (david, mary)
Fact: spouse(george, susan)
Fact: female (jane)
Fact: female (mary)
Fact: female (susan)
Fact: male (john)
Fact: male (david)
Fact: male (george)
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)? 

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

Imperative languages

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 it 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 changes 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, Query programming falls into declarative programming. Procedural and Object Oriented programming falls 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 a 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. 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 or a LINQ query, which are 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 a 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 is declarative programming and imperative programming?
  2. How do declarative and imperative programming differ in complexity?
  3. Explain what "Execution is not clearly delineated" means.
  4. Give two examples of declarative PL and imperative PL.

Exercises:

Ex. 1

Ex. 2

 

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