Declarative vs imperative programming

Choosing a career in programming is one of the most lucrative deals one can pursue in this age of technology. This is pretty evident by the number of students there are enrolling on various programming courses, degrees or choosing other ways of learning programming online. There is a huge community, demand and every bit of luxury this career can provide you with is only to be experienced through either subjective experience or watching others.

Confusing terms and approaches of programming

It is easy to get into programming but takes a great deal of effort, dedication and commitment to keep yourself to the top of your game. Getting the fundamentals is the best move you can make if you are just beginning. For those, who have already got their fundamentals right and somewhat floating in between with the floating points, this article will be of a great use especially if you are looking for the exact explanation of imperative and declarative programming.

You might have come across these two terms often used by the mentors or seniors interchangeably. This may leave you in confusion, but here is this reading might save you some great deal on the way.

Declarative programming

So, what is declarative and imperative programming? Let’s dive in. These two are basically the way you would a write your code blocks based on the language and your projects need. Declarative programming is something, where you have to pedantically write every details to instruct the computer for your desired purpose. Mind boggled? Okay, let me break it down a further.

Declarative programming is the approach making your code look like step-by-step instructions. Let’s say, we want have a string variable called ‘text’ where we store some sort of string input, maybe from a user. Now, we are to make a copy of the string and store it in another variable, called ‘copy’ and assign the ‘text’ variable to it.

Now, to see the surprise, if we make the first letter of the  ‘copy’ to uppercase and print out both ‘text’ and ‘copy’, we will see that one of them has changed while the other remained unchanged. In this case, the first letter of the ‘copy’ has been changed but the ‘text’ has not been changed. This is so, because, such languages as C in nature, require a declarative approach. I am not going to dive into the solution here by code, but I am going to tell you the solution is to use a function in the way in which one has to point both ‘copy’ and ‘text’ to the memory location.

Imperative programming

As you can imagine, this is not the same for imperative programming.

With the imperative approach a lot of these pedantic details has been abstracted away for the user of high level programming language like JavaScript, Python and others. To give you the spoiler, I have just given you another great concept of abstraction here. It would be wise to remember this concept.

So, if we look at the same copy text example from above, we could simply go ahead and use some sort of an already defined function ready for the programmers to use, where they do not have to worry about the nitty-gritty of low level details. With programming languages like JavaScript, we could simply do this with the help of prototyping and knowledge of inheritance.

So there it is, this is the simplest way to understand this concept. Happy coding!