re: Nested Conditional Operators VIEW POST


I think that if I were to run across this while digging through code, I would be much happier to just see

const validateInput = ({ field1, field2, field3 }) => {
    if (!field1) {
        return Promise.reject('Missing field1');
    if (!Array.isArray(field2)) {
        return Promise.reject('field2 is not an array');
    if (!isValidType(field3)) {
        return Promise.reject('field3 is invalid');
    return Promise.resolve();

If definitely doesn't win any code golf or cleverness competitions, but it imposes an absolute minimum of cognitive load on someone passing through the code.

Code of Conduct Report abuse