Recently I have noted several occasions where I have two or more tests that need to be done, before I run a block of code, but the same code should be run if any of the tests is false.
For example:
If isWindow(hwnd_x)
_ If isVisible(hwnd_x)
_ _ doSomething()
_ Else
_ _ complain()
_ End If
Else
_ complain()
End If
If I call isVisible on a nonexisting window, I will get an error, so I test to make sure that the window exists before proceeding. Running the first test is necessary before you even try to run the "real" test, but failure on either test should have the same effect.
It is fairly easy to use the code above to handle the situation, but this is less maintainable, because if you change one of the else clauses and not the other you will never know if that was intentional or a bug. Anyway, there are advantages to having one else clause to handle both if clauses.
So, I propose an And If clause that works like this.
If isWindow(hwnd_x)
And If isVisible(hwnd_x)
_ doSomething()
Else
_ complain()
End If
Of course, any number of And If's could modify a single if statement, each checked in succession and all sharing the same internal code block and else block, like so.
If isWindow(hwnd_x)
And If isVisible(hwnd_x)
And If isUgly(hwnd_x)
And If current_count < MAX_WINDOW_COUNT
_ doSomething()
Else
_ complain()
End If
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If isWindow(hwnd_x)
And If isVisible(hwnd_x)
And If isUgly(hwnd_x)
And If current_count < MAX_WINDOW_COUNT
_ doSomething()
Else
_ complain()
End If
and have it check each one in turn and opt out as soon as one is false, then why not have the "Or" version of the same thing that keeps checking until one is true.
If isWindow()
Or If isUgly()
Or If imFeelingLucky()
_ doSomething()
Else
_ complain()
End If
The difference between this notation and just anding or oring a bunch of booleans is that they are executed in order, and not all of the comparisons need to be made.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Recently I have noted several occasions where I have two or more tests that need to be done, before I run a block of code, but the same code should be run if any of the tests is false.
For example:
If isWindow(hwnd_x)
_ If isVisible(hwnd_x)
_ _ doSomething()
_ Else
_ _ complain()
_ End If
Else
_ complain()
End If
If I call isVisible on a nonexisting window, I will get an error, so I test to make sure that the window exists before proceeding. Running the first test is necessary before you even try to run the "real" test, but failure on either test should have the same effect.
It is fairly easy to use the code above to handle the situation, but this is less maintainable, because if you change one of the else clauses and not the other you will never know if that was intentional or a bug. Anyway, there are advantages to having one else clause to handle both if clauses.
So, I propose an And If clause that works like this.
If isWindow(hwnd_x)
And If isVisible(hwnd_x)
_ doSomething()
Else
_ complain()
End If
Of course, any number of And If's could modify a single if statement, each checked in succession and all sharing the same internal code block and else block, like so.
If isWindow(hwnd_x)
And If isVisible(hwnd_x)
And If isUgly(hwnd_x)
And If current_count < MAX_WINDOW_COUNT
_ doSomething()
Else
_ complain()
End If
if it is ok to say
If isWindow(hwnd_x)
And If isVisible(hwnd_x)
And If isUgly(hwnd_x)
And If current_count < MAX_WINDOW_COUNT
_ doSomething()
Else
_ complain()
End If
and have it check each one in turn and opt out as soon as one is false, then why not have the "Or" version of the same thing that keeps checking until one is true.
If isWindow()
Or If isUgly()
Or If imFeelingLucky()
_ doSomething()
Else
_ complain()
End If
The difference between this notation and just anding or oring a bunch of booleans is that they are executed in order, and not all of the comparisons need to be made.