kodecocodes / objective-c-style-guide

A style guide that outlines the coding conventions for Kodeco
http://www.raywenderlich.com/62570/objective-c-style-guide
3.1k stars 628 forks source link

Ternary operator #17

Closed hollance closed 10 years ago

hollance commented 10 years ago

I suggest we write it as such:

result = (a > b) ? x : y;

In other words, place parentheses around the condition. There are two reasons for this:

  1. It is easier to read
  2. You don't have to worry about operator precedence.
ColinEberhardt commented 10 years ago

Are you happy with no parens for the following?

result = a ? x : y;
funkyboy commented 10 years ago

if a is a BOOL, no prob

On Fri, Nov 8, 2013 at 9:43 AM, ColinEberhardt notifications@github.comwrote:

Are you happy with no parens for the following?

result = a ? x : y;

— Reply to this email directly or view it on GitHubhttps://github.com/raywenderlich/objective-c-style-guide/issues/17#issuecomment-28047149 .

Cesare Rocchi http://studiomagnolia.com

hollance commented 10 years ago

I'm happy with a ? x : y if a is a BOOL, but I don't like it when I see this:

int a = ...
result = a ? x : y;

In other words, non-booleans should always compare against something:

result = (a != 0) ? x : y;