Nested IFs, Good, bad, or neither?


#1

1) Give a description of the problem
Trying to determine if having a nested if statement is worthwhile. I’m working on a larger piston so I am trying to cut down on the lines that have to be parsed every time. I know I can do separate if statements and not have them nested, but ideally I would like to have a few like this that would cut down on the number of lines parsed. Is this going to give me the desired outcome from a processing standpoint or is there a better way of doing this? Or does it just not matter?

2) What is the expected behaviour?
To ignore the inner if statements if the outer if statement is false

3) What is happening/not happening?
its working, but curious if this is the best design

4) Post a Green Snapshot of the pistonimage

5) Attach logs after turning logging level to Full
No logs


#2

Nesting can be good, if done right.

Each case is different, but generally speaking, I put a precise trigger first, followed by a fairly precise condition (as you have done)… If I need a third layer, it is usually for very vague conditions. Technically, I can often put them in any order, but if the common condition is first (top level), then the piston will usually keep chugging along…

This layered approach (with “rares” at the top) can reduce load time, since it will often cancel out after the first (or second) check.


The concern I have with this piston is whenever either light attempts to turn off, it immediately comes back on… Is this your intention?


#3

The only thing I’d add is that nests are for contingencies. It really does work that the nested if is only tested if the initial if is true.
Be sure to check the logic. There are a number of piston problems I’ve seen where you actually don’t want nesting because the inner if shouldn’t be entirely contingent on the outer.


#4

The concern I have with this piston is whenever either light attempts to turn off, it immediately comes back on… Is this your intention?

@WCmore
Yes this is for two switches that I don’t have yet, so I used the lights as place holders to build what I wanted.