Make your Kotlin code more expressive and assertiv...
# feed
s
Make your Kotlin code more expressive and assertive with these neat tricks! As you can tell from the tone and content of my latest blog post, it’s 100% serious and definitely meant to be taken seriously 👍😄 https://medium.com/@sam-cooper/5-pro-kotlin-dev-hacks-for-more-assertive-code-26cd2db4d7d7
‼️ 13
🤣 4
mind blown 12
😁 1
k
That got me thinking...
String?
means "`null` or `String`". So
String??
means "`null` or `String?
Copy code
".
!!` turns a
T?
to a
T
. Therefore:
Copy code
fun foo(a: String??) {
   val x = a!!
}
x should now be
String?
but it's a
String
!
😁 1
😄 1
x
On rare occasions, people can miss the end of a function. So you can use the
return return return return return yourResult
statement as a delimiter to make it more visible. 🤪
😁 1
blob think smart 2
s
Can’t believe I didn’t think of that one! I like it 😎
k
Or you can throw your readers off the scent by putting
throw return yourResult
when you just want a normal return
👏 4
😁 2
👏🏻 1
😮 2