Short-circuiting

  • Thread starter Thread starter Raymond Lewallen
  • Start date Start date
R

Raymond Lewallen

I understand the differences between And and AndAlso and the differences
between Or and OrElse. Under what circumstance would you NOT use
short-circuiting when evaluating expressions? It seems to me that
short-circuiting should be the preference, but certainly there is a reason,
other than backward-compatibility, that you would use And and Or as opposed
to AndAlso and OrElse.

Thank you for any explanation,

Raymond Lewallen
Federal Aviation Administration
 
* "Raymond Lewallen said:
I understand the differences between And and AndAlso and the differences
between Or and OrElse. Under what circumstance would you NOT use
short-circuiting when evaluating expressions? It seems to me that
short-circuiting should be the preference, but certainly there is a reason,
other than backward-compatibility, that you would use And and Or as opposed
to AndAlso and OrElse.

I use 'And' and 'Or' when all parts of a condition must be evaluated,
for example, if they are function calls. But this strongly depends on
what you want to archieve.
 
Hi Raymond,

As Herfried said, you can use it when all expressions must be evaluated.
However, in most every scenario, you won't want to evaluate every single
expression. It just doesn't make sense. Why go through the extra
processing when you can break on the first instance of an invalid
expression?

I believe Cor said it best... http://tinyurl.com/yw9re :-)

Take care,

Eric
 
Herfried,

It seems to me that your case is an even better reason to use the
short-circuiting, seeing that you are making function calls. If the first
function fails, why bother with the expensive operations that aren't going
to help the expression evaluate to a needed outcome to process following
code in the code-block?

Raymond Lewallen
 
Hi Raymond,

I will follow this with full interest, I never saw a good reason why the
functionality from the OrElse was not normal in the Or and the same with the
AndAlso, however you opened a new point of view to argue about that.

Cor
 
* "Raymond Lewallen said:
It seems to me that your case is an even better reason to use the
short-circuiting, seeing that you are making function calls. If the first
function fails, why bother with the expensive operations that aren't going
to help the expression evaluate to a needed outcome to process following
code in the code-block?

In some situation it must be sure that both functions are executed, even
if one of them returned 'False'.
 
Raymond,
I see And useful on Function calls (as Herfried suggested), when both
functions need to be executed, however I only care if they both succeeds.
For example the functions called update database records, I still want to
update the second database, however I want to combine the return values...
(if that made any sense)

I will also use And & Or when using AndAlso & OrElse really does not buy me
anything. However this is mostly just laziness and carry over from VB6, not
a misunderstanding of the difference of And & AndAlso! :-)

For example:

Dim lValue, rValue As Boolean

If lValue And rValue Then
' Do something interesting here
End If

I don't see a huge benefit of either And or AndAlso over the other in the
above. Of course once using And proved to be a performance problem, due to
profiling, then I would consider changing it to the other operator, so see
if that improved performance...

Hope this helps
Jay
 
Back
Top