Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

using ! or == false? your preference?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anders Borum - 14 Nov 2006 13:09 GMT
Hello!

While in-between sessions at the Tech-ED in Barcelona, I overheard a
discussion between some developers. The subject of the discussion was code
layout (i.e. how each developer formats and writes code). What are your
takes at the following two examples? Which one would you choose and why?

// may be easier to read for short statements
if (boolean == false) {}
if (DetermineResultFromMethod(arg1, arg2) == false)) {}

or

// easier for longer statements?
if (! boolean) {}
if (! DetermineResultFromMethod(arg1, arg2)) {}

Thinking about it, we've had this discussion at the office several times,
and I've never seen any public discussion on the subject.

Signature

with regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Dan Bass - 14 Nov 2006 13:15 GMT
Personal preference, I always go for "== false" if for one reason only... My
eye sometimes misses a single "!"

> Hello!
>
[quoted text clipped - 15 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.
Martin Pöpping - 14 Nov 2006 13:34 GMT
Dan Bass schrieb:
> Personal preference, I always go for "== false" if for one reason only... My
> eye sometimes misses a single "!"

So I have the other personal preference.

I prefer a shorter code if the code is not more difficult to understand.

and a "!" instead of a "== false" is much shorter but not more difficult
to understand (assumed that you do not have a poor eyesight or poor glasses)

Regards,

Martin
fallenidol - 14 Nov 2006 13:56 GMT
definatley the first one. it's much easier to read.

Signature

"If ignorance is bliss, then wipe the smile from my face."

> Hello!
>
[quoted text clipped - 15 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.
Tom Porterfield - 14 Nov 2006 14:26 GMT
> Hello!
>
[quoted text clipped - 15 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.

It just comes down to personal preference and which you find easier to read.
I personally use the latter but can read either one just fine.  Those
unfamiliar with C# will find the first easier to understand, but when seeing
it they will have no motivation to learn anything more about C#.

My opinion is it's generally best not to dictate things that come down to
personal style choices when it is as simplistic as your example.  There is
no strong justifiable reason to force anyone to use one syntax over the
other.
Signature

Tom Porterfield

Dave Sexton - 14 Nov 2006 16:01 GMT
Hi Anders,

Using the ! character is safer in case you accidentally forget an = character:

bool aBool = false;

if (aBool = false)
{
   // aBool was false, but this code won't execute
   // Worse even, the compiler allows it
}

Although, the compiler does give a nice warning :)

Signature

Dave Sexton

> Hello!
>
[quoted text clipped - 15 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.
Dan Bass - 14 Nov 2006 16:06 GMT
Ah you could aruge that if you miss an extra "=", you could potentially miss
! from (!MyVar).
That would give you no warning.  ;-)

> Hi Anders,
>
[quoted text clipped - 31 lines]
>> Thinking about it, we've had this discussion at the office several times,
>> and I've never seen any public discussion on the subject.
Dave Sexton - 14 Nov 2006 16:59 GMT
Hi Dan,

No, they're really not the same.

I've accidentally entered a single = at times but I've never just forgotten to
type "!" when I was trying to negate an expression.

I'm speaking more of a type-o.

Signature

Dave Sexton

> Ah you could aruge that if you miss an extra "=", you could potentially miss
> ! from (!MyVar).
[quoted text clipped - 34 lines]
>>> Thinking about it, we've had this discussion at the office several times,
>>> and I've never seen any public discussion on the subject.
Ebbe Kristensen - 14 Nov 2006 16:09 GMT
> Using the ! character is safer in case you accidentally forget an =
> character:
[quoted text clipped - 7 lines]
>
> Although, the compiler does give a nice warning :)

Always put the constant first:

if( false = aBool )
{
}

won't compile, not even with the most ancient C-compiler, you can come up
with.

Ebbe
Dave Sexton - 14 Nov 2006 16:59 GMT
Hi Ebbe,

Good point.  Now, it's looking a lot closer to:

if (! aBool)
{
}

:)

Signature

Dave Sexton

