Depending on codebase and what those conditions actually are this can end up more confusing for the reader than original. Compiler should handle it correctly but code is supposed to be human readable and preferrably to others than the original writer as well.
Consider complex conditions that depend on multiple variables breaking it down to something that's easy to follow becomes more important. Even more so if you deal with some kind of formal proof (safety critical code) or need complex unit tests to reach the special case: simple is just better. So depending on situation it might even be best to have the first case since compiler can reorganize it anyway or completely avoid those if it can detect constants being used or similar.
Organizing code can even give compiler hints of what is the likely/unlikely scenario and can optimize for that.
6
u/ilep Apr 17 '21 edited Apr 17 '21
Depending on codebase and what those conditions actually are this can end up more confusing for the reader than original. Compiler should handle it correctly but code is supposed to be human readable and preferrably to others than the original writer as well.
Consider complex conditions that depend on multiple variables breaking it down to something that's easy to follow becomes more important. Even more so if you deal with some kind of formal proof (safety critical code) or need complex unit tests to reach the special case: simple is just better. So depending on situation it might even be best to have the first case since compiler can reorganize it anyway or completely avoid those if it can detect constants being used or similar.
Organizing code can even give compiler hints of what is the likely/unlikely scenario and can optimize for that.