This is the code:
public noArtistBeingEdited(): boolean {
if (this.isFirstNameBeingEdited()) {
return false;
}
if (this.isLastNameBeingEditable()) {
return false;
}
return true;
}
How can I simplify it?
Use an OR (||) operator:
public noArtistBeingEdited(): boolean {
if (this.isFirstNameBeingEdited() || this.isLastNameBeingEditable()) {
return false;
}
return true;
}
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
OR(||) the first two conditions, and leave it otherwise. It keeps this very clear.