KotlinConf 2018 - Representing State: the Kotlin Edition by Christina Lee
Video Statistics and Information
Channel: JetBrainsTV
Views: 22,061
Rating: 4.7196579 out of 5
Keywords: Kotlin, KotlinConf18, KotlinConf 2018, Java, JetBrains, Amsterdam, Pinterest
Id: -lVVfxsRjcY
Channel Id: undefined
Length: 42min 47sec (2567 seconds)
Published: Mon Oct 08 2018
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
So many people praised this talk on Twitter. Can't wait to see it. Christina is honestly so good.
This is something Kotlin is definitely useful for. The talk reminded me of a Domain Driven Design talk from a couple of years ago. It discusses DDD in F#, but a lot of the concepts apply to Kotlin as well. If you liked Christina's talk, I recommend having a look at this one as well!
Bottom line: Make illegal states unrepresentable!
πNice talk with some good examples. Lots of problems tend to evaporate when you write good models.
Conversely, bad models amplify confusion and produce more bugs.
Great talk overall, however I find the solution to problem #4 (at 38:04) less than ideal, or maybe incomplete.
The problem is that enforcing the check at every access will inevitably lead to many redundant checks, in situations where the video data will always be loaded.
Of course, as mentioned in the talk, relying on such assumptions ("the video data will always be loaded here, because of some UI event sequence") that are not enforced by the compiler is bad, because they can change over time as the code gets refactored.
But having too many unnecessary checks is a problem of its own. They obscure the logic and make the code less readable - the reader now has to think "when will this situation occur? can it even occur?". And because the person writing the code still has the expectation that the failure branch will most likely never be executed (but they have to write it anyway), the code that's handling it may not be the best way to deal with the situation once other parts of the codebase change and the branch becomes reachable. It's defensive programming, with all of its tradeoffs.
A better solution would be to re-design the code in a way where the compiler can prove our assumptions, which is usually done by separating the part where the video data is possibly uninitialized, from the part where it will always exist, so that we can only initialize the second part once we have the data (i.e. the second part is a class or a closure that holds a non-nullable reference to the data itself). Basically splitting the code along state lines and initializing things at appropriate times. All the code that counts on the data existing should then only go into the second part, and all the checks that remain in the first part will be real, necessary checks, where we know exactly how the "data missing" branch can occur (and can reproduce it if we want).
Of course, Android being what it is, this may not always be feasible and we often have to make do with sub-optimal solutions. I just see the "just enforce checks everywhere" solution as kind of giving up.
But again, that's the only part I have a small issue with, the rest of the talk was great.
Oh man I'll watch this when I canEDIT: I've watched it. It's just as informative as I expected. In these previous few weeks I've actually been wondering about just how crazy it is that we have
if(
statements where just adding a single!
can make your code execute the exact opposite of what you wanted. Crazy room for error!Naming state with sealed data class, even in these "simple-looking" cases, seems like a pretty cool way of enforcing the contract rather than messing up with an infinite combination of
&&
s and||
s, which may or may not do what you intended. Sure, boolean logic - but one character error and it's broken to bits.And of course, this pretty much is only addressing scene 1 :D there are all the others
I'm surprised she did not mention inline classes. I know this feature is only coming in Kotlin 1.3. But it is so relevant to the talk.
Edit: great talk btw
Very solid concepts that apply regardless of language. I find myself applying these sorts of concepts to restrict the possibility of illegal states as a Java-only programmer all the time. I highly suggest anyone to watch this regardless of whether you use Kotlin or not.
I'm going to watch this as soon as possible, thanks for sharing :)
Correct Horse Battery Stinger