- Imperative programming
- Declarative programming
- No silver bullet
- The confusing terminology
- How to prepare for this course
We can write almost any code in any language in different ways. You can split the code into functions differently, name variables in different ways, or change the nesting depth of conditional constructs. These are all metrics of code quality and the individual components that make coding style.
However, coding style has almost no effect on the specific task and the solution architecture. We determine these properties by choosing the programming paradigm.
A programming paradigm is a set of ideas about how to solve a problem. We start applying the paradigm even before we start writing code. The code in a particular language merely allows us to write our thoughts in a program with a greater or lesser degree of convenience.
An experienced programmer knows how to:
- Think in the right way and various paradigms
- Apply different paradigms
- Choose the right paradigm for specific tasks
- Write their thoughts in code in different programming languages
We can say that the paradigm is suitable for any programming language when you don't need to battle with the language and change your approach to solve a problem. If the paradigm fits the language, the programmer writes down their thoughts as they are.
Python and most modern high-level programming languages are considered multiparadigms. In other words, you can use many different paradigms. It won't be any less convenient.
C and other low-level languages cannot afford this freedom because code in these languages must be efficient. In these languages, solutions are tied as much as possible to how the task is understood by the computer, not by the developer.
Imperative programming
Let's imagine that we need to solve a complex problem. We cannot solve it in one computer action, so we describe a sequence of steps to be performed.
We break down the broad steps into smaller ones until the interpreter or compiler of the chosen language can understand each step. Simply put, we think through and describe how to achieve the desired result. This description of a sequence of steps is called imperative programming.
Imperative programming involves changing the state of the computer. Large programs constantly modify global and local variables:
- Write things to the disk
- Send and receive from the network
- Run individual subroutines and procedures
Such a code ensures the highest possible efficiency. But there is a disadvantage: it forces the programmer to remember everything that changes with each step.
Most low-level languages assume you'll be coding only in this paradigm — after all, all the computer does is sequentially execute machine language commands and change the memory state. Many high-level languages also involve imperative problem-solving.
Declarative programming
When we work on problems, the first thing we do is imagine the desired outcome. Then, we think of a way to get there. Programmers also use this approach. Sometimes, a programmer can describe the result, and the computer will find ways to achieve it.
Declarative programming works this way. When solving a problem in a declarative style, we describe how the result relates to the input data. We can refine the details until the interpreter has enough information to proceed. By specifying our requirements, we still don't tell the computer exactly how to do the job — we describe the ratio of inputs to outputs more precisely.
Declarative programming is often like writing a formula or combining individual processes into a pipeline that generates output data from input data. We break complicated expressions into simpler ones and divide complex operations into chains of simpler ones. When you break it down, you don't have to think about how the state of the computer will change and how it will store intermediate data.
Writing and reading the code are easier this way, but it complicates the interpreter or compiler itself. And the more difficult it becomes to make interpreters and compilers that will allow the computer to run efficiently.
It is why low-level languages have almost no tools for declarative programming because, for such languages, the main thing is efficiency. However, not every high-level language provides the means to solve any problem strictly declaratively. Usually, we only talk about the elements of declarative programming.
No silver bullet
It's human nature to think in declarative terms. Take mathematics as an example: we know the axioms and theorems, for example:
The square of the hypotenuse is equal to the sum of the squares of the other two sides
We usually reason in these declarative terms. The same thought would go like this imperatively:
Take the first side, multiply by itself, memorize, add, and memorize the result,
and extract the root from the previous result
If our computers were infinitely productive and comprehensible, any code would be declarative.
However, we don't have such efficient computers and cannot describe every task declaratively. There is no perfect solution — a point made by software engineer Frederick Brooks in his famous article «No Silver Bullet».
Since we don't have a perfect solution, we have to be happy with:
- Purely declarative problem-solving in limited subject areas — for example, when working with SQL language and subject-oriented languages
- Declarative style with elements of imperative style
We can use Python to implement declarative subject-oriented languages to suit the task. But that's not all — you can write declaratively with elements of imperative writing. That's what this course is all about.
The confusing terminology
In different sources, you can encounter the term functional style. In such cases, people talk about the programming paradigm, not the coding style. So, focus on the adjective "functional" rather than the noun "style".
There are also variations, like "in a functional way". We need to pay attention to the adjective here, too.
How to prepare for this course
To prepare for the declarative approach, you need to master the basics of programming language syntax, functions, and working with collections. We covered these topics in detail in the following courses:
You can find the correct sequence of courses in the syllabus for the Python Developer.
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.