Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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 firstItemand secondItem or firstProperty and secondProperty would be better.

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 answer of @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 firstItemand secondItem or firstProperty and secondProperty would be better.

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 answer of @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 firstItemand secondItem or firstProperty and secondProperty would be better.

added 297 characters in body
Source Link
Heslacher
  • 50.5k
  • 5
  • 81
  • 175

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 answer of @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 firstItemand secondItem or firstProperty and secondProperty would be better.

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.


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 firstItemand secondItem or firstProperty and secondProperty would be better.

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 answer of @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 firstItemand secondItem or firstProperty and secondProperty would be better.

Source Link
Heslacher
  • 50.5k
  • 5
  • 81
  • 175

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.


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 firstItemand secondItem or firstProperty and secondProperty would be better.