One thing from the article “How to Write Clean Code – Tips and Best Practices (Full Handbook)” by Germán Cocca, that I want to start doing more often is efficiency. Understanding and getting better at algorithmic complexity to run a program with the most efficient resource usage in terms of time and space. For example, the following two codes:
// Inefficient Example
function sumArrayInefficient(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
// Efficient example
function sumArrayEfficient(array) {
return array.reduce((a, b) => a + b, 0);
}
The second one is more efficient because it only needs one iteration over the array and performs a summing operation on each element as it goes.
One thing I want to avoid doing is “Shotgun Surgery,” a code smell I learned from “Everything you need to know about Code Smells” by codegrip, which is when one change requires altering many different classes. I want to avoid this code smell because it leads to a high risk of errors and makes the system hard to maintain.
Leave a Reply