Refactor Mercilessly? – Bowling Part 8

I believe that, apart from one wafer-thin bug, our 10 pin bowling code is complete. It passes all its tests, and has all the features that it needs.

In software though, complete rarely means finished. It’s a fact of life that because jobs often take longer than we anticipated, we feel pressure to say that they are done, even when they are not as polished as we would like them to be. Even here, where it’s my job to personify programming prowess, I feel I should wrap up an episode before you get bored.

To counteract this, I find it helpful to return to code once it is working, but after at least a night’s sleep. I check that I haven’t missed anything with fresh eyes. A sort of personal PR review if you like, but without the ceremony, one-upmanship and soul-sucking delay.

In this episode

  • 00:00:58 Review code from the outside in
  • 00:01:04 Combine two when cases
  • 00:01:37 Reorder when by converting to and from if
  • 00:02:04 Convert overwhealming Game constructor to extension
  • 00:02:56 Move PlayableGame constructor logic to PlayableLine
  • 00:04:13 Add a test for PlayableGame precondition
  • 00:05:21 Existing lower level preconditions will do
  • 00:06:27 IntelliJ Safe Delete bug
  • 00:07:23 We could hide implementation behind interfaces
  • 00:07:39 Commit
  • 00:07:49 Reviewing Lines
  • 00:08:23 Inconclusive thoughts about object equality
  • 00:09:19 Amend commit
  • 00:09:22 Tidy PlayableLine constructor
  • 00:10:38 Avoiding an odd cast with filterInstanceOf
  • 00:11:21 Review where specifying variable types helps and where it doesn’t
  • 00:11:38 Checkin
  • 00:11:43 Review Frames
  • 00:12:07 Try (and fail) to define PinCount.equals(Int)
  • 00:13:01 How well are inline classes working?
  • 00:13:09 Lets try to read the bytecode to find out
  • 00:14:42 Back to sensible code
  • 00:15:00 Delegating sums to PinCount and Score
  • 00:19:02 Commit
  • 00:19:18 Refactoring extension with is check to method
  • 00:22:43 Extract Frame.score as an extension
  • 00:23:17 Intellij can’t extract with overloaded operators?
  • 00:25:17 We have a bug report!
  • 00:25:30 Create tests to show the problem
  • 00:26:54 Fix by disqualifying illegal states in the bonus final frames
  • 00:28:56 Check in
  • 00:29:03 Add a test for a whole game of strikes
  • 00:29:30 Get all the games by folding over the rolls
  • 00:32:24 Our bowling is in fact event sourcing

You can see the code on GitHub https://github.com/dmcg/bowling-for-youtube and this miniseries in a playlist https://www.youtube.com/playlist?list=PL1ssMPpyqocjSQbAigPgMpDzLMxMc7kSv

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.

submitted by /u/dmcg
[link] [comments]