Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.2 Declarative Programming

Functional programming takes a declarative approach for writing code. It's a common (and very trendy) way of writing code. We'll examine declarative programming and compare it to imperative programming in this lesson.

2.2 Declarative Programming

One of the ideas behind functional programming is the concept of declarative programming. Now, this is actually a concept that has been permeating the JavaScript community for a very long time. It might not necessarily be in terms of functional programming but if you look at the UI frameworks like Vue or React or Svelte, they all encourage you to build user interfaces using a declarative style of programming. For example this is Vue, and this is the template for a very simple Vue component. But if you look, it looks mostly like HTML, but if you look a little closer you can see that there is some kind of declaration going on. And this actually sets up the click event handler for the button, and this is the JavaScript code that will execute. Now, of course, this is a very simple example and doing something more complex might require you to write a function that handles the click event. But the idea of setting up the event or outputting information is all part of a declarative style of programming. Well, with functional programming, it's not very different. So let's look at this. So let's create a new file, let's call it declarative.js, and let's create an array. We'll just call it numbers, and it doesn't matter what the values inside of this array are, I'm just gonna use some simple values. Now, in typical JavaScript code, we would take an imperative approach, that is essentially telling the computer what to do, here are the steps that you need to go through. And here is the exact way that you're going to do it. So we might use a for loop here and we would say, okay, here's your counter, you're going to count each item within this array and you're going to do so for as long as the counter is less than the length of this array. And you're also going to increment and then from here on out, you're going to access each element using the index and so on and so forth. That is an imperative style of programming. Well, with functional programming, we typically don't do something like that, we rely upon functions to do it for us. So in our particular case, we have an array, and an array has a forEach method, and this accepts a function that will execute for every item in the array. So if we wanted to display each item in the console, then all we would have to do is pass a function that accepts the item and then write that to the console. And so the idea behind declarative programming is so that if you're new to a code base you can get an understanding of what's going on far quicker than if you were dealing with imperative code. Because imperative code gets bogged down in the details like our for loop. Whereas declarative code is much cleaner, and it reads much like how we would say it. Although we can actually improve upon this. Let's say that we had a function called output that accepts the item and it simply writes that item to the console. Well, then we could just use this function instead of an anonymous function. And that makes it far more easier to understand what's going on. And then we could take it a step further, we might have a for each function that we would then pass in our callback. And then we would pass in the array of numbers so that this even reads much better for each item we're going to output these numbers. And so when it comes to functional programming, we tend to stay away from these control structures, which are basically every type of loop. So, for, while, do, while and so on. We also tend to stay away from if statements or if else. We might also stay away from switch statements. Instead of if-else we might use a ternary, so that if we needed to make a decision, it would be a very simple operation. So it might look something like this. If something is true then we have this value, otherwise, it's that value. So the idea is that we have this very concise, very easy to read code broken up into small functions that we would then build larger applications with. Now, it was saying that there are some concepts in functional programming that well, it takes a little bit to wrap your head around. However, once you have the concepts down, understanding an application that's written using a functional approach is much easier to understand, and it's all due to declarative programming.

Back to the top