# Discussion on: The Power of Variable Names

Julien Dephix

Despite typos here and there this is a nice article.

Good variable naming is hard but important and when done correctly there is no need for comments.

Function/method names are equally important.
For example when you do this:

``````// Compute roots of a quadratic equation.
// This assumes that (b^2-4*a*c) is positive.
let temp = Math.sqrt(b^2 - 4*a*c);
root[0] = (-b + temp) / (2 * a);
root[1] = (-b - temp) / (2 * a);
``````

You can get create a function called `getQuadraticRoot` and get rid of the first comment because we now know what the function does.
Same goes with the second one by adding an `if`:

``````function getQuadraticRoot(a, b) {
const discriminantSquare = b^2 - 4*a*c;
if (discriminantSquare <= 0) {
return null; // or whatever makes sense, I’m no quadratic root expert!
}

const discriminant = Math.sqrt(discriminantSquare);
root[0] = (-b + discriminant) / (2 * a);
root[1] = (-b - discriminant) / (2 * a);

return root;
``````

We could then argue about `discriminant` which could well be called `d` as it does not really help understand the code: it’s an official formula. `E=mc2` won’t be easier to grasp with good variable names. ;)

One more thing. You mention convention but you list `teamPointsMax`, `maxPoints` as just right. Convention could be to prefix with `max` so choices should be `maxTeamPoints` and `maxPoints`.

Lastly, you do not need `let` when filling arrays in a loop.

Again, nice article!

Wow, thanks for the advice and additional knowledge. I need to learn more. After I reread it, I realized I was careless with lots of typos and wrong implementation. Thank you for being reminded. I have very little experience so I am very grateful to be able to listen to experienced people. Thank you so much for taking the time 🙏

Julien Dephix

Glad I can be of any help.

Good variable names is one thing but it's just one step.

``````function getFlaggedCells() {
let flaggedCells = [];
for (let cell in gameBoard) {
if (cell[STATUS_VALUE] == FLAGGED)
}
return flaggedCells;
}
``````

1. it's not a pure function since gameBoard is defined outside of the function.
2. it can be refactored in a more functional way
``````function getFlaggedCells(gameBoard) {
return gameBoard.filter(cell => cell[STATUS_VALUE] === FLAGGED)
}
``````