>> Using the ! character is safer in case you accidentally forget an =
>> character:
[quoted text clipped - 18 lines]
>
> Ebbe
Mark Wilden - 14 Nov 2006 18:05 GMT
> Always put the constant first:
>
[quoted text clipped - 4 lines]
> won't compile, not even with the most ancient C-compiler, you can come up
> with.

Why write code in C# as if you were writing code for an ancient C compiler?

///ark
Jon Skeet [C# MVP] - 14 Nov 2006 19:47 GMT
> > Always put the constant first:
> >
[quoted text clipped - 6 lines]
>
> Why write code in C# as if you were writing code for an ancient C compiler?

Because in this *one* case, it's dangerous to put the variable first
and the value second. In non-boolean cases, the compiler will throw an
error at you.

Now, it happens to *warn* you if you do

if (x = false)

but it wouldn't warn you with:

if (x = SomeMethodCall())

whereas

if (SomeMethodCall() = x)

will give an error.

I know it's not quite the same situation, but it's still interesting :)

Note that although many of us treat all warnings as errors and would
spot any warnings, not everyone does - if a warning for
"if (x = false)" goes unnoticed, that could be pretty serious.

Of course, as I've already posted, I prefer if (!x) in the first
place...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mark Wilden - 14 Nov 2006 21:55 GMT
>> Why write code in C# as if you were writing code for an ancient C
>> compiler?
>
> Because in this *one* case, it's dangerous to put the variable first
> and the value second. In non-boolean cases, the compiler will throw an
> error at you.

Actually, I was just commenting on why we C# programmers would care about an
ancient C compiler.

> Note that although many of us treat all warnings as errors and would
> spot any warnings, not everyone does - if a warning for
> "if (x = false)" goes unnoticed, that could be pretty serious.

I do treat warnings as errors, myself, and I also write unit tests.

> Of course, as I've already posted, I prefer if (!x) in the first
> place...

Me too.

///ark
Ben Voigt - 14 Nov 2006 21:13 GMT
>> Always put the constant first:
>>
[quoted text clipped - 7 lines]
> Why write code in C# as if you were writing code for an ancient C
> compiler?

Because those who don't learn from history (even ancient history) are doomed
to repeat it.

> ///ark
Jon Skeet [C# MVP] - 14 Nov 2006 21:36 GMT
> > Why write code in C# as if you were writing code for an ancient C
> > compiler?
>
> Because those who don't learn from history (even ancient history) are doomed
> to repeat it.

Fortunately in this case, the C# language designers learned from the
mistakes of the C language designers - unless you actually *do*
comparisons of boolean values using == (which you very rarely need to
do anyway) you can write comparisons in the more readable

if (variable == constant)

way with no fear of accidental assignment.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dave Sexton - 15 Nov 2006 10:49 GMT
Hi Jon,

You were referring to a developer only typing a single = character on
accident, correct?

> if (variable == constant)
>
> [...] with no fear of accidental assignment.

I think you mean

if (constant == variable)

can be used with no fear of accidental assignment.

The compiler allows the code I posted previously

bool aBool = false;
if (aBool = false) { }

although it does display a warning.

Signature

Dave Sexton

>> > Why write code in C# as if you were writing code for an ancient C
>> > compiler?
[quoted text clipped - 11 lines]
>
> way with no fear of accidental assignment.
Jon Skeet [C# MVP] - 15 Nov 2006 17:39 GMT
> You were referring to a developer only typing a single = character on
> accident, correct?
[quoted text clipped - 8 lines]
>
> can be used with no fear of accidental assignment.

No, I meant exactly what I wrote. See the previous paragraph:

<quote>
unless you actually *do*
comparisons of boolean values using == (which you very rarely need to
do anyway) you can write comparisons in the more readable
</quote>

In other words, for types other than boolean, the compiler stops you
anyway, and for boolean comparisons you can usually just do:

if (foo)
or
if (!foo)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dave Sexton - 16 Nov 2006 17:08 GMT
Hi Jon,

Thanks for clearing that up for me.

Signature

Dave Sexton

>> You were referring to a developer only typing a single = character on
>> accident, correct?
[quoted text clipped - 23 lines]
> or
> if (!foo)
Ebbe Kristensen - 15 Nov 2006 10:28 GMT
> Why write code in C# as if you were writing code for an ancient C
> compiler?

I picked this trick up many years ago when I used an ancient C compiler :-)

Ebbe
Bobbo - 14 Nov 2006 16:37 GMT
> While in-between sessions at the Tech-ED in Barcelona, I overheard a
> discussion between some developers. The subject of the discussion was code
> layout (i.e. how each developer formats and writes code).

Slightly OT, but we had a similar discussion in the days of ye-olde
ASP, on which of the following was more efficient.

This one:
If <condition> Then
 var = 1
Else
 var = 2
End If

Or this one:
var = 2
If <condition> Then var = 1

Imagine how heated that got when I threw IIF() into the pot, for fun.
By the way, the outcome was much the same as this one.
Dan Bass - 14 Nov 2006 17:12 GMT
Agree with the OT bit! Using == false or ! or your example below doesn't
dictate good or bad programming practices...

If at the end of a month long discussion a conclusion was agreed upon that !
is the way to go, but everyone forgot to comment their code, or even worse,
comment their code badly, then the readability of the ! suddenly becomes a
non-event.

As long as the style is clear and consisten, easy to read and well designed,
then things like this are irrelevant.

>> While in-between sessions at the Tech-ED in Barcelona, I overheard a
>> discussion between some developers. The subject of the discussion was
[quoted text clipped - 17 lines]
> Imagine how heated that got when I threw IIF() into the pot, for fun.
> By the way, the outcome was much the same as this one.
Mattias Sjögren - 14 Nov 2006 19:22 GMT
Anders,

>Which one would you choose and why?

Why not

if (DetermineResultFromMethod(arg1, arg2) != true)) {}

