An if statement where both branches do nothing but return or write to a variable can be better expressed using the ternary ? operator.

Use of the ternary operator enhances readability in two ways:

Consider using a ternary operator in this situation.

The following code includes two examples of if statements, myAbs1 and 1, which can be simplified using the ternary operator. myAbs2 and s2 show how the statements can be improved.