Skip navigation.
 
mlRe: assignment in if clause (was: alternate pattern for object initializers)
FROM : Jonathan Prescott
DATE : Mon May 26 20:49:06 2008

It's really the same issue as the "if" statement.  To be pedantic, 
the "while" statement should read:

   while( (instance=[someEnumerator nextObject]) != nil )

Jonathan

On May 26, 2008, at 2:06 PM, Stuart Malin wrote:

>
> On May 26, 2008, at 7:13 AM, Jens Alfke wrote:
>

>>

>>> I tend to write:
>>>
>>> if (self = [super init]) {
>>>     // do stuff
>>> }
>>> return self;

>>
>> Putting assignments in 'if' statements is bad style, because it's 
>> very easy to get them mixed up with the more common equality 
>> tests. In fact, putting '=' instead of '==' in an 'if' is a common 
>> enough error that GCC has compiler warnings for it, so code like 
>> the above will actually generate warnings if someone turns on 
>> those flags. For that reason, I would avoid using that syntax. A 
>> workaround for that is
>>     if ( (self = [super init]) != NULL )
>> but I avoid that too. I just find that putting assignments into 
>> conditionals at all makes the code confusing; but YMMV.

>
> Since I hadn't received any warnings about embedded assignments in 
> if statement conditions, I realized I must not have all warnings 
> turned on... and so added -Wall to my project settings compiler 
> flags. Well, that humbled me because my next compile had more than 
> 30 warnings. These were easily and swiftly cleaned up, but my doing 
> so leads to another idiom/convention question:
>
> With -Wall enabled, the compiler complained about
>
>     while (instance = [someEnumerator nextObject]) {
>
> so I changed that (in quite a few places) to:
>
>     while ((instance = [someEnumerator nextObject])) {
>
> (the compiler is satisfied with this).
>
> Even though the double parens make clear that I had intended an 
> assignment, it looks odd, and I can't recall ever seeing anything 
> like this -- even in numerous example code snippets in the docs 
> that use enumerators. So, my question is, is this double paren 
> idiom good, or should I code such while loops in yet some other way?
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/jprescott12%
> 40comcast.net
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlalternate pattern for object initializers Stuart Malin May 26, 07:47
mlRe: alternate pattern for object initializers Kyle Sluder May 26, 08:51
mlRe: alternate pattern for object initializers Stuart Malin May 26, 09:25
mlRe: alternate pattern for object initializers Kevin Grant May 26, 09:28
mlRe: alternate pattern for object initializers Michael Gardner May 26, 09:59
mlRe: alternate pattern for object initializers Hamish Allan May 26, 11:01
mlRe: alternate pattern for object initializers Jens Alfke May 26, 19:13
mlassignment in if clause (was: alternate pattern for object initializers) Stuart Malin May 26, 20:06
mlRe: assignment in if clause (was: alternate pattern for object initializers) Hamish Allan May 26, 20:40
mlRe: assignment in if clause (was: alternate pattern for object initializers) Jonathan Prescott May 26, 20:49
mlRe: assignment in if clause Michael Gardner May 26, 21:12
mlRe: assignment in if clause Scott Ribe May 26, 21:35
mlRe: assignment in if clause Hamish Allan May 26, 21:38
mlRe: assignment in if clause Kevin Grant May 27, 05:37
mlRe: assignment in if clause Scott Ribe May 27, 15:46
mlRe: assignment in if clause Steve Christensen May 27, 16:52
mlRe: assignment in if clause Isak Styf May 27, 21:31