Skip to main content
added 36 characters in body
Source Link
John Wu
  • 26.9k
  • 10
  • 68
  • 92

If you are attempting to optimize for readability and not optimizing for cleverness or performance or brevity, this is the way I would write it.

String getMyString(String parameter1, String parameter2) {
    if (parameter1 == optionA && parameter2 == optionX) return "STRING_A.X";
    if (parameter1 == optionA && parameter2 != optionX) return "STRING_A.Y";
    if (parameter1 == optionB && parameter2 == optionX) return "STRING_B.X";
    if (parameter1 == optionB && parameter2 != optionX) return "STRING_B.Y";
    return "STRING_A.Y";
}

This eliminates any confusion and many of the common logic bugs. The clausesconditions are all mutually exclusive and free of sequential dependency; they can be put in any order, and new ones can be added in between, or conditions removed, with little risk of impacting the other conditions. Also, the default value is clearly expressed as a catch-all at the end.

If you are attempting to optimize for readability and not optimizing for cleverness or performance or brevity, this is the way I would write it.

String getMyString(String parameter1, String parameter2) {
    if (parameter1 == optionA && parameter2 == optionX) return "STRING_A.X";
    if (parameter1 == optionA && parameter2 != optionX) return "STRING_A.Y";
    if (parameter1 == optionB && parameter2 == optionX) return "STRING_B.X";
    if (parameter1 == optionB && parameter2 != optionX) return "STRING_B.Y";
    return "STRING_A.Y";
}

This eliminates any confusion and many of the common logic bugs. The clauses can be put in any order, and new ones can be added in between, or conditions removed, with little risk of impacting the other conditions. Also, the default value is clearly expressed as a catch-all at the end.

If you are attempting to optimize for readability and not optimizing for cleverness or performance or brevity, this is the way I would write it.

String getMyString(String parameter1, String parameter2) {
    if (parameter1 == optionA && parameter2 == optionX) return "STRING_A.X";
    if (parameter1 == optionA && parameter2 != optionX) return "STRING_A.Y";
    if (parameter1 == optionB && parameter2 == optionX) return "STRING_B.X";
    if (parameter1 == optionB && parameter2 != optionX) return "STRING_B.Y";
    return "STRING_A.Y";
}

This eliminates any confusion and many of the common logic bugs. The conditions are all mutually exclusive and free of sequential dependency; they can be put in any order, and new ones can be added in between, or conditions removed, with little risk of impacting the other conditions. Also, the default value is clearly expressed as a catch-all at the end.

Source Link
John Wu
  • 26.9k
  • 10
  • 68
  • 92

If you are attempting to optimize for readability and not optimizing for cleverness or performance or brevity, this is the way I would write it.

String getMyString(String parameter1, String parameter2) {
    if (parameter1 == optionA && parameter2 == optionX) return "STRING_A.X";
    if (parameter1 == optionA && parameter2 != optionX) return "STRING_A.Y";
    if (parameter1 == optionB && parameter2 == optionX) return "STRING_B.X";
    if (parameter1 == optionB && parameter2 != optionX) return "STRING_B.Y";
    return "STRING_A.Y";
}

This eliminates any confusion and many of the common logic bugs. The clauses can be put in any order, and new ones can be added in between, or conditions removed, with little risk of impacting the other conditions. Also, the default value is clearly expressed as a catch-all at the end.