java and effective programming. is it possible? - iaeste case week 2016

34
Java and effective programming. Is it possible? Łukasz Koniecki April 14th, 2016

Upload: lukasz-koniecki

Post on 15-Apr-2017

196 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Java and effective programming.Is it possible?

Łukasz Koniecki

April 14th, 2016

Page 2: Java and effective programming. Is it possible? - IAESTE Case Week 2016

https://github.com/lkoniecki/EffectiveJava

Page 3: Java and effective programming. Is it possible? - IAESTE Case Week 2016
Page 4: Java and effective programming. Is it possible? - IAESTE Case Week 2016
Page 5: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Whoami

•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

Page 6: Java and effective programming. Is it possible? - IAESTE Case Week 2016

APM

Page 7: Java and effective programming. Is it possible? - IAESTE Case Week 2016
Page 8: Java and effective programming. Is it possible? - IAESTE Case Week 2016

What this workshop is about?

•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

Page 9: Java and effective programming. Is it possible? - IAESTE Case Week 2016

What this workshop is not about?

•Learning programming from scratch,

•Learning Java from the beginning,

•Solving all possible Java problems

Page 10: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Design patterns

Page 11: Java and effective programming. Is it possible? - IAESTE Case Week 2016
Page 12: Java and effective programming. Is it possible? - IAESTE Case Week 2016

POJO with multiple instance members

Page 13: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Telescoping constructor pattern

Page 14: Java and effective programming. Is it possible? - IAESTE Case Week 2016

JavaBean pattern

Page 15: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Builder pattern

Page 16: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Example

Page 17: Java and effective programming. Is it possible? - IAESTE Case Week 2016

The moral

• 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.

Page 18: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Exercise 1

Page 19: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Specification

• 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”, „Adele”} method should return following map { {„John”, 2}, {„Aaron”, 1}, {„Adam”, 1}, {„Adele”, 1}}

• Method should throw IllegalArgumentException if elements list is null,

• Method should return empty map if elements list is empty.

Page 20: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Functionalprogramming

Page 21: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Java7-ish vs. stream-like version

Page 22: Java and effective programming. Is it possible? - IAESTE Case Week 2016

But… Is it safe?

Page 23: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Benchmark results

Page 24: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Moral

• 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)

Page 25: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Generics

Page 26: Java and effective programming. Is it possible? - IAESTE Case Week 2016

What you should know about generics?

•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.

Page 27: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Example

Page 28: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Moral

•Always try to get into your API consumer shoes,

•Remember the rule:

Producer – <? extends V>

Consumer - <? super V>

Page 29: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Exercise 2

Page 30: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Specification

• 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…

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

Page 31: Java and effective programming. Is it possible? - IAESTE Case Week 2016
Page 32: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Java/C++/Python/JavaScriptis your favorite?

Be the dev we need!

Visit http://gdansk.dynatrace.pl

Page 33: Java and effective programming. Is it possible? - IAESTE Case Week 2016

Credits

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

•Nicolai Parlog - Stream performance

Page 34: Java and effective programming. Is it possible? - IAESTE Case Week 2016