effective programming in java - kronospan job fair 2016

34
Effective programming in Java Łukasz Koniecki March 17 th , 2016

Upload: lukasz-koniecki

Post on 15-Jan-2017

135 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Effective programming in Java - Kronospan Job Fair 2016

Effective programming in Java

Łukasz Koniecki

March 17th, 2016

Page 2: Effective programming in Java - Kronospan Job Fair 2016

https://github.com/lkoniecki/EffectiveJava

Development environment

Page 3: Effective programming in Java - Kronospan Job Fair 2016
Page 4: Effective programming in Java - Kronospan Job Fair 2016
Page 5: Effective programming in Java - Kronospan Job Fair 2016

• Software Architect in the Dynatrace,

• Worked as Software Developer and Team Lead,

• 10+ years of experience with Java – Enterprise Application mostly,

• Main area of interest: optimization and application performance

Whoami

[email protected]@lkoniecki

Page 6: Effective programming in Java - Kronospan Job Fair 2016

APM?Application

Performance

Management

Page 7: Effective programming in Java - Kronospan Job Fair 2016
Page 8: Effective programming in Java - Kronospan Job Fair 2016

• Showing examples on good, better and the best solutions to some common design problems,

• Signaling some commons programming anti-patterns you should avoid,

• Having fun solving simple Java programming puzzles

What this workshop is about?

Page 9: Effective programming in Java - Kronospan Job Fair 2016

• Learning programming from scratch,

• Learning Java from the beginning,

• Solving all possible Java problems

What this workshop is not about?

Page 10: Effective programming in Java - Kronospan Job Fair 2016

Let’s talk about…

Design patterns

Page 11: Effective programming in Java - Kronospan Job Fair 2016

Design Patterns bible

Page 12: Effective programming in Java - Kronospan Job Fair 2016

POJO with multiple instance members

*POJO – Plain Old Java Object

Page 13: Effective programming in Java - Kronospan Job Fair 2016

Telescoping constructor pattern

Page 14: Effective programming in Java - Kronospan Job Fair 2016

JavaBean pattern

Page 15: Effective programming in Java - Kronospan Job Fair 2016

Builder pattern

Page 16: Effective programming in Java - Kronospan Job Fair 2016

Example

Page 17: Effective programming in Java - Kronospan Job Fair 2016

• There are many ways of implementing the same algorithm/functionality,

• There are reusable patterns that you should use (on every possible level: implementation, integration, deployment etc.

The moral

Page 18: Effective programming in Java - Kronospan Job Fair 2016

Exercise 1

Page 19: Effective programming in Java - Kronospan Job Fair 2016

• Given the lists of strings as an input, method groupBy in the Example1 class should return map with all strings that occurred in the input list as key and number of string occurrences as value,

Example:

For a given input list {„John”, „Aaron”, „John” „Adam”, „Adelle”} method should return following map { {„John”, 2}, {„Aaron”, 1}, {„Adam”, 1}, {„Adelle”, 1}}

• Method should throw IllegalArgumentException if elements list is null,

• Method should return empty map if elements list is empty

Specification

Page 20: Effective programming in Java - Kronospan Job Fair 2016

Let’s talk about…

Functional programming

Page 21: Effective programming in Java - Kronospan Job Fair 2016

Java7-ish vs. stream-like version

Page 22: Effective programming in Java - Kronospan Job Fair 2016

But… Is it safe?

Page 23: Effective programming in Java - Kronospan Job Fair 2016

Benchmark results

Page 24: Effective programming in Java - Kronospan Job Fair 2016

• Functional programming is more about coding convenience,

• You should not mix imperative and functional programming styles,

• Developer must be familiar with how streams work and behave (e.g. parallel stream execution),

• Must be used with caution (see: 10 Subtle Mistakes When Using the Streams API)

Moral

Page 25: Effective programming in Java - Kronospan Job Fair 2016

Let’s talk about…

Generics

Page 26: Effective programming in Java - Kronospan Job Fair 2016

• Compile time type check rather than runtime check,

• Generic types are invariant (compare with covariant arrays)

• List<Integer> is not subtype of List<Number>,

• Can be confusing when implementing API contract,

• Use wildcards with caution.

What you should know about generics?

Page 27: Effective programming in Java - Kronospan Job Fair 2016

Example

Page 28: Effective programming in Java - Kronospan Job Fair 2016

• Always try to get into your API consumer shoes,

• Remember the rule:

Producer – <? extends V>

Consumer - <? super V>

Moral

Page 29: Effective programming in Java - Kronospan Job Fair 2016

Exercise 2

Page 30: Effective programming in Java - Kronospan Job Fair 2016

• Implement union method in the Exercise2 class, so that it returns set being an union of two sets passed as an argument,

• Change it’s definition so that the following line of code compiles

• Method should throw IllegalArgumentException if any set passed as an argument is null,

• Remember that producer – super, consumer – extends…

Specification

Set<Number> result = Excercise2.union(new HashSet<Integer>(), new HashSet<Number>());

Page 31: Effective programming in Java - Kronospan Job Fair 2016
Page 32: Effective programming in Java - Kronospan Job Fair 2016
Page 33: Effective programming in Java - Kronospan Job Fair 2016

• Joshua Bloch - Effective programming in Java (2nd edition)

• Nicolai Parlog - Stream performance

Credits

Page 34: Effective programming in Java - Kronospan Job Fair 2016

gdansk.dynatrace.pl