<g>

No seriously I tend to use !

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Anders Borum - 14 Nov 2006 20:58 GMT
Mattias,

Good one. I needed that here at 9:30 pm ;-)

Also good to see the comments. I agree with the statements on variable
assignment being a bad thing, but also realize that this is one of those
things that may be left open for decision by the developer (where as
programming syntax etc. is a whole other discussion - we enforce that at our
company (all developers agreed on syntax, so it's not a problem)).

Signature

With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Bob Jones - 14 Nov 2006 23:26 GMT
I prefer to name things so that using the " ! " it reads well; and it's
is more concise.  Often I'll put a space after the " ! " because, as
mentioned already, it's kinda easy to miss.  Beyond that programmers
will just have to learn how to program well. Any programming idiom is
mauled by the inexperienced and incompetent.

And piggy-backing on the "if then" discussions, I've come to love the
tertiary statement. You know:
    <some statement that resolves to a boolean> ? <execute if true> :
<execute if false>;
Very concise, readable, and much less typo-error prone.

> Anders,
>
[quoted text clipped - 8 lines]
>
> Mattias
Bobbo - 16 Nov 2006 10:08 GMT
> And piggy-backing on the "if then" discussions, I've come to love the
> tertiary statement. You know:
>     <some statement that resolves to a boolean> ? <execute if true> :
> <execute if false>;
> Very concise, readable, and much less typo-error prone.

That's one of my current favourites too, although when I first started
using it in Javascript (yes, OT again sorry) I forgot the syntax and
couldn't remember what it was called either - try searching for that in
Google!
Anders Borum - 16 Nov 2006 10:41 GMT
Hi Mattias,

Yea, the good ol' discussion on tertiary statements just ressurected! :-)

> And piggy-backing on the "if then" discussions, I've come to love the
> tertiary statement. You know:
> <some statement that resolves to a boolean> ? <execute if true> : <execute
> if false>;
> Very concise, readable, and much less typo-error prone.

I'm also a big fan of that and it's extremely fast to write (and read, once
you spend a few days with it).

Signature

With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Jon Skeet [C# MVP] - 14 Nov 2006 19:43 GMT
> While in-between sessions at the Tech-ED in Barcelona, I overheard a
> discussion between some developers. The subject of the discussion was code
[quoted text clipped - 13 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.

If I'm treating the boolean as a *condition* (which is the usual case)
I'll use if (!condition)

That way when I read it it's: "if not condition" which makes more sense
to me than "if condition equals false".

If it's a "value" in itself (and I'm struggling to even think of any
examples here) then I'd probably be okay with if (value == false).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mark R. Dawson - 15 Nov 2006 06:46 GMT
This is same reason I prefer the ! syntax to "== false"  Whenever I create a
property or function that returns a bool I try to give it a name that can
easily be read like a sentence,  whenever I see ! that immediately translates
to NOT in my mind so something like:

if(!MaxValue)   // if not MaxValue
{
}

seems a lot clearer than:

if(MaxValue == false)
{
}

which takes a few more cycles of my brain to evaluate than the first way.

Mark.
Signature

http://www.markdawson.org

> > While in-between sessions at the Tech-ED in Barcelona, I overheard a
> > discussion between some developers. The subject of the discussion was code
[quoted text clipped - 22 lines]
> If it's a "value" in itself (and I'm struggling to even think of any
> examples here) then I'd probably be okay with if (value == false).
JR - 15 Nov 2006 10:12 GMT
I would have named it ValueAtMax, I thing it is clearer. MaxValue would be
more appropriate to the maximal value.

I find that I make much use of PF2 to rename things so that the names are
clear and uncofusable.

JR

> This is same reason I prefer the ! syntax to "== false"  Whenever I create
> a
[quoted text clipped - 46 lines]
>> If it's a "value" in itself (and I'm struggling to even think of any
>> examples here) then I'd probably be okay with if (value == false).
Mark R. Dawson - 16 Nov 2006 01:46 GMT
Yea, MaxValue was not a very good example :-) in this case.

Signature

http://www.markdawson.org

> I would have named it ValueAtMax, I thing it is clearer. MaxValue would be
> more appropriate to the maximal value.
[quoted text clipped - 54 lines]
> >> If it's a "value" in itself (and I'm struggling to even think of any
> >> examples here) then I'd probably be okay with if (value == false).
Bill Butler - 15 Nov 2006 18:15 GMT
> This is same reason I prefer the ! syntax to "== false"  Whenever I
> create a
[quoted text clipped - 16 lines]
> which takes a few more cycles of my brain to evaluate than the first
> way.

I agree completely

if (!Done)   --> If not Done

Unfortunately I have maintained code from programmers who aren't as
careful with there naming.

example:

   if (!NotDone) --> if not 'Not done'

Ugh, double negatives hurt my brain.

Of course in this case I generally rewrite the logic to be less painful.

   Bill
David Anton - 14 Nov 2006 19:55 GMT
I think the risk with the "== false" approach is that you might be tempted to
start coding "== true" to be consistent and that would just be redundant
(then you'd read your boolean conditions as "if it's true that x is true"...).
Signature

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter

> Hello!
>
[quoted text clipped - 15 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.
Dave Sexton - 15 Nov 2006 01:19 GMT
Hi David,

I don't think "== true" is redundant for that reason, though.  You could read
the "== false" as, "if it's false that x is true" as well.

It's redundant simply because the code will compile with just the predicate
itself, so there is no need for "== true" to be present.

Signature

Dave Sexton

>I think the risk with the "== false" approach is that you might be tempted to
> start coding "== true" to be consistent and that would just be redundant
[quoted text clipped - 20 lines]
>> Thinking about it, we've had this discussion at the office several times,
>> and I've never seen any public discussion on the subject.
Dave Sexton - 15 Nov 2006 01:19 GMT
Hi Anders,

Interesting thread.

Personally, I just use ! everywhere because I think it's quite easy to
understand, it only takes one key press, it's not going to accidentally assign
a value, and contrary to what other respondents have written, I don't think
that it's hard to miss at all.  As a matter of fact,  I never place a space
between the ! and the predicate in real code either, and I don't remember ever
exclaiming, "My code wasn't working as I expected, but then I realized that I
didn't see the !" ;)

"false ==" and "== false" just looks sloppy to me for some reason, the latter
may be subject to accidental assignment with only a compiler warning and it's
more to type.  Although, I agree that reading "== false" as, "if condition is
false" might make more sense than reading it as, "if not true", but that's not
the deciding factor for me.  "false ==" is not readable at all, but is
recommended to prevent accidental assignment in certain situations.
Therefore, it makes sense to use ! in certain situations and then, for the
sake of consistency, all situations.  The value of consistency here may be
arguable though.

Signature

Dave Sexton

> Hello!
>
[quoted text clipped - 15 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.
Arne Vajhøj - 15 Nov 2006 02:21 GMT
> While in-between sessions at the Tech-ED in Barcelona, I overheard a
> discussion between some developers. The subject of the discussion was code
[quoted text clipped - 13 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.

I am all for the use of the ! operator.

The reason is that ==false and ==true gives me associations about
very inexperienced programmers.

Arne
Anders Borum - 16 Nov 2006 10:45 GMT
Hello!

> The reason is that ==false and ==true gives me associations about
> very inexperienced programmers.

Although I prefer the ! operator, I wouldn't necessarily agree with the
above statements.

Lots of people acknowledged it was up to personal preference (although good
arguments has been presented - especially on accidental assignments). I
would base my rating of a developer on overall code efficiency, architecture
and quality (documentation too) than the - perhaps intentional - use of the
== false pattern.

Signature

With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)

Arne Vajhøj - 17 Nov 2006 02:15 GMT
>> The reason is that ==false and ==true gives me associations about
>> very inexperienced programmers.
>
> Although I prefer the ! operator, I wouldn't necessarily agree with the
> above statements.

You won't agree that I get that association ??

> Lots of people acknowledged it was up to personal preference (although good
> arguments has been presented - especially on accidental assignments). I
> would base my rating of a developer on overall code efficiency, architecture
> and quality (documentation too) than the - perhaps intentional - use of the
> == false pattern.

Where I come from we do not base rating on associations at all - not
from coding style or code efficiency or anything else.

But that does not prevent me from getting associations.

And it does not prevent me from wondering what people
think when they read my code.

Arne
Kevien Lee - 15 Nov 2006 03:44 GMT
what the code is shorter,the coder is smarter:)

> Hello!
>
[quoted text clipped - 20 lines]
> Anders Borum / SphereWorks
> Microsoft Certified Professional (.NET MCP)
Dan Bass - 15 Nov 2006 10:48 GMT
So... all in all, seems I was outnumbered 10-1!
Go for the "!"

> Hello!
>
[quoted text clipped - 15 lines]
> Thinking about it, we've had this discussion at the office several times,
> and I've never seen any public discussion on the subject.
tjmadden1128@gmail.com - 15 Nov 2006 18:21 GMT
Make it 11-1!
I consider it a standard practice. I've had one or two languages where
the ! operator was not defined, and I always thought it was odd.

> So... all in all, seems I was outnumbered 10-1!
> Go for the "!"
[quoted text clipped - 23 lines]
> > Anders Borum / SphereWorks
> > Microsoft Certified Professional (.NET MCP)
Arne Vajhøj - 17 Nov 2006 02:16 GMT
> Make it 11-1!
> I consider it a standard practice. I've had one or two languages where
> the ! operator was not defined, and I always thought it was odd.

Yes.

I think we can say that the common practice is to use !.

Now
   best placement of {
and
   for(;;) versus while(true)
would probably be much harder to agree on.

Arne
Bobbo - 17 Nov 2006 09:33 GMT
Arne Vajh?j wrote:

>     for(;;) versus while(true)
> would probably be much harder to agree on.

Hmm... Surely while(true) is much clearer?
Ben Newsam - 17 Nov 2006 09:44 GMT
>Now
>    best placement of {

I always put { and } in pairs above one another. Putting it at the end
of the line saves one line in the file, but is less clear, IMO.
Personally, I don't indent the brackets, but I have seen it done so
that the brackets line up with the contents. Looked OK, but I just
don't do it. Unless it is the house style, of course, lol.
   
>and
>    for(;;) versus while(true)

The latter is clearer, IMO. Also, it is "purer" in structured
programming terms, it describes a loop and actually has an "end
condition" rather than relying on the almost accidental way that the
for loop works.

>would probably be much harder to agree on.

Indeed.

Signature

Posted via a free Usenet account from http://www.teranews.com

Ajith Menon - 18 Nov 2006 19:15 GMT
Gr8 thread ... got to know how different developers think.

:-)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.