This if
construct
if (propA === propB) { var array = props; array.shift(); return props.length ? comparePropsOfAWithB(a, b, props) : false } else if (propA < propB ) { return true } else { return false }
can be written in pseudocode like this
if (condition) {
return boolean;
} else if (otherCondition) {
return true;
} else {
return false;
}
if the first condition is true, the else if
won't be reached so we can rewrite it like
if (condition) {
return boolean;
}
if (otherCondition) {
return true;
} else {
return false;
}
now the last if...else if
boils down to if otherCondition
is true return true
otherwise return false
. As the otherCondition
evaluates to a boolean
we can simply return this condition like so
if (condition) {
return boolean;
}
return otherCondition;
which leads for your code to
function comparePropsOfAWithB(a, b, props) {
var prop = props[0];
var propA = a[prop];
var propB = b[prop];
if (propA === propB) {
var array = props;
array.shift();
return props.length ? comparePropsOfAWithB(a, b, props) : false
}
return propA < propB;
}
which is much simpler and easier to read, but taking into account the answeranswer of @200_success@200_success this won't help you, because your assumptions regarding the returnvalue of a compare function is wrong.
You should use descreptive meaningful names for naming variables, methods and classes. You shouldn't use abbreviations for naming because this reduces the readability of your code.
So instead of propA
and propB
something like firstItem
and secondItem
or firstProperty
and secondProperty
would be better.