Swift-style-guide: One line if-else statement

Created on 28 May 2016  路  3Comments  路  Source: raywenderlich/swift-style-guide

I have a question regarding the one liner if statements, as I have not found it mentioned in the guide. Is it okay to be using something like:

var playerMark = player == 1 ? "x" : "o"

Thanks!

Most helpful comment

I prefere to put all ternary operators inside (...). It looks much more readable for me:

let playerMark = ( player == 1 ? "x" : "o" )      // lets var be let

All 3 comments

Personally I am a fan. I am a little less of a fan of nested ternary one liners. BTW, I think that should be a let :)

I prefere to put all ternary operators inside (...). It looks much more readable for me:

let playerMark = ( player == 1 ? "x" : "o" )      // lets var be let

Are there not cases when using this would be bad.
I came across this code in a repo I contribute to and I find this:

hostNameLabel.text = host.name == nil || (host.name!.isEmpty) ? "DetailsDetails.NoName".localized : host.name

a lot less readable than this

if host.name == nil || host.name!.isEmpty {
  "DetailsDetails.NoName".localized
} else {
  host.name
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

icanzilb picture icanzilb  路  6Comments

sima-11 picture sima-11  路  5Comments

samkim102 picture samkim102  路  6Comments

hollance picture hollance  路  28Comments

rwenderlich picture rwenderlich  路  29Comments