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

Post on 15-Apr-2017

196 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java and effective programming.Is it possible?

Łukasz Koniecki

April 14th, 2016

https://github.com/lkoniecki/EffectiveJava

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

APM

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

What this workshop is not about?

•Learning programming from scratch,

•Learning Java from the beginning,

•Solving all possible Java problems

Design patterns

POJO with multiple instance members

Telescoping constructor pattern

JavaBean pattern

Builder pattern

Example

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.

Exercise 1

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.

Functionalprogramming

Java7-ish vs. stream-like version

But… Is it safe?

Benchmark results

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)

Generics

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.

Example

Moral

•Always try to get into your API consumer shoes,

•Remember the rule:

Producer – <? extends V>

Consumer - <? super V>

Exercise 2

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>());

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

Be the dev we need!

Visit http://gdansk.dynatrace.pl

Credits

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

•Nicolai Parlog - Stream performance

top related