Kotlin

Refactoring to single-expression function in Kotlin



In this pair programming session Duncan McGregor navigates me through the refactoring steps from the upcoming “Java to Kotlin” book to convert Kotlin function from block body to a single expression.

——————-
Timecodes
——————-
00:00 – Intro
01:27 – Code overview
03:39 – Single-expression/block function demo on EmailAddress.toString()
06:00 – Inline atIndex variable (exploring that fact that we can do it)

Refactoring attempt 1
09:48 – Extract emailAddress() function,…

Similar Posts

6 thoughts on “Refactoring to single-expression function in Kotlin
  1. Kotlin `split` – unlike Java `split` – comes in 2 flavors: one taking a plain string and one taking a regular expression. Thus, `split("@")` will do the right thing in Kotlin, and you do not need `split(Regex("\Q@\E"))` or so.

  2. Timecodes:

    00:00 – Intro
    01:27 – Code overview
    03:39 – Single-expression/block function demo on EmailAddress.toString()
    06:00 – Inline atIndex variable (exploring that fact that we can do it)

    Refactoring attempt 1
    09:48 – Extract emailAddress() function, convert parse() to single expression
    14:20 – Inline require()
    17:38 – Convert emailAddress() to single expression
    19:22 – Refactoring conclusions, revert

    Refactoring attempt 2
    21:00 – Convert parse() to single expression using .let()
    24:42 – Move require() into .also()
    26:40 – Refactoring conclusions, revert

    Refactoring attempt 3
    27:50 – Another look at what parse() function is doing
    30:41 – Extract split() function
    32:42 – Covert parse() to single expression using .let()
    34:00 – Refactor split()
    36:00 – Covert split() to single expression using .let()
    37:03 – Rename split() to splitAround(), make it more generic

    40:25 – What have we learned?
    43:42 – "Java to Kotlin" book

Leave a Reply

Your email address will not be published. Required fields are marked *