DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Discussion on: The Power of Variable Names

Collapse
 
joolsmcfly profile image
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);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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!

Collapse
 
adillaumam profile image
Ahmad Adillaumam • Edited on

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 πŸ™

Collapse
 
joolsmcfly profile image
Julien Dephix

Glad I can be of any help.

Good variable names is one thing but it's just one step.
If I take your example:

function getFlaggedCells() {
    let flaggedCells = [];
    for (let cell in gameBoard) {
        if (cell[STATUS_VALUE] == FLAGGED)
            flaggedCells.add(cell);
    }
    return flaggedCells;
}
Enter fullscreen mode Exit fullscreen mode

It's more readable but

  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)
}
Enter fullscreen mode Exit fullscreen mode

I find it more readable.

Thread Thread
 
adillaumam profile image
Ahmad Adillaumam

Again, I really learned a lot from you, but here I can only say thank you very much to you and thank you for creating this discussion.

Thread Thread
 
joolsmcfly profile image
Julien Dephix

ha no problems. Keep on writing, it opens up discussions. πŸ‘Œ