Can You Understand This Code In One Read? - Refactoring Notes #1
I've got three fairly short pieces of code for you. Can you understand them in 15 seconds?
Basically as your read the code, no extra effort required, without making any charts, or debugging the logic. If you have to spend more than a minute on each, just move on and keep reading.
Snippet #1
function lonelyinteger(a) {
let resultado = [];
a.sort(function (a, b) { return a - b; });
for (let i = 0; i < array.length; i++) {
while (array[i] === array[i + 1]) {
array.splice(i, 2);
}
}
return resultado[0];
}
So, how was it? A minute ran out and you got what this function does? I dare to say you didn't... let's move on to snippet 2.
Snippet #2
function lonelyinteger(a) {
let lonelyCandidates = [];
for (let i = 0; i < a.length; i++) {
if (lonelyCandidates.includes(a[i])) {
let index = lonelyCandidates.indexOf(a[i]);
lonelyCandidates.splice(index, 1);
} else {
lonelyCandidates.push(a[i]);
}
}
return lonelyCandidates[0];
}
You probably got it, after all it's not that complex, but you still had to put that extra effort in following the logic, no matter how long you've been coding, and it may have taken you more than 15 seconds. And surely if you didn't know JavaScript you had to even make a little more effort on top.
Now check this one out... can you get it in one read?
Recommended by LinkedIn
Snippet #3
function lonelyinteger(numbers) {
let lonelyCandidates = [];
const addCandidate = (candidate) => lonelyCandidates.push(candidate);
const removeCandidate = (candidate) => {
let index = lonelyCandidates.indexOf(candidate);
lonelyCandidates.splice(index, 1);
};
const isDuplicate = (candidate) =>
lonelyCandidates.includes(candidate) ? true : false;
const lonelyNumber = () => lonelyCandidates[0];
const determineLonelyNumber = () => {
numbers.forEach(n => {
if (isDuplicate(n)) removeCandidate(n);
else addCandidate(n);
});
};
determineLonelyNumber();
return lonelyNumber();
}
How did this one go? I kept the names "lonely" not to give the whole functionality away so clearly. All 3 snippets provide the same functionality. This function is way easier to understand, even if you don't know a JavasCript like language, I dare to say even if you don't know programming! After all...
"Good code reads like well written prose."
The proper quote is: “Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control." - Grady Booch author of Object Oriented Analysis and Design with Applications”
If you follow a purely functional approach, you'll be tempted to make each of the little functions pure, hence accepting one more parameter. I consider this may be an overkill in such a simple case. Still, I hope you learned something new which you can apply when writing your own code, and when working on refactoring an old codebase.
This comes from a solution from a HackerRank exercise. I'll leave the function name as it is, since it doesn't give away what it does so clearly. You'll find the challenge at: https://www.hackerrank.com/challenges/three-month-preparation-kit-lonely-integer
What's your scariest refactoring story? Read you in the comments. Thanks for stopping by.