mhtien / js-calculator

JavaScript Calculator
https://mhtien.github.io/js-calculator/
0 stars 0 forks source link

Give long conditions descriptive names #4

Open oliverjam opened 4 years ago

oliverjam commented 4 years ago

You've got quite a few complex/non-obvious conditional checks, e.g.

https://github.com/mhtien/js-calculator/blob/d220d0209c55d1f10150d0f5dc68ab42bffe1d78/script.js#L94

You could make these clearer by extracting the conditions to variables with descriptive names. E.g.

const numSecondLast =  !operatorsArray.includes(calcDisplay.slice(-2, -1));
const operatorLast = operatorsArray.includes(calcDisplay.slice(-1));
// ...
} else if (target === "-" && numSecondLast && operatorLast) {
// ...

This makes if statements a bit quicker to scan, and might help others reading your code later more quickly grasp what's happening. I often find if you're writing long comments to explain a line of code you can probably name things more descriptively instead.

mhtien commented 4 years ago

This makes total sense! Thanks for the tip :)

View changes here