https://kotlinlang.org logo
#announcements
Title
# announcements
w

wathek

09/13/2019, 2:52 PM
Hi guys, I'm facing a dilemma and I need your advice. Which one of these two lines you think is better:
val parsedValue = if (propertyValue is String) propertyValue.toDouble() else propertyValue as Double
VS
val parsedValue = (propertyValue as? String)?.toDouble() ?: propertyValue as Double
s

Shawn

09/13/2019, 3:21 PM
first
1
but what type is
propertyValue
even
w

wathek

09/13/2019, 3:21 PM
@Shawn any reason why you think it's better ?
s

Shawn

09/13/2019, 3:22 PM
looking at the statements in a vacuum I like the first better because it’s much clearer what’s happening
💯 1
w

wathek

09/13/2019, 3:22 PM
@Shawn
propertyValue
is Any
s

Shawn

09/13/2019, 3:22 PM
are you okay with that potential ClassCastException lying in wait lol
w

wathek

09/13/2019, 3:23 PM
yes I'm ok with that potential ClassCastException that can come from
as Double
m

Mike

09/13/2019, 3:50 PM
Agree with Shawn. The second one takes a lot of 'processing' to figure out what is going on. The first is obvious.
👍 2