Refactoring to Java 8 (QCon New York)

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

Transcript of Refactoring to Java 8 (QCon New York)

Page 1: Refactoring to Java 8 (QCon New York)

Trisha Gee (@trisha_gee)

Developer & Technical Advocate, JetBrains

Refactoring to Java 8

Page 2: Refactoring to Java 8 (QCon New York)

Why Java 8?

Page 3: Refactoring to Java 8 (QCon New York)

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)

Easy to Parallelize

Page 5: Refactoring to Java 8 (QCon New York)

Fewer Lines of Code

Page 6: Refactoring to Java 8 (QCon New York)

New Solutions to Problems

Page 7: Refactoring to Java 8 (QCon New York)

Minimizes Errors

Page 8: Refactoring to Java 8 (QCon New York)

Safety Check

Page 9: Refactoring to Java 8 (QCon New York)

Test Coverage

Page 10: Refactoring to Java 8 (QCon New York)

Performance Tests

Page 11: Refactoring to Java 8 (QCon New York)

Decide on the Goals

Page 12: Refactoring to Java 8 (QCon New York)

Limit the Scope

Page 13: Refactoring to Java 8 (QCon New York)

Morphia

https://github.com/mongodb/morphia

Page 14: Refactoring to Java 8 (QCon New York)

Refactoring!

Page 15: Refactoring to Java 8 (QCon New York)

Lambda Expressions

Page 16: Refactoring to Java 8 (QCon New York)

Automatic Refactoring

•Predicate

•Comparator

•Runnable

•etc…

Page 17: Refactoring to Java 8 (QCon New York)

Abstract classes

Page 18: Refactoring to Java 8 (QCon New York)

Advanced Search

Page 19: Refactoring to Java 8 (QCon New York)

Collections & Streams API

Page 20: Refactoring to Java 8 (QCon New York)

Automatic Refactoring

•For loop to collect

•For loop to forEach

•…with and without Streams

Page 21: Refactoring to Java 8 (QCon New York)

Manual Refactoring

Page 22: Refactoring to Java 8 (QCon New York)

Optional

Page 23: Refactoring to Java 8 (QCon New York)
Page 24: Refactoring to Java 8 (QCon New York)

But what about performance?

Page 25: Refactoring to Java 8 (QCon New York)

Lambdas

Page 26: Refactoring to Java 8 (QCon New York)

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)

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)

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)

Streams vs Iteration

Page 30: Refactoring to Java 8 (QCon New York)

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)

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)

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)

BasicDAO – map & collect

Page 34: Refactoring to Java 8 (QCon New York)

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)

DuplicatedAttributeNames – filter, map & collect

Page 36: Refactoring to Java 8 (QCon New York)

0

200

400

600

800

1000

1200

Op

s/m

sDuplicatedAttributeNames

original refactored

Page 37: Refactoring to Java 8 (QCon New York)

EntityScanner– forEach

Page 38: Refactoring to Java 8 (QCon New York)

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)

DatastoreImpl – filter & forEach

Page 40: Refactoring to Java 8 (QCon New York)

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)

MappingValidator – single stream operation

Page 42: Refactoring to Java 8 (QCon New York)

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)

QueryImpl – multiple operations

Page 44: Refactoring to Java 8 (QCon New York)

0

500

1000

1500

2000

2500

3000

3500

Op

s/m

sQueryImpl

original simplified refactored

Page 45: Refactoring to Java 8 (QCon New York)

Going parallel

Page 46: Refactoring to Java 8 (QCon New York)

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)

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)

Optional

Page 49: Refactoring to Java 8 (QCon New York)

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)

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)

Summary

Page 52: Refactoring to Java 8 (QCon New York)

Sometimes new idioms decrease clutter

Page 53: Refactoring to Java 8 (QCon New York)
Page 54: Refactoring to Java 8 (QCon New York)

…but sometimes they don’t

Page 55: Refactoring to Java 8 (QCon New York)
Page 56: Refactoring to Java 8 (QCon New York)

Sometimes the new features improve performance

Page 57: Refactoring to Java 8 (QCon New York)

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)

…and sometimes they don’t

Page 59: Refactoring to Java 8 (QCon New York)

0

200

400

600

800

1000

1200

Op

s/m

sDuplicatedAttributeNames

original refactored

Page 60: Refactoring to Java 8 (QCon New York)

Sometimes a new feature makes life easier

Page 61: Refactoring to Java 8 (QCon New York)
Page 62: Refactoring to Java 8 (QCon New York)

…sometimes not so much

Page 63: Refactoring to Java 8 (QCon New York)
Page 64: Refactoring to Java 8 (QCon New York)

Some refactoring can safely be done automatically

Page 65: Refactoring to Java 8 (QCon New York)
Page 66: Refactoring to Java 8 (QCon New York)

…often you need a human brain

Page 67: Refactoring to Java 8 (QCon New York)
Page 68: Refactoring to Java 8 (QCon New York)

Conclusion

Page 69: Refactoring to Java 8 (QCon New York)

Should you migrate your code to Java 8?

Page 70: Refactoring to Java 8 (QCon New York)

It Depends

Page 71: Refactoring to Java 8 (QCon New York)

Always remember what your goal is

And compare results to it

Page 72: Refactoring to Java 8 (QCon New York)

Understand what may impact performance

And if in doubt, measure

Page 73: Refactoring to Java 8 (QCon New York)

Code may magically improve

Or you may expose areas for improvement

Page 74: Refactoring to Java 8 (QCon New York)

Your tools can help you

But you need to apply your brain too

Page 75: Refactoring to Java 8 (QCon New York)

http://bit.ly/refJ8

@trisha_gee