Avoiding Kotlin Generics with Extensions (Bowling Part 5)

Опубликовано: 09 Февраль 2024
на канале: Pairing with Duncan
811
33

We’re going to take a little detour in our bowling game project to address feedback from friend of the channel, Tim Schraepen (   / @timschraepen  .

Firstly we’ll see if we can improve the readability of our tests. Tests often reflect the combined complexity of the code they are testing. If the tests themselves are too complex then bugs can hide there, so we should strive to make them as expressive as possible.

Secondly we’ll look at forbidding knocking down say 11 pins in a frame. I don’t think that we can do that in Kotlin just using the type system, so we are going to have to resort to runtime checks, but even they turn out to be convoluted. In the end I stumble on a solution that mixes extension functions with methods to bend the types without too much complexity, but let me know in the comments if you have a nicer solution.

In this episode

00:00:58 Are our tests at an appropriate level?
00:01:24 Can we make them more readable through refactoring?
00:05:52 We can now simplify our tests even more
00:07:07 List of lines is a better representation than a single string
00:08:12 Checkin
00:08:18 Addressing Illegal rolls
00:10:51 Adding constraints to our base Frame type
00:12:21 Debugging our Frame construction lifecycle
00:15:06 But what about where the constraint is different in the subtypes?
00:16:05 Let's start again
00:16:17 Try to use types to enforce the range constraint
00:17:03 Different constraints then need different type
00:21:01 Finally compiling
00:21:50 Check in
00:21:58 We tried to solve a problem using generics. Now we have two problems
00:22:29 Let's start again, again
00:22:57 Use types to enforce the constraint, but only in subtypes of Frame
00:23:31 What we want is structural typing
00:23:43 which we can simulate with an extension
00:25:03 We now need to make totalPinCount evaluated for every Frame on creation
00:26:23 Review

You can see the code on GitHub https://github.com/dmcg/bowling-for-y... and this miniseries in a playlist    • Bowling Kata  

If you like this, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style.

Thank you to Orion Williams http://toolofgod.com/my-music/royalty... for the music.


Смотрите видео Avoiding Kotlin Generics with Extensions (Bowling Part 5) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Pairing with Duncan 09 Февраль 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 811 раз и оно понравилось 33 людям.