What is less code anyway? What do we really mean when we use the term "less code"? How can we even achieve this? Does writing less code make you a better developer at the end of the day? All these and more we'll find out in this piece. Let us first establish what mean by the term "less code".
When you see the term less code and you think of shortening your variable name, using dynamic arrow functions or some form of weird gimmick because somehow it reduces the amount of broiler plate we have to maintain, then you missed it a bit. Less code is not the same thing as writing code that's unreadable and difficult to understand. Someone should not have to prompt an AI with your code to understand how it works.
Less code in this context refers to utilizing the best strategy to execute a logic such that no matter how much complexities that would be added, the entire system would be able to accommodate for and process said logic handling and said complexities without changing much, if at all.
I'm talking about an approach to writing software such that it can be dynamic and efficient enough to handle different and new situations without breaking.
Now we have established what "less code" is, let's see how we can write "less code".
Consider a branching situation where we handle decision making using a conditional statement. Our language of choice here is JavaScript and there are two types of conditional statement available to us in Javascript.
We have the if-else statement and the switch statement to handle conditionals in Javascript. Supposed we have an Object car with property wheels defined on it, we need to implement a certain logic for different values that wheels can be. The normal approach would be as follows
const MakeCar = (brand, wheels) => {
return {
brand,
wheels
}
}
const car = MakeCar("mercedes", 4);
if (car.wheels === 4) {
console.log("has 4 wheels")
}
Okay let's say our logic has been updated and we need to check the car brand and run custom logic for each brand. We would have to update our if check and run custom logic for each brand.
if (car.brand === "mercedes") {
console.log("Mercdes car")
}
You are starting to see where this is going, for each check we have to make, we will need to add an if statement. This is not super intuitive. If the complexities keep growing our code will easily become a nightmare to manage. Even if we do manage to keep up, this is going to be a nightmare for new developers working on this.
What if we change the if statement to an else statement, would we fare any better? I highly doubt that but let's explore the approach together.
switch (car.brand) {
case "mercedes":
console.log("mercedes car")
break;
case "toyota":
console.log("toyota car")
break;
default:
break;
}
The basic setup is looking manageable for now but the same problem we got with the if statement has also reproduced itself in the switch statement with even bigger consequences, with a switch statement we can only run a check for one property or value, to do some custom logic for the wheels we have to use another switch statement and this approach is starting to look a bit stressful to manage.
What then is a proper approach to this problem? There isn't any absolute approach to solving this problem but we can combat this problem of growing complexities. How? Let's create an object literal that will store the various conditions we will check for as a key. While the value for each key will the a function that houses the logic for that condition.
const conditions = {
mercedes: (car) => {
console.log(car.brand)
},
toyota: (car) => {
console.log(car.brand)
},
}
if (conditions[car.brand]) {
conditions[car.brand](car)
}
What is unique about this implementation? We don't need to add multiple if statements for a new logic, overall the code is more cleaner and pleasant to read. The code above is very easy to manage. Although the complexities of the conditions we are checking could always keep increasing however we'd always be on top of the situation, we don't have to worry about multiple if's statement. There's just one, we can inspect the object properties to have an idea of what's happening.
That's it for this piece guys, leave your thoughts in the comment section below, hope you found this super useful. I'll see you in the next post.
Top comments (0)