I made the case to coworkers that deep levels of control flow was harmful to the readability of code.
Example, taken from the relevant stack overflow question https://softwareengineering.stackexchange.com/questions/52685/if-you-need-more-than-3-levels-of-indentation-youre-screwed:
for(int i=0; i<10; ++i){
Object val = repeat(i, someVar);
if(val.value > 3){
switch(val.item){
case DOG:
if(mProcess){
outputToUser(val);
doMoreThings(val, mMoreThingDoer);
if(mRepurpose){
addExample(val);
}
// and so on, and so on...
As with most things, it’s trivially easy to find opinions on this topic.
I’m wondering however if someone can contribute more to it than that.
Has there for example been done an actual study relevant to the problem?
Or can other arguments be made that go beyond “I like X better”?
3
Quick googling shows that some research has been done. For instance, this paper shows that there’s a value of cyclomatic complexity of code that minimizes bug rate:
Probably deep nesting may be fine as long as it does not branch at every point. That is, a having many nested conditions on top, as in your example, is probably fine, since it’s essentially one condition, just not written as a conjunction.
OTOH if your switch statement below is large and has seriously non-trivial branches, the ‘you’re screwed’ clause may apply.