


























Ever feel like your codebase is a hoarder’s paradise? You’re not alone. I’ve recently picked up a habit that’s been a game-changer for my projects: the subtract day. It’s exactly what it sounds like – one day a week (or fortnight) where I focus not on adding features, but on subtracting parts of the code or requirements that have become stale or could be deprioritized.
In his book “Subtract: The Untapped Science of Less”, Leidy Klotz argues that our default approach to problem-solving is addition. We’re hardwired to add more features, more code, more complexity. But often, the best solution is to take something away.
Think about it: when was the last time you deleted a chunk of code and felt that sweet, sweet dopamine hit? It’s like decluttering your digital closet – oddly satisfying and surprisingly productive.
Here’s how I structure my subtract days:
// Before subtraction
function processData(data) {
let result = [];
for (let i = 0; i < data.length; i++) {
if (data[i] % 2 === 0) {
result.push(data[i] * 2);
} else {
result.push(data[i]);
}
}
return result;
}
// After subtraction
const processData = data => data.map(num => num % 2 === 0 ? num * 2 : num);
// Performance test
const largeDataSet = Array.from({ length: 1000000 }, () => Math.floor(Math.random() * 100));
console.time('Before');
processData(largeDataSet);
console.timeEnd('Before');
console.time('After');
processData(largeDataSet);
console.timeEnd('After');
Less code often means faster execution. In the example above, we’ve simplified our processData function, making it more concise and potentially faster for large datasets.
Fewer lines of code mean fewer places for bugs to hide. It’s like having a smaller house – less to clean, less to maintain.
By removing unnecessary features or code, what remains often has a clearer purpose. It’s easier to understand and explain to new team members.
Less code means less mental overhead. You’re not juggling unnecessary complexity, allowing you to focus on what really matters.
Klotz points out in “Subtract” that we have a strong bias towards addition. It feels productive to add new features or write more code. But sometimes, the most productive thing you can do is take something away.
Here are some strategies to overcome this bias:
The subtract day isn’t just about tidying up your codebase (though that’s a nice benefit). It’s about challenging our instinct to always add more. It’s about recognizing that sometimes, less really is more.
So, next time you’re tempted to add a new feature or write more code, pause and ask yourself: “Could I subtract instead?” Your future self, digging through that codebase six months from now, will thank you.
Remember, as Klotz says in “Subtract”, “When we subtract, we often make room for more of something we want.” In the case of our code, that something might just be clarity, performance, and maintainability.
Now, if you’ll excuse me, I’ve got some code to delete. Happy subtracting!
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。