The most common way of programming a computer is to give it a list of instructions. The computer will then go through each instruction step-by-step until the program finishes.
This is called imperative programming.
It’s like you’re explaining your favourite pasta recipe to someone who has never cooked in their life.
You can’t just tell them the ingredients for the sauce and expect them to produce something delicious. They need to know every single step in the most detail possible:
Slice the mushrooms
Boil the pasta
If you leave any detail out, they’ll probably make a mistake and ruin the dish
But what if you were explaining this recipe to someone who has experience cooking?
Your instructions would look like this instead:
You don’t need to specify how to do each of those things, because the chef knows that. All you need to do is to tell them what they are making.
This is called declarative programming.
Instead of specifying every single step of the program, you only need to tell the computer what should happen, and it knows how to handle the rest.
We have a component here that takes in a list
prop, but always renders just the top 3 items:
export default {
props: {
list: {
type: Array,
},
},
data() {
return {
topThree: [],
};
},
watch: {
list: {
// Run as soon as the component loads
immediate: true,
handler() {
// Recalculate and set the `topThree` variable
this.topThree = this.getTopThree(this.list);
},
},
},
methods: {
getTopThree(list) {
// Sort the array
list.sort();
// Return first 3 items
return list.slice(2);
}
}
}
Our component state topThree
will always contain the top three items in the list, no matter how the list
prop changes.
Step-by-step, this is what it’s doing: 1. If the list
prop changes, our watcher fires 1. We pass this.list
to our getTopThree
method to calculate the top 3 items 1. We take those items and update our component state
We’re following an imperative approach here, by specifying exactly what needs to happen.
Actually, our watcher is somewhat declarative, because we don’t need to write any logic to check when props update. Vue just runs the function for us when the list
prop changes.
But if we switch to using a computed prop, we end up with a more declarative approach. You’ll also see that it’s a simpler and more understandable way of writing this functionality.
To switch this to a computed prop we’ll first get rid of the watcher completely:
export default {
props: {
list: {
type: Array,
},
},
data() {
return {
topThree: [],
};
},
methods: {
getTopThree(list) {
// Sort the array
list.sort();
// Return first 3 items
return list.slice(2);
}
}
}
We can also get rid of our data
section as well:
export default {
props: {
list: {
type: Array,
},
},
methods: {
getTopThree(list) {
// Sort the array
list.sort();
// Return first 3 items
return list.slice(2);
}
}
}
Now we can convert the method into a computed prop that we’ll call topThree
:
export default {
props: {
list: {
type: Array,
},
},
computed: {
topThree(list) {
// Sort the array
list.sort();
// Return first 3 items
return list.slice(2);
}
}
}
However, computed props can’t accept any arguments. Instead we’ll just grab the list
prop directly from inside the computed prop:
export default {
props: {
list: {
type: Array,
},
},
computed: {
topThree() {
// Copy the list to avoid side effects
const copy = this.list.slice();
// Sort the array
copy.sort();
// Return first 3 items
return copy.slice(2);
}
}
}
As we saw in Lesson 2.2 — Mutating an Array, we have to make a copy of this list first so we don’t accidentally modify it and produce a confusing side effect.
Here are some of the benefits that we can already see just from looking at this small illustration:
I think you’d probably agree with me that the approach with computed props is easier to understand as well.
In this lesson we explored what declarative code is, and saw that one of the reasons why people love Vue so much is because it allows us to write our code more declaratively.
We also saw that computed props are the main way that we can convert our imperative code to be more declarative, and saw that there are several benefits to writing code this way.
Because of this, computed props are the most important feature in Vue in my opinion — hopefully I’ve convinced you.
Thank you for reading!
#vue #vue-js #code #developer #programming