• 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: Steve Christensen <email@hidden>
  • Date: Tue, 27 May 2008 07:52:33 -0700

On May 26, 2008, at 8:37 PM, Kevin Grant wrote:

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

The style I adopted is to place the "most constant" part of an expression first: for example, "if (0 == x)" instead of "if (x == 0)". This generates a compile-time error for accidental assignments.

I don't like the "if (0 == x)" style myself since it just looks bass- ackwards. Of course that's just a style preference.


To prevent myself from unwanted assignments (and a lot of other stupid mistakes) I end up turning on almost every warning option I can. I may start off with a pile of warnings when I first compile, but then I work through them and fix the underlying issues.

So, for example, with all those warnings turned on, if I type "if (a = b)", I get the following in the build results window:

    warning: suggest parentheses around assignment used as truth value


steve

_______________________________________________

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: Isak Styf <email@hidden>
References: 
 >Re: assignment in if clause (From: Scott Ribe <email@hidden>)
 >Re: assignment in if clause (From: Kevin Grant <email@hidden>)

  • Prev by Date: Re: Date compiled?
  • Next by Date: Re: running an external app
  • Previous by thread: Re: assignment in if clause
  • Next by thread: Re: assignment in if clause
  • Index(es):
    • Date
    • Thread