• 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: get rid of warning "suggest parenthesis around assignment used as truth value"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: get rid of warning "suggest parenthesis around assignment used as truth value"


  • Subject: Re: get rid of warning "suggest parenthesis around assignment used as truth value"
  • From: Howard Moon <email@hidden>
  • Date: Thu, 20 Nov 2008 14:02:14 -0800


On Nov 20, 2008, at 1:31 PM, Jim Correia wrote:

On Nov 20, 2008, at 10:18 AM, Clark S. Cox III wrote:

The fact that I used '=' conveys that I meant to use '=' :)

But seriously, I can't think of a single instance (outside of school) that I've read (or written) code in which someone wrote '=' when they meant '=='.

It seems like such an obvious mistake that, even if/when it does happen, it would be caught almost immediately by running the code or via a unit test.

Our experience differs.

It is a fairly easy typo to make, and is easy to step over the code in the debugger without noticing the assignment side effect.

Worse, when you didn't write the original code, is determining what the author meant.

As such, I discourage assignments inside a conditional, without also making them explicit.

My opinion is that you are writing code for the compiler *and* the next person who is going to come through and read it or debug it. Since I believe an assignment inside of a conditional is ambiguous, it doesn't meet the second goal, regardless of whether it is correct (both logically, and from the compiler's perspective.)


I agree. In that light, my rule has always been to avoid writing code that performs two tasks in one line. I usually write such code like this:


int a = whatever();
if (a)
{
	dosomething();
}

This is safe, obvious, and separates the two tasks (assigning a value to a versus checking its value). Why combine the two? Just to save a couple small lines of code? That's hacker mentality, not good programming.

However, as seems to be missed in all the responses so far is the fact that the code which generated the warning is template code generated in Xcode! Shouldn't generated code be as warning-free as possible? (Perhaps a bug report is in order?)

-Howard




_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: get rid of warning "suggest parenthesis around assignment used as truth value"
      • From: Steve Checkoway <email@hidden>
References: 
 >get rid of warning "suggest parenthesis around assignment used as truth value" (From: Bob Sabiston <email@hidden>)
 >Re: get rid of warning "suggest parenthesis around assignment used as truth value" (From: Sherm Pendley <email@hidden>)
 >Re: get rid of warning "suggest parenthesis around assignment used as truth value" (From: Bob Sabiston <email@hidden>)
 >Re: get rid of warning "suggest parenthesis around assignment used as truth value" (From: Jim Correia <email@hidden>)
 >Re: get rid of warning "suggest parenthesis around assignment used as truth value" (From: "Clark S. Cox III" <email@hidden>)
 >Re: get rid of warning "suggest parenthesis around assignment used as truth value" (From: Jim Correia <email@hidden>)

  • Prev by Date: Re: get rid of warning "suggest parenthesis around assignment used as truth value"
  • Next by Date: building cURL?
  • Previous by thread: Re: get rid of warning "suggest parenthesis around assignment used as truth value"
  • Next by thread: Re: get rid of warning "suggest parenthesis around assignment used as truth value"
  • Index(es):
    • Date
    • Thread