I've seen a lot of times code like this:
...
val aString: String = someFunctionDataReturnsAString()
...
if (someCondition) Some(aString)
else
...
Is it right to create a new Option
using Some(aString)
?
What if for some reason turns out the value of aString
is null
?
Wouldn't be good replacing the if
sentence with:
if (someCondition) Option(aString)
Because:
val a = Some("hello")
val b: String = null
val c = Some(b)
val d = Option(b)
println(a)
println(c)
println(d)
Will print in the console:
Some(hello)
Some(null)
None
Looks like a better idea to use Option
when passing a string as parameter, so in case the string is null
the value of the Option
will be None
.
I've seen code to be reviewed and I think with cases like this a comment should be added asking for replacing Some(aString)
with Option(aString)
.