Java to Kotlin: A Refactoring Guidebook

Java to Kotlin: A Refactoring Guidebook

by Duncan McGregor, Nat Pryce
Java to Kotlin: A Refactoring Guidebook

Java to Kotlin: A Refactoring Guidebook

by Duncan McGregor, Nat Pryce

Paperback

$65.99 
  • SHIP THIS ITEM
    Qualifies for Free Shipping
  • PICK UP IN STORE
    Check Availability at Nearby Stores

Related collections and offers


Overview

It takes a week to travel the 8,000 miles overland from Java to Kotlin. If you're an experienced Java developer who has tried the Kotlin language, you were probably productive in about the same time.

You'll have found that they do things differently in Kotlin, though. Nullability is important, collections are different, and classes are final by default. Kotlin is more functional, but what does that mean, and how should it change the way that you program? And what about all that Java code that you still have to support?

Your tour guides Duncan and Nat first made the trip in 2015, and they've since helped many teams and individuals follow in their footsteps. Travel with them as they break the route down into legs like Optional to Nullable, Beans to Values, and Open to Sealed Classes. Each explains a key concept and then shows how to refactor production Java to idiomatic Kotlin, gradually and safely, while maintaining interoperability.

The resulting code is simpler, more expressive, and easier to change. By the end of the journey, you'll be confident in refactoring Java to Kotlin, writing Kotlin from scratch, and managing a mixed language codebase as it evolves over time.


Product Details

ISBN-13: 9781492082279
Publisher: O'Reilly Media, Incorporated
Publication date: 09/21/2021
Pages: 422
Sales rank: 831,082
Product dimensions: 7.00(w) x 9.19(h) x 0.86(d)

About the Author

Nat and Duncan both started programming in Java before its 1.0 release, and have 55 years of combined experience on both the JVM and other platforms. Until they discovered Kotlin in 2015 Java was their language of choice for most applications.



That changed when they fell in love with JetBrains’ new creation and spread the word, first with their own colleagues and clients, then the JVM community in London, and then internationally through conferences. They have both presented at KotlinConf, where they also run a one day workshop “Refactoring to Kotlin,” which forms the basis for the introductory chapters of this book.

Nat and Duncan both started programming in Java before its 1.0 release, and have 55 years of combined experience on both the JVM and other platforms. Until they discovered Kotlin in 2015 Java was their language of choice for most applications.



That changed when they fell in love with JetBrains’ new creation and spread the word, first with their own colleagues and clients, then the JVM community in London, and then internationally through conferences. They have both presented at KotlinConf, where they also run a one day workshop “Refactoring to Kotlin,” which forms the basis for the introductory chapters of this book.

Table of Contents

Preface xi

1 Introduction 1

The Grain of a Programming Language 1

An Opinionated History of Java Programming Style 4

The Grain of Kotlin 9

Refactoring to Kotlin 11

What Are We Working On? 13

Let's Get Started! 14

2 Java to Kotlin Projects 15

Strategy 15

Adding Kotlin Support to a Java Build 17

Moving On 20

3 Java to Kotlin Classes 21

A Simple Value Type 21

The Limitations of Data Classes 28

Moving On 32

4 Optional to Nullable 35

Representing Absence 35

Refactoring from Optional to Nullable 37

Refactoring to Idiomatic Kotlin 44

Moving On 50

5 Beans to Values 51

Beans 51

Values 52

Why Should We Prefer Values? 53

Refactoring Beans to Values 53

Moving On 60

6 Java to Kotlin Collections 61

Java Collections 61

Kotlin Collections 64

Refactoring from Java to Kotlin Collections 67

Moving On 75

7 Actions to Calculations 77

Functions 77

Calculations 78

Actions 79

Why Should We Care? 80

Why Prefer Calculations? 81

Refactoring Actions to Calculations 82

Moving On 93

8 Static Methods to Top-Level Functions 95

Java Statics 95

Kotlin Top-Level Functions, Objects, and Companions 97

Refactoring from Static Methods to Top-Level Functions 98

Move to Top Level 101

Kotlinify 103

Moving On 105

9 Multi- to Single-Expression Functions 107

Take 1: Mining 109

Take 2: Introduce a Function 110

Take 3: Let 114

Take 4: Stepping Back 116

Moving On 119

10 Functions to Extension Functions 121

Functions and Methods 121

Extension Functions 123

Extensions and Function Types 124

Extension Properties 125

Conversions 125

Nullable Parameters 127

Nullable Receivers 128

Generics 130

Extension Functions as Methods 131

Refactoring to Extension Functions 131

Moving On 145

11 Methods to Properties 147

Fields, Accessors, and Properties 147

How to Choose 151

Mutable Properties 153

Refactoring to Properties 153

Moving On 158

12 Functions to Operators 159

A Foundational Class: Money 159

Adding a User-Defined Operator 161

Calling Our Operator from Existing Kotlin Code 163

Operators for Existing Java Classes 164

Conventions for Denoting Values 165

Moving On 168

13 Streams to Iterables to Sequences 169

Java Streams 169

Kotlin Iterables 171

Kotlin Sequences 172

Multiple Iterations 173

Choosing Between Streams, Iterables, and Sequences 176

Algebraic Transformation 177

Refactoring from Streams to Iterables and Sequences 179

Moving On 191

14 Accumulating Objects to Transformations 193

Calculating with Accumulator Parameters 193

Refactoring to Functions over Immutable Data 198

Let's Do That Again 203

Enriching the Abstraction We Discovered 207

Moving On 208

15 Encapsulated Collections to Type Aliases 211

Composing Domain Collections 213

Collections with Other Properties 214

Refactoring Encapsulated Collections 215

Moving On 224

16 Interfaces to Functions 227

Object-Oriented Encapsulation 228

Functional Encapsulation 231

Function Types in Java 234

Mix and Match 236

Comparing the Approaches 240

Coupling 243

Object-Oriented or Functional? 244

The Legacy of Java 247

Traceability 247

Refactoring from Interfaces to Functions 248

Moving On 254

17 Mocks to Maps 255

Replacing Mocks with Maps 258

Have We Really Weaned Off Mocks, Though? 262

Moving On 265

18 Open to Sealed Classes 267

Polymorphism or Sealed Classes? 271

Converting an Interface to a Sealed Class 272

Moving On 278

19 Throwing to Returning 281

Error Handling Before Exceptions 283

Error Handling with Exceptions 284

Java and Checked Exceptions 284

Kotlin and Exceptions 287

Beyond Exceptions: Functional Error Handling 287

Error Handling in Kotlin 290

Refactoring Exceptions to Errors 291

More Fixup 305

Layers 311

Moving On 313

20 Performing I/O to Passing Data 315

Listening to Tests 315

I/O to Data 317

Efficient Writing 322

Efficient Reading 326

Moving On 329

21 Exceptions to Values 331

Identifying What Can Go Wrong 331

Representing Errors 338

What About I/O? 344

Moving On 346

22 Classes to Functions 347

An Acceptance Test 348

Unit Testing 351

Headers 356

Different Field Separators 363

Sequences 369

Reading from a File 374

Comparison with Commons CSV 379

Moving On 383

23 Continuing the Journey 385

Grain 385

Functional Thinking 386

Simple Design 388

Functional Programming and Textual Reasoning 389

Refactoring 389

Refactoring and Functional Thinking 390

Bibliography 393

Learning Kotlin 393

Testing and Test-Driven Development 393

Working with Legacy Code 393

Software Design and Development 394

Java and the JVM 394

Index 395

From the B&N Reads Blog

Customer Reviews