I have a piece of code:
if (foo > bar) {
baz = foo - bar
} else {
baz = foo + bar
}
I have a question if I can somehow shorten this code to a single line, something like
PSEUDOCODE:
baz = foo (foo > bar ? + : -) bar
Real code I'd like to shorten
if (gradientStartH > gradientEndH) {
h = gradientStartH - Math.abs(gradientStartH - gradientEndH) / arr.length * i
} else {
h = gradientStartH + Math.abs(gradientStartH - gradientEndH) / arr.length * i
}
Thanks!
I have a piece of code:
if (foo > bar) {
baz = foo - bar
} else {
baz = foo + bar
}
I have a question if I can somehow shorten this code to a single line, something like
PSEUDOCODE:
baz = foo (foo > bar ? + : -) bar
Real code I'd like to shorten
if (gradientStartH > gradientEndH) {
h = gradientStartH - Math.abs(gradientStartH - gradientEndH) / arr.length * i
} else {
h = gradientStartH + Math.abs(gradientStartH - gradientEndH) / arr.length * i
}
Thanks!
Share Improve this question edited Mar 8, 2019 at 20:17 Pavel T asked Mar 8, 2019 at 19:24 Pavel TPavel T 976 bronze badges 3- 3 If the operation is "much more plicated", a ternary should be avoided, for readability and maintainability reasons – Taplar Commented Mar 8, 2019 at 19:29
-
baz = (foo > bar) ? (foo - bar) : (foo + bar)
? – el-teedee Commented Mar 8, 2019 at 19:35 - @Taplar maybe you are right! But please take a look on real code example that I'd like to "enhance" – Pavel T Commented Mar 8, 2019 at 19:42
6 Answers
Reset to default 4You could convert the check to a number or take -1
as factor.
baz = foo + (foo > bar || -1) * bar
The cleanest approach is to use an object with the operands and a check for getting the operands.
op = {
true: function (a, b) { return a + b; }, // add
false: function (a, b) { return a - b; } // sub
}
baz = op[foo > bar](foo, bar);
You can remove the if-else
and Math.abs
to just this:
h = gradientStartH - (gradientStartH - gradientEndH) / arr.length * i
Here's a snippet paring it with your code:
// Your code
function getDiffExisting(gradientStartH, gradientEndH, arr) {
let h = 0;
if (gradientStartH > gradientEndH) {
h = gradientStartH - Math.abs(gradientStartH - gradientEndH) / arr.length
} else {
h = gradientStartH + Math.abs(gradientStartH - gradientEndH) / arr.length
}
return h;
}
console.log(getDiffExisting(200, 100, [1,2]))
console.log(getDiffExisting(50, 100, [1,2]))
function getDiffNew(gradientStartH, gradientEndH, arr) {
let h = gradientStartH - (gradientStartH - gradientEndH) / arr.length
return h;
}
console.log(getDiffNew(200, 100, [1,2]))
console.log(getDiffNew(50, 100, [1,2]))
(I have removed the i
for testing purposes)
baz = foo > bar ? foo - bar : foo + bar;
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
EDIT:
I understand your specific question. I'm pretty sure I'm going to get lynched for saying this, but you could use eval()
to evaluate it as a string. Not remend, if any of the below derives via unsantized user data.
h = eval(`${gradientStartH} ${gradientStartH > gradientEndH ? '-' : '+'} ${Math.abs(gradientStartH - gradientEndH) / arr.length * i}`);
Otherwise a decent two liner. Preferred.
const absVal = Math.abs(gradientStartH - gradientEndH) / arr.length * i;
h = gradientStartH > gradientEndH ? gradientStartH - absVal : gradientStartH + absVal;
You were almost there. foo - bar
could be written as foo + -bar
. So the pseudo code could be written as:
baz = foo + (foo > bar ? +1 : -1) * bar
Something like this,
baz = foo > bar ? foo - bar : foo + bar
You could use a ternary operator, as you attempted in your original answer. The syntax for a ternary operator should be condition ? true : false;
baz = foo > bar ? foo-bar : foo+bar;
You mentioned that it "might be much more plicated than this" which does not allow much clarity for a solution. Ternary operators likely will not work depending on the plexity. You can read more about ternary operators here.