Refactoring to single-expression function in Kotlin

preview_player
Показать описание
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
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

---------
Links
---------

#Refactoring #Kotlin #FunctionalProgramming #PairProgramming #LiveCoding
Рекомендации по теме
Комментарии
Автор

Thanks guys, I really enjoyed watching you work through this. Kotlin + Intellij is a wonderful thing!

ocon
Автор

Thanks Dmitry for these videos. I find them really interesting, particularly the pairing sessions with guests.

regularju
Автор

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

DmitryKandalov
Автор

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.

NorbertKiesel
Автор

Is the "real" answer not to make "parse" a second constructor, and implement it using `val parts = value.split("@"); require(parts.size == 2)`

NorbertKiesel
visit shbcf.ru