Thursday, April 26, 2018

Declarative and Imperative programming paradigms

Declarative programming is all the rage these days, but I wonder how many people understand that when they're writing in this style, they are writing to an abstraction layer. These two paradigms are not mutually exclusive, and understanding both will make you a better programmer.

As we all learned during our first forays into computers, they are just machines, and they only do exactly what you tell them to do, how you tell them to do it. I emphasize that last point, as that is what imperative programming is. Understand that imperative programming is the basis for everything built on top of it. Computers must be told exactly how to do anything you want them to do. Declarative programming is built on top of this foundation. Imperative programming deals with the nitty-gritty details of how to loop over an array, or IEnumerable. Declarative programming just says I want to loop over this thing (whatever it is). The declarative must at some layer betranslated to the imperative in order for computers to work.

Don't get me wrong, I love writing imperative code, who wants to write quicksort for the 10000th time, or a binary tree, etc. Declarative code makes all our lives as programmers easier, but understand that it's foundations are built upon imperative code. While it's great to write that declarative code for iterating over an array by just writing array.forEach(x=>x = x+1), understand that someone wrote the imperative code to actually do the looping over the array, and it would be great if we could inspect all the imperative code that we're depending on to make sure that it's well written and that we understand all the implications of the declarative code we're writing.

No comments:

Post a Comment