Best Practice

G

Greg Maxey

In this example, is there technical reason to use example 1A & B over
example 2 A & B. Does it make any difference?

Sub BestPractice()
Dim bPractice As Boolean
bPractice = True

If bPractice Then MsgBox "Example 1A"
If bPractice = True Then MsgBox "Example 2A"
bPractice = False
If Not bPractice Then MsgBox "Example 1B"
If bPractice = False Then MsgBox "Example 2B"

End Sub

Thanks
 
C

Charles Kenyon

A difference is that your 1 examples are explicit. I prefer explicit. It
makes for easier debugging (for me).
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

Jay Freedman

In this example, is there technical reason to use example 1A & B over
example 2 A & B. Does it make any difference?

Sub BestPractice()
Dim bPractice As Boolean
bPractice = True

If bPractice Then MsgBox "Example 1A"
If bPractice = True Then MsgBox "Example 2A"
bPractice = False
If Not bPractice Then MsgBox "Example 1B"
If bPractice = False Then MsgBox "Example 2B"

End Sub

Thanks

Practically, no, there's no difference. Use whichever seems more
readable to you.

Formally, 2A & B are a bit redundant. The 1A & B statements evaluate
as

If True Then ...
If Not False Then ... => If True Then ...

The 2A & B statements evaluate as

If True = True Then ...
If Not False = True Then ...

and then the VBA interpreter uses the result of the comparison
operator as the condition of the If. Since both comparisons are true,
this is equivalent to the same pair of statements as in 1A & B. You're
asking VBA to do a little extra work, but you wouldn't be able to
measure the extra time it takes to execute.
 
J

Jezebel

There's a very important difference to bear in mind, although not
significant in this case.

TRUE, as a logical value, is defined as -1. However, true, as evaluated
within logical expresssions, is defined as "not false" -- ie, any value
other than zero.

This trips people up with expressions like --

Dim A as long, B as long

A = ...
B = ...

If (A AND B) then

which works fine and is readable.

But

If (A AND B) = TRUE then

does not work.
 
H

Howard Kaikow

Always use:

If A then

instead of

If A =True then

Always use:

If Not A then

instead of

If A = False then

The above assumes that A is a boolean expression.
 
J

Jonathan West

Greg Maxey said:
In this example, is there technical reason to use example 1A & B over
example 2 A & B. Does it make any difference?

Sub BestPractice()
Dim bPractice As Boolean
bPractice = True

If bPractice Then MsgBox "Example 1A"
If bPractice = True Then MsgBox "Example 2A"

The first option above is better. The Then clause is executed if the overall
expression evaluates to True. Since bPractice is boolean, bPractice=True is
always thge same is bPractice.

However, note what others have said about impicit conversions to boolean If
you have this instead

Sub BestPractice()
Dim iPractice As Long
iPractice = 5

If iPractice Then MsgBox "Example 1A"
If iPractice = True Then MsgBox "Example 2A"

The Then clause of the first line will execute if iPractice has any value
other than 0, because when a Long is connverted to a Boolean, 0 becomes
False and all other values convert to True

The Then clause of the second line will only execute if iPractice is -1.
That is because True is converted to a Long whose value is -1 before the
comparison is made with iPractice. The entire expression only evaluates to
True if the two values are equal.

bPractice = False
If Not bPractice Then MsgBox "Example 1B"
If bPractice = False Then MsgBox "Example 2B"

Much the same considerations apply here.

The overall lesson here is that you need to be aware of what is going on
when type conversions are happening. To ensure that type conversions happen
in the way that you want and expect, you can use the CInt, CLng, CBool and
similar type conversion functions to force an explicit type conversion on an
expression.

Thus you get different results from the following

If CBool(iPractice) = True Then MsgBox "Example 2A"
If iPractice = CLng(True) Then MsgBox "Example 2A"

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top