Imperative And Declarative Programming
Programming history is too old (about half of the century or a little bit more) and lots of experienced and highly talented programmers, engineers and mathematicians are invented new paradigms and methods. Now we will look to imperative and declarative programming paradigms.
When we write code we’re try to solve a problem. There are lots of solutions for every problems and how do you decide which solution will you use? How do you organize your code, when you create new functions, how do you name these functions etc. The imperative/declarative programming paradigms can give you opinions about these behaviours. When you understand difference of these things then you can create more efficient codes.
I can say that, imperative programming answers that question: How to do that. But declarative programming is answers that question: What are you doing? Imperative programming focuses on the path. Declarative programming focuses to the result (target, goal, destination, aim or whatever you want to call). You can’t separate imperative programming from declarative programming but you can organize the solution into these. Declarative programming is dividing imperative blocks and giving name to each of them.
Feature | Declarative | Imperative |
---|---|---|
Thinking style | What are you doing? | How to do that? |
Division | Calls undividable functions | Undividable |
Direction | Top to bottom | Bottom to top |
Role | Manager (boss) | Worker (employee) |
For example a + b is imperative, but sum(a, b) is declarative. Because we’re defining the purpose, not the real job. Real job will be done inside of the function. Declarative programming is sum of the named indivisible blocks and another declarative things. Declarative programming is not concerned with the how, it is concerned with the what. Declarative programming is boss, imperative programming is employee. Declarative programming manages the biggest jobs, imperative programming does the smallest jobs.
You need to divide code to minor blocks which does the real business and they have special name. Declarative programming is something like superset of imperative programming. For example you want to draw a square. You need three functions for this. One function is draw_point(x, y), another is draw_line(x1, y1, x2, y1), another is draw_square(x1, y1, x2, y2). Let us write code:
fn draw_point(x: i32, y: i32) {
// Create point in here, you can't divide this function
// to smalles pieces. Because of this block will be imperative.
}
fn draw_line(x1: i32, y1: i32, x2: i32, y2: i32) {
// There different algorithms for finding necessary points for
// drawing line. If you implement one of these algorithms in here
// than this function will be imperative. If you create different
// functions for each algorithm and invoke them in here this
// will be declarative.
}
fn draw_square(x1: i32, y1: i32, x2: i32, y2: i32) {
draw_line(x1, y1, x2, y1);
draw_line(x2, y1, x2, y2);
draw_line(x2, y2, x1, y2);
draw_line(x1, y2, x1, y1);
// This block is declarative. Because we used all divided pieces and
// we can't divide any other piece of the job.
}
When you use this paradigm then your code will be better structured, better organized, readable, extendible, maintainable and everything”able”.
Divide and conquer methodology is reaches to same target as declarative programming. When you divide tasks to minor pieces then the undividable piece will be imperative, others will be declarative.
If your code can’t be divideable more minor pieces than your code is declarative.
Final word
Don’t get too caught up in the concepts. There is only one important concept: divide and conquer. Other paradigms and concepts are revolves around this. Just write code and refactor it by this concept. That’s it.
Happy coding.
0 yorum