Several considerations may apply, depending on whether the conditions are mutually exclusive or not.
If the conditions are mutually exclusive, the simplest solution is to group them by their return value:
if (condition1 || condition3 || condition4) return 1;
if (condition2 || condition5) return 2;
return 3;
If the conditions are not mutually exclusive, you can group together only the ones that are next to each other in the testing order:
if (condition1) return 1;
if (condition2) return 2;
if (condition3 || condition4) return 1;
if (condition5) return 2;
return 3;
Note that although you do not need an else
after a return, it is common in some shops to include an else
anyway. This is a matter of coding standardization which should be decided together by your coding team.
If you would like to get really fancy, you can create an array of blocks, test conditions in order, and return the value returned by the block. This is by far the trickiest approach. It would significantly impair readability, unless the conditions are structured in the same way, and the code makes sense to the reader. Chains of if
s always make sense to a reader, so it is very hard to compete on readability with them.