0

This is the code:

public noArtistBeingEdited(): boolean {
    if (this.isFirstNameBeingEdited()) {
        return false;
    }
    if (this.isLastNameBeingEditable()) {
        return false;
    }
    return true;
}

How can I simplify it?

1
  • I'd just OR (||) the first two conditions, and leave it otherwise. It keeps this very clear. Commented May 5, 2020 at 23:14

3 Answers 3

4

Use an OR (||) operator:

public noArtistBeingEdited(): boolean {
    if (this.isFirstNameBeingEdited() || this.isLastNameBeingEditable()) {
        return false;
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also, this code can be shortened to: return !(this.isFirstNameBeingEdited() || this.isLastNameBeingEditable());
0

You could simplify by going step-by-step, so initially you can reduce the two statements into one by merging them together. Since they return the same, os either the first or last need to be true in order for it to return false.

public noArtistBeingEdited(): boolean {
    if (this.isFirstNameBeingEdited() || this.isLastNameBeingEditable()) {
        return false;
    }
    return true;
}

Then you can see that if the whole this.isFirstNameBeingEdited() || this.isLastNameBeingEditable() is true then it will return false. So you could just put the whole statement inside brackets as it acts as one.

(this.isFirstNameBeingEdited() || this.isLastNameBeingEditable()) === false

So now you know that if you invert that whole statement then you'll get true, so you can just

!(this.isFirstNameBeingEdited() || this.isLastNameBeingEditable())

The above means that they need to be both false for the function to return true:

let fn = (a, b) => {
  if (a) {
    return false;
  }
  if (b) {
    return false;
  }
  return true;
};


console.log(fn(true, true)); // false
console.log(!(true || true)); // false

console.log(fn(false, false)); // true
console.log(!(false || false)); // true

console.log(fn(false, true)); // false
console.log(!(false || true)); // false

console.log(fn(true, false)); // false
console.log(!(true || false)); // false

Comments

-1
public noArtistBeingEdited(): boolean {
    return !this.isFirstNameBeingEdited() && !this.isLastNameBeingEditable()
}

Your expresion reduces to neither of the two checks i.e. NOT A AND NOT B

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.