Cost/Benefit with respect to If ... Then.. structure

  • Thread starter Thread starter ScardyBob
  • Start date Start date
S

ScardyBob

Hello All!

I have a general question related to the structure of a If ... Then ...
statement. More specifically what are the costs/benefits from writing
an If ... Then ... statement as follows:

If i <> 0 Then
'Do Nothing
Else
'Code you want to do something productive
End If

Rather then writing it as:

If i = 0 Then
'Code you want to do something productive
End If

I know this may seem like a stupid question, but I've been writing
certain If ... Then ... statements in the first manner, thinking they
are equivalent and simply a manner of personnel preference. Is there a
downside to this (besides having to write an extra line or two)?

Thanks in Advance!
Mike
 
When you deal with data, you always need to consider the case Null case.

In any essence, a comparison has 3 possible results:
- True
- False
- Null

So, your example means:
If i is a non-zero value to nothing;
otherwise (it is a zero value, or it is null), then run the code.

Consider this case, where you want to run some checks (say in
Form_BeforeUpdate) unless MyControl is unchanged. The 4 cases to consider
are:
- MyControl is null, but it had a value previously;
- MyControl is not null, but it had no value previously;
- MyControl has a valud that is different from its previous value;
- MyControl has the same value as previously.

You could code:
If (IsNull(.Value) And Not IsNull(.OldValue)) _
OR (Not IsNull(.Value) And IsNull(.OldValue)) _
OR (.Value <> .OldValue) Then
'run some code
End If

Or, you could use the Else block to handle all 3 of those cases:
With Me.MyControl
If .Value = .OldValue Then
'do nothing
Else
'run some code.
End If
End With

Which seems easier to code and more efficient to maintain and execute?

If this understanding of Nulls is new, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html
 
ScardyBob said:
I have a general question related to the structure of a If ... Then ...
statement. More specifically what are the costs/benefits from writing
an If ... Then ... statement as follows:

If i <> 0 Then
'Do Nothing
Else
'Code you want to do something productive
End If

Rather then writing it as:

If i = 0 Then
'Code you want to do something productive
End If

I know this may seem like a stupid question, but I've been writing
certain If ... Then ... statements in the first manner, thinking they
are equivalent and simply a manner of personnel preference. Is there a
downside to this (besides having to write an extra line or two)?


The difference is the first one has an additional
instruction (the Else statement) that will consume a
nanosecond or two of execution time. This cost is modified
by the probablility of i being 0 or not. When i <> 0, there
is no difference, the additional execution time only occurs
when i = 0.

Bottom line, on today's modern computers, the difference is
so small that you will not notice it unless it is in a loop
with a very large number of iterations.
 
Marshall and Allen,

I think I get it now. I should try to put the narrowest possible
criteria in the If clause to reduce the amount of coding and typing
errors.

Thanks for your help!

Mike
 
Back
Top