effective programming in java - kronospan job fair 2016

Post on 15-Jan-2017

135 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Effective programming in Java

Łukasz Koniecki

March 17th, 2016

https://github.com/lkoniecki/EffectiveJava

Development environment

• 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

lukasz.koniecki@gmail.com@lkoniecki

APM?Application

Performance

Management

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

• Learning programming from scratch,

• Learning Java from the beginning,

• Solving all possible Java problems

What this workshop is not about?

Let’s talk about…

Design patterns

Design Patterns bible

POJO with multiple instance members

*POJO – Plain Old Java Object

Telescoping constructor pattern

JavaBean pattern

Builder pattern

Example

• 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

Exercise 1

• 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

Let’s talk about…

Functional programming

Java7-ish vs. stream-like version

But… Is it safe?

Benchmark results

• 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

Let’s talk 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.

What you should know about generics?

Example

• Always try to get into your API consumer shoes,

• Remember the rule:

Producer – <? extends V>

Consumer - <? super V>

Moral

Exercise 2

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

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

• Nicolai Parlog - Stream performance

Credits

gdansk.dynatrace.pl

top related