• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: assignment in if clause
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: assignment in if clause


  • Subject: Re: assignment in if clause
  • From: Scott Ribe <email@hidden>
  • Date: Mon, 26 May 2008 14:35:30 -0500
  • Thread-topic: assignment in if clause

> I disagree with the whole notion that using the result of an assignment
> is "bad style" or "confusing".

I agree that it is perfectly clear. But there is still a serious problem
with it: it is too easy to make the typo of if(a=b) when one intended
if(a==b). This can be a surprisingly difficult typo to spot, when one is
staring at the code with the preconceived belief that one typed if(a==b).

There is no way for a compiler to warn one only on those instances where one
did *not* intend to type if(a=b), and that is why many people consider
if(a=b), if not actually "bad style", at least not worth the risk. I like
if(a=b), it's very C-like, as is if(myPtr). But I don't use it, because even
though I don't do that typo very often, it really just isn't worth it to not
be explicit.

The extra parentheses are just a convention that was adopted as a way to
tell the compiler that you really did want to make an assignment and test
the result in one expression. (I personally hate it, because I keep looking
for the "other stuff that should be in the parentheses"; I prefer just going
ahead and comparing to nil/NULL/0 as appropriate.)

> ...man serving machine, not the other way around as it should be.

Uhm, excuse me? I thought we were talking about C programming? Why would
there be any question about who is the master in this scenario?

;-)

--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: assignment in if clause
      • From: Kevin Grant <email@hidden>
References: 
 >Re: assignment in if clause (From: Michael Gardner <email@hidden>)

  • Prev by Date: Re: Loops in Objective C?
  • Next by Date: Re: assignment in if clause
  • Previous by thread: Re: assignment in if clause
  • Next by thread: Re: assignment in if clause
  • Index(es):
    • Date
    • Thread