PC Review


Reply
Thread Tools Rate Thread

AndAlso, OrElse and brackets

 
 
Mike Ratcliffe
Guest
Posts: n/a
 
      22nd Mar 2010
We have been having a lively debate at work about whether or not we
should use brackets with conditionals that contain AndAlso. This is
because AndAlso has precedence over OrElse and I say that it makes
code more manageable if brackets are included.

False OrElse True AndAlso False will return False because AndAlso has
precedence over OrElse.

I say that this is better written as:
False OrElse (True AndAlso False)

What do you think?
 
Reply With Quote
 
 
 
 
AMercer
Guest
Posts: n/a
 
      22nd Mar 2010
I agree with you - include the parentheses. More so with less often used
constructs, and more so with more complex expressions. Intent will be clear
to the new guy next year whose first assignment is making a mod to this code.

"Mike Ratcliffe" wrote:

> We have been having a lively debate at work about whether or not we
> should use brackets with conditionals that contain AndAlso. This is
> because AndAlso has precedence over OrElse and I say that it makes
> code more manageable if brackets are included.
>
> False OrElse True AndAlso False will return False because AndAlso has
> precedence over OrElse.
>
> I say that this is better written as:
> False OrElse (True AndAlso False)
>
> What do you think?
> .
>

 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      22nd Mar 2010
Am 22.03.2010 13:32, schrieb Mike Ratcliffe:
> We have been having a lively debate at work about whether or not we
> should use brackets with conditionals that contain AndAlso. This is
> because AndAlso has precedence over OrElse and I say that it makes
> code more manageable if brackets are included.
>
> False OrElse True AndAlso False will return False because AndAlso has
> precedence over OrElse.
>
> I say that this is better written as:
> False OrElse (True AndAlso False)
>
> What do you think?


Agreeing with AMercer.

--
Armin
 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      22nd Mar 2010
Mike,

This are question which completely depends on the knowledge of the users.

Some say parentheses make it easier to read, others say you would not put
things in code without a function, because then others become suspicious and
take time in it to examine why you did it and when they cannot find it, take
even more time.

However, personally I do it too, because I am simply to lazy to reminds me
what goes first especially in a not situation.

But to say that it is better, like I wrote, it is mainly because I'm lazy.

jmo

Cor



"Mike Ratcliffe" <(E-Mail Removed)> wrote in message
news:1693dfce-1557-4369-889c-(E-Mail Removed)...
> We have been having a lively debate at work about whether or not we
> should use brackets with conditionals that contain AndAlso. This is
> because AndAlso has precedence over OrElse and I say that it makes
> code more manageable if brackets are included.
>
> False OrElse True AndAlso False will return False because AndAlso has
> precedence over OrElse.
>
> I say that this is better written as:
> False OrElse (True AndAlso False)
>
> What do you think?


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      22nd Mar 2010
Am 22.03.2010 13:32, schrieb Mike Ratcliffe:
> We have been having a lively debate at work about whether or not we
> should use brackets with conditionals that contain AndAlso. This is
> because AndAlso has precedence over OrElse and I say that it makes
> code more manageable if brackets are included.
>
> False OrElse True AndAlso False will return False because AndAlso has
> precedence over OrElse.
>
> I say that this is better written as:
> False OrElse (True AndAlso False)
>
> What do you think?


I don't like the brackets, but I'd suggest to use them because I doubt
that anybody who will work on the code is aware of the precendence rules
;-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Michel Posseth [MCP]
Guest
Posts: n/a
 
      22nd Mar 2010

i believe math rules are universal and that is the reasson thart using
brackets would give self documentation of the code , and that alone would
make it "good coding practice" in my homble opinion


HTH

Michel Posseth


"Mike Ratcliffe" <(E-Mail Removed)> schreef in bericht
news:1693dfce-1557-4369-889c-(E-Mail Removed)...
> We have been having a lively debate at work about whether or not we
> should use brackets with conditionals that contain AndAlso. This is
> because AndAlso has precedence over OrElse and I say that it makes
> code more manageable if brackets are included.
>
> False OrElse True AndAlso False will return False because AndAlso has
> precedence over OrElse.
>
> I say that this is better written as:
> False OrElse (True AndAlso False)
>
> What do you think?


 
Reply With Quote
 
Mark Hurd
Guest
Posts: n/a
 
      24th Mar 2010
"Mike Ratcliffe" <(E-Mail Removed)> wrote in message
news:1693dfce-1557-4369-889c-(E-Mail Removed)...
> We have been having a lively debate at work about whether or not we
> should use brackets with conditionals that contain AndAlso. This is
> because AndAlso has precedence over OrElse and I say that it makes
> code more manageable if brackets are included.


I agree.

> False OrElse True AndAlso False will return False because AndAlso has
> precedence over OrElse.
>
> I say that this is better written as:
> False OrElse (True AndAlso False)


Bad example

(False OrElse True) AndAlso False = False
False OrElse (True AndAlso False) = False

and all expressions always need to be evaluated.

An example where it matters:
True OrElse True AndAlso False
(True OrElse True) AndAlso False = False
True OrElse (True AndAlso False) = True

and the expressions in the AndAlso in the second case are now not even
evaluated.

--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)

 
Reply With Quote
 
Mike Ratcliffe
Guest
Posts: n/a
 
      24th Mar 2010
We have a guy working here that is a brilliant developer, he is a
genius when it comes to dealing with extremely complex stuff. He
doesn't like the idea of adding "unnecessary brackets" as he says that
he never has to think twice when he looks at conditions. Personally I
don't see how using brackets in this situation could be a bad thing
but I guess he is pretty resistant to change.

I just find it fascinating that anybody would be opposed to such an
obvious improvement.

On 24 Mar, 01:23, "Mark Hurd" <markh...@ozemail.com.au> wrote:
> "Mike Ratcliffe" <sabine.michael.ratcli...@gmail.com> wrote in message
>
> news:1693dfce-1557-4369-889c-(E-Mail Removed)...
>
> > We have been having a lively debate at work about whether or not we
> > should use brackets with conditionals that contain AndAlso. This is
> > because AndAlso has precedence over OrElse and I say that it makes
> > code more manageable if brackets are included.

>
> I agree.
>
> > False OrElse True AndAlso False will return False because AndAlso has
> > precedence over OrElse.

>
> > I say that this is better written as:
> > False OrElse (True AndAlso False)

>
> Bad example
>
> (False OrElse True) AndAlso False = False
> False OrElse (True AndAlso False) = False
>
> and all expressions always need to be evaluated.
>
> An example where it matters:
> True OrElse True AndAlso False
> (True OrElse True) AndAlso False = False
> True OrElse (True AndAlso False) = True
>
> and the expressions in the AndAlso in the second case are now not even
> evaluated.
>
> --
> Regards,
> Mark Hurd, B.Sc.(Ma.) (Hons.)


 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      25th Mar 2010
I agree with that guy, YOU want to change rules which are already more than
5000 years old.

Which does not mean that in complex situations I simply set some
parentheses.

Although I then mostly earlier break up the code with some more ifs.

But the guy is right.

Cor

"Mike Ratcliffe" <(E-Mail Removed)> wrote in message
news:c02e923a-7a10-41ec-b24f-(E-Mail Removed)...
> We have a guy working here that is a brilliant developer, he is a
> genius when it comes to dealing with extremely complex stuff. He
> doesn't like the idea of adding "unnecessary brackets" as he says that
> he never has to think twice when he looks at conditions. Personally I
> don't see how using brackets in this situation could be a bad thing
> but I guess he is pretty resistant to change.
>
> I just find it fascinating that anybody would be opposed to such an
> obvious improvement.
>
> On 24 Mar, 01:23, "Mark Hurd" <markh...@ozemail.com.au> wrote:
>> "Mike Ratcliffe" <sabine.michael.ratcli...@gmail.com> wrote in message
>>
>> news:1693dfce-1557-4369-889c-(E-Mail Removed)...
>>
>> > We have been having a lively debate at work about whether or not we
>> > should use brackets with conditionals that contain AndAlso. This is
>> > because AndAlso has precedence over OrElse and I say that it makes
>> > code more manageable if brackets are included.

>>
>> I agree.
>>
>> > False OrElse True AndAlso False will return False because AndAlso has
>> > precedence over OrElse.

>>
>> > I say that this is better written as:
>> > False OrElse (True AndAlso False)

>>
>> Bad example
>>
>> (False OrElse True) AndAlso False = False
>> False OrElse (True AndAlso False) = False
>>
>> and all expressions always need to be evaluated.
>>
>> An example where it matters:
>> True OrElse True AndAlso False
>> (True OrElse True) AndAlso False = False
>> True OrElse (True AndAlso False) = True
>>
>> and the expressions in the AndAlso in the second case are now not even
>> evaluated.
>>
>> --
>> Regards,
>> Mark Hurd, B.Sc.(Ma.) (Hons.)

>

 
Reply With Quote
 
Mark Hurd
Guest
Posts: n/a
 
      29th Mar 2010
"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in message
news:OEf%23FG$(E-Mail Removed)...
>I agree with that guy, YOU want to change rules which are already more
>than 5000 years old.
>
> Which does not mean that in complex situations I simply set some
> parentheses.
>
> Although I then mostly earlier break up the code with some more ifs.
>
> But the guy is right.
>
> Cor


If you were talking about the basic arithmetic operators, */ +-, you'd
be right, brackets are almost always excessive.

But for And and Or I believe there have been computer languages that
have a precedence opposite that of Basic (however I couldn't find them
with a quick Google search).

When you think about it a bit And binding more tightly than Or /does/
make sense, but it isn't anywhere near as definite as multiplication and
addition (admittedly probably because we get taught BODMAS -- or one of
the variations described in Wikipedia -- very early in school, where as
Boolean algebra might not be formalised until Uni).

--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to translate OrElse and AndAlso from VB.NET? Siegfried Heintze Microsoft C# .NET 3 11th Jan 2008 11:08 PM
And vs. AndAlso , Or vs. OrElse and Set data type Herfried K. Wagner [MVP] Microsoft VB .NET 9 26th Jul 2005 09:24 PM
AndAlso & OrElse Raterus Microsoft VB .NET 12 5th Aug 2004 01:35 AM
AndAlso/OrElse usage Mike Hale Microsoft VB .NET 10 12th Nov 2003 11:26 PM
The Ballad of AndAlso and OrElse Jeremy Microsoft VB .NET 14 29th Aug 2003 12:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:37 AM.