Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster...

75
Trisha Gee (@ trisha_gee ) Developer & Technical Advocate, JetBrains Refactoring to Java 8

Transcript of Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster...

Page 1: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Trisha Gee (@trisha_gee)

Developer & Technical Advocate, JetBrains

Refactoring to Java 8

Page 2: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Why Java 8?

Page 3: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

It’s Faster

•Performance Improvements in Common Data Structures

•Fork/Join Speed Improvements

•Changes to Support Concurrency

•…and more

http://bit.ly/refJ8

Page 4: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Easy to Parallelize

Page 5: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Fewer Lines of Code

Page 6: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

New Solutions to Problems

Page 7: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Minimizes Errors

Page 8: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Safety Check

Page 9: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Test Coverage

Page 10: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Performance Tests

Page 11: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Decide on the Goals

Page 12: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Limit the Scope

Page 13: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Morphia

https://github.com/mongodb/morphia

Page 14: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Refactoring!

Page 15: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Lambda Expressions

Page 16: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Automatic Refactoring

•Predicate

•Comparator

•Runnable

•etc…

Page 17: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Abstract classes

Page 18: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Advanced Search

Page 19: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Collections & Streams API

Page 20: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Automatic Refactoring

•For loop to collect

•For loop to forEach

•…with and without Streams

Page 21: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Manual Refactoring

Page 22: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Optional

Page 23: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring
Page 24: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

But what about performance?

Page 25: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Lambdas

Page 26: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

20

40

60

80

100

120

140

160

180O

ps/

ms

Anonymous Inner Classes vs Lambdas

Anonymous Inner Class Lambda

Page 27: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf

0

2

4

6

8

10

12

14

16

18

20

single thread max threads

nse

c/o

p

Performance of Capture

anonymous(static) anonymous(non-static) lambda

Page 28: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

50,000

100,000

150,000

200,000

250,000

300,000

350,000

400,000

450,000

500,000

Constant message Variable message

Op

s/m

sLogging Performance

Direct call Lambda

Page 29: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Streams vs Iteration

Page 30: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

1

2

3

4

5

6

7

8

9

Op

s/m

sIterator vs Stream (1000 elements)

for loop forEach()

Page 31: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

1000

2000

3000

4000

5000

6000

7000

1 10 100 1000

Op

s/m

sIterHelper

original simplified refactored

Page 32: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

1

2

3

4

5

6

7

8

9

1000 10000 100000

Op

s/m

sIterHelper

original simplified refactored

Page 33: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

BasicDAO – map & collect

Page 34: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

5000

10000

15000

20000

25000

1 10 100 1000

Op

s/m

s

BasicDAO

original simplified refactored

Page 35: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

DuplicatedAttributeNames – filter, map & collect

Page 36: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

200

400

600

800

1000

1200

Op

s/m

sDuplicatedAttributeNames

original refactored

Page 37: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

EntityScanner– forEach

Page 38: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

0.01

0.02

0.03

0.04

0.05

0.06

Op

s/m

sEntityScanner

original refactored

Page 39: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

DatastoreImpl – filter & forEach

Page 40: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Op

s/m

sDatastoreImpl

original refactored

Page 41: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

MappingValidator – single stream operation

Page 42: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

100

200

300

400

500

600

700

800

900

EntityWithOneError EntityWith10Errors EntityWith20Errors

Op

s/m

sMappingValidator

original refactored

Page 43: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

QueryImpl – multiple operations

Page 44: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

500

1000

1500

2000

2500

3000

3500

Op

s/m

sQueryImpl

original simplified refactored

Page 45: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Going parallel

Page 46: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

Serial Parallel

Tim

e Ta

ken

(se

con

ds)

map()

MacBook Surface

Page 47: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

20

40

60

80

100

120

140

160

Serial Parallel

Tim

e Ta

ken

(m

illis

)findAny()

MacBook Surface

Page 48: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Optional

Page 49: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

50,000

100,000

150,000

200,000

250,000

300,000

350,000

Value not null Value null

Op

s/m

sCompare Constant Field Value with Null

Null check Optional

Page 50: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

50,000

100,000

150,000

200,000

250,000

300,000

Value not null Value null

Op

s/m

sCompare Variable Field Value with Null

Null check Optional

Page 51: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Summary

Page 52: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Sometimes new idioms decrease clutter

Page 53: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring
Page 54: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

…but sometimes they don’t

Page 55: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring
Page 56: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Sometimes the new features improve performance

Page 57: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

20

40

60

80

100

120

140

160

180O

ps/

ms

Anonymous Inner Classes vs Lambdas

Anonymous Inner Class Lambda

Page 58: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

…and sometimes they don’t

Page 59: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

0

200

400

600

800

1000

1200

Op

s/m

sDuplicatedAttributeNames

original refactored

Page 60: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Sometimes a new feature makes life easier

Page 61: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring
Page 62: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

…sometimes not so much

Page 63: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring
Page 64: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Some refactoring can safely be done automatically

Page 65: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring
Page 66: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

…often you need a human brain

Page 67: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring
Page 68: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Conclusion

Page 69: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Should you migrate your code to Java 8?

Page 70: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

It Depends

Page 71: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Always remember what your goal is

And compare results to it

Page 72: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Understand what may impact performance

And if in doubt, measure

Page 73: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Code may magically improve

Or you may expose areas for improvement

Page 74: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

Your tools can help you

But you need to apply your brain too

Page 75: Refactoring to Java 8 - QCon New York · Refactoring to Java 8. Why Java 8? It’s Faster •Performance Improvements in Common Data ... Lambda Expressions. Automatic Refactoring

http://bit.ly/refJ8

@trisha_gee