effective java

Post on 15-Jan-2017

316 views 4 download

transcript

Globalcode – Open4education

Trilha – Android

Bruno Fernandes CamposDesenvolvedor Mobile

EFFECTIVE JAVA

Globalcode – Open4education

About

Globalcode – Open4education

Why?

Globalcode – Open4education

How?

Globalcode – Open4education

Agenda

• What the difference in performance between for loops?

• How to deal with several parameters classes?• How to make static classes safe?• How to avoid the unecessary objects creation?

• Inside classes• Inside for loops• When concatenating strings

Globalcode – Open4education

Comparing For Loops

Globalcode – Open4education

For Loops In Arrays

Manual for-loop x Enhanced for-loop

Globalcode – Open4education

For Loops In Arrays

Manual for-loop x Enhanced for-loop

SAME PERFORMANCEWHEN READING ENTRANCES

Globalcode – Open4education

For loops in CollectionsFor each X Iterator

Globalcode – Open4education

For loops in CollectionsFor each X Iterator

SAME PERFORMANCE

Globalcode – Open4education

For loops in ArrayListsManual for-loop X For each

Globalcode – Open4education

For loops in ArrayLists

X For eachManual for-loop

Globalcode – Open4education

For loops in ArrayLists

X

1061 ms 1183 ms

For each

100000 strings logged in the console performance test

Manual for-loop

Globalcode – Open4education

Many Constructors Parameters

What should I do?

Globalcode – Open4education

Nutrition Facts Class

Globalcode – Open4education

Telescoping Pattern

Globalcode – Open4education

Using Telescoping Constructor Pattern

● Force some unwanted values● It does not scale well!● Parameters reversion

Globalcode – Open4education

Javabeans Pattern

Globalcode – Open4education

Javabeans Pattern

● Easy to read● Precludes the possibility of making a class imutable● Imutability -> "All of the information contained in each instance is

provided when it is created and is fixed for the lifetime of the object"

Globalcode – Open4education

The builder pattern!

Globalcode – Open4education

Using Builder Pattern

● Easy to read and Write● Safe -> Imutable

Globalcode – Open4education

How to turn static classes safe

Globalcode – Open4education

Static class

Globalcode – Open4education

AndroidAPIUtils Usage

Globalcode – Open4education

Wrong usage

Globalcode – Open4education

AndroidAPIUtils avoiding instantiability

Globalcode – Open4education

Avoid Creating Unecessary Objects

Globalcode – Open4education

Prefeer Static Factory Methods

Constructors will always create a new instance!

Globalcode – Open4education

INSIDE CLASSES

Avoid Creating Unecessary Objects

Globalcode – Open4education

Bad example

Globalcode – Open4education

Use Static initializers

Globalcode – Open4education

Why use static initialisers?● Calendar, Date and Timezone

instances created only once

● Executing 10 milion times : ○ 1st version took 32000ms○ 2nd version took 130 ms

250 times faster!

Globalcode – Open4education

INSIDE FOR LOOPS

Avoid Creating Unecessary Objects

Globalcode – Open4education

Bad practice at for loops

Globalcode – Open4education

Good allocation example

Globalcode – Open4education

WHEN CONCATENATING STRINGS

Avoid Creating Unecessary Objects

Globalcode – Open4education

Bad Practice

Globalcode – Open4education

Bad Practice

● Strings are immutable● Complexity square n

Globalcode – Open4education

Good Practice

● Linear complexity● 85 times faster than the first one for 8.000 strings.

Globalcode – Open4education

QUESTIONS?

This is the END