Skip to content

Deep Nesting

Tstream edited this page Apr 26, 2017 · 1 revision
  • too many levels of nesting are make code hard to read.
  • most of the time can rearrange code to reduce nesting.

Deep Nesting Example 1:

public boolean do_stuff() {
    if (this_thing) {
        if (and_this) {
            if (also_this) {
                if (aswell_this) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        } 
    } else {
        return false;
    }
}

Deep Nesting Example 2:

public int do_stuff() {
    if (this_thing) {
        if (and_this) {
            if (also_this) {
                if (aswell_this) {
                    return 1;
                } else {
                    return 2;
                }
            } else {
                return 3;
            }
        } else {
            return 4;
        } 
    } else {
        return 5;
    }
}

Deep Nesting Solutions

Clone this wiki locally