Java Control Flow: To Nest or Not to Nest.

Hey Nonso Biose! Thanks for reading and reaching out. Yes, if-elseif statements and using the `||` and `&&`, but there may be times when you should nest if statements for the sake of readability because clean code does always equate to readable code.

An important part of your job as an engineer will be making sure your code is readable by and comprehensive for other developers. Humans usually comprehend content and make decisions better when things flow in logical sequences, and the best flow a sequence can take in code is often vertical, not horizontal:

//check if it's raining
//if it is raining, check if you want to go outside
//-->if you want to go outside, check if you need rain boots
//--->if you need rain boots, check if you have appropriate socks
//---->if you don't have appropriate socks, you can't wear rain boots
//----->if you can't wear rain boots, you can't go outside.
//------>if you can't go outside, sick down and read a book.

Notice how many decisions are being made there. Let’s try to make those decisions horizontally:

/*check if it's raining. if it is raining, check if you want to go outside. if you want to go outside, check if you need rain boots. if you need rain boots, check if you have appropriate socks. if you don't have appropriate socks, you can't wear rain boots. if you can't wear rain boots, you can't go outside. if you can't go outside, sick down and read a book.*/

This approach would work only if you preprocessed those decisions are stored them in variables as I will cover below. It’s definitely possible, but, even if exaggerated, it is tougher to read as it is clustered and not easy for the mind to progress in a linear fashion through the different decisions. Of course that’s a matter of opinion unless we consult psychologists to do some studies for us, so let’s take a more objective approach.

In order to avoid nesting those decisions, you would need to sacrifice some space in your program to store temporary `boolean` variables to aide in making inline decisions. That’s fine and it works with little negative impact in cases where the decisions you make are trivial. So if we do believe all of our decisions are trivial, we could take this approach:

boolean itsRaining = checkIfItsRaining()
boolean iWantToGoOutside = decideIfIWantToGoOutside()
boolean needRainBoots = checkIfINeedRainBoots()
boolean iHaveCleanRainBootsSocks = checkIfHaveCleanRainBootsSocks()
boolean canGoOutside = !isRaining || ( itsRaining && (!needRainBoots || needRainBoots && iHaveCleanRainBootsSocks) )if canGoOutside {
//go outside
} else {
//read a book
}

Okay, so we did it. We processed to the point where we only needed to check a single condition in our if-statement and it’s beautifully clean and readable. Great. But wait… it looks like we may have made some errors or even did too much unnecessary processing.

Why am I checking for rain boots if I don’t know whether or not its raining yet? Why check for clean socks if I may not even need them? What if my decision to go outside depends on whether or not its raining?

Imagine if all of those decisions are actually extremely expensive for your computer to work through. That would be a great deal of wasted resources. To circumvent that, we end up back here:

if itsRaining {
if iWantToGoOutside && needRainBoots
&& iHaveCleanRainBootsSocks {
//go outside
} else if iWantToGoOutside && needRainBoots
&& !iHaveCleanRainBootsSocks {
//stay inside and do laundry
} else { //stay inside and read book }
} else {
if iWantToGoOutside {
//go outside
} else {
//stay inside and read book
}
}

Notice that there are some decisions we could only make once we had prior knowledge — made previous decisions that subsequent decisions depended upon, and there are some decisions we could make inline with if-elseif statements.

Thus, the conclusion here is that there are definitely times when we should nest statements to preserve resources and avoid making erroneous decisions, and there are other times when we can cleanly make decisions inline.

I hope I’ve adequately answered your question. Please let me know how the rest of the book is treating you.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store