• 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: Isak Styf <email@hidden>
  • Date: Tue, 27 May 2008 21:31:52 +0200


With Steve I am on this one.

if (CONSTANT == variable) is like saying "if red is the house" or "if a car is the vehicle".

When Yoda says things like that its hilarious, when you are trying to read code it is just a pain.

I often find that writing code that way is just an excuse for not getting/using better tools.

    /// Isak

27 maj 2008 kl. 16.52 skrev Steve Christensen:

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


References: 
 >Re: assignment in if clause (From: Scott Ribe <email@hidden>)
 >Re: assignment in if clause (From: Kevin Grant <email@hidden>)
 >Re: assignment in if clause (From: Steve Christensen <email@hidden>)

  • Prev by Date: Re: NSSearchField and Key detection
  • Next by Date: [moderator] Re: Cocoa-dev Digest, Vol 5, Issue 919
  • Previous by thread: Re: assignment in if clause
  • Next by thread: Re: assignment in if clause
  • Index(es):
    • Date
    • Thread