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

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Re: Best Practice by Charles

Charles
Tue May 24 21:57:02 CDT 2005

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.

"Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
news:eVwhSPNYFHA.3164@TK2MSFTNGP12.phx.gbl...
> 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
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>



Re: Best Practice by Jay

Jay
Tue May 24 22:07:04 CDT 2005

On Tue, 24 May 2005 22:45:49 -0400, "Greg Maxey"
<gmaxey@mvps.OscarRomeoGolf> wrote:

>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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

Re: Best Practice by Jezebel

Jezebel
Tue May 24 23:56:17 CDT 2005

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.








"Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
news:eVwhSPNYFHA.3164@TK2MSFTNGP12.phx.gbl...
> 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
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>



Re: Best Practice by Greg

Greg
Wed May 25 07:03:38 CDT 2005

Thank you all for your comments and insight.


Re: Best Practice by Howard

Howard
Wed May 25 11:41:56 CDT 2005

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.

--
http://www.standards.com/; See Howard Kaikow's web site.



Re: Best Practice by Jonathan

Jonathan
Wed May 25 13:55:55 CDT 2005


"Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
news:eVwhSPNYFHA.3164@TK2MSFTNGP12.phx.gbl...
> 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


Re: Best Practice by Greg

Greg
Wed May 25 20:42:22 CDT 2005

Thanks Jonathan,

I have this entire string in my keep folder.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jonathan West wrote:
> "Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
> news:eVwhSPNYFHA.3164@TK2MSFTNGP12.phx.gbl...
>> 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"