O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

effective java

406 visualizações

Publicada em

Geralmente aplicativos buscam resolver uma única tarefa. Mas conforme o passar do tempo, a aplicação tende a ganhar novas features e otimizações, consequentemente ela tende a crescer. Mais features e mais usuários significam mais código. E quanto mais código você escreve, mais código você precisa manter e reescrever. Nos livros "Effective Java" e "Android programming - pushing limits", Joshua Bloch e Erik Hellman, respectivamente, demonstram boas práticas para o desenvolvimento em Java e na plataforma Android. A idéia é fazer um apanhado das melhores práticas trazidas por estes livros. Detalharei o que deve o que não deve ser feito ao aplicar tais práticas.

Publicada em: Educação
  • Seja o primeiro a comentar

effective java

  1. 1. Globalcode – Open4education Trilha – Android Bruno Fernandes Campos Desenvolvedor Mobile EFFECTIVE JAVA
  2. 2. Globalcode – Open4education About
  3. 3. Globalcode – Open4education Why?
  4. 4. Globalcode – Open4education How?
  5. 5. 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
  6. 6. Globalcode – Open4education Comparing For Loops
  7. 7. Globalcode – Open4education For Loops In Arrays Manual for-loop x Enhanced for-loop
  8. 8. Globalcode – Open4education For Loops In Arrays Manual for-loop x Enhanced for-loop SAME PERFORMANCE WHEN READING ENTRANCES
  9. 9. Globalcode – Open4education For loops in Collections For each X Iterator
  10. 10. Globalcode – Open4education For loops in Collections For each X Iterator SAME PERFORMANCE
  11. 11. Globalcode – Open4education For loops in ArrayLists Manual for-loop X For each
  12. 12. Globalcode – Open4education For loops in ArrayLists X For eachManual for-loop
  13. 13. Globalcode – Open4education For loops in ArrayLists X 1061 ms 1183 ms For each 100000 strings logged in the console performance test Manual for-loop
  14. 14. Globalcode – Open4education Many Constructors Parameters What should I do?
  15. 15. Globalcode – Open4education Nutrition Facts Class
  16. 16. Globalcode – Open4education Telescoping Pattern
  17. 17. Globalcode – Open4education Using Telescoping Constructor Pattern ● Force some unwanted values ● It does not scale well! ● Parameters reversion
  18. 18. Globalcode – Open4education Javabeans Pattern
  19. 19. 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"
  20. 20. Globalcode – Open4education The builder pattern!
  21. 21. Globalcode – Open4education Using Builder Pattern ● Easy to read and Write ● Safe -> Imutable
  22. 22. Globalcode – Open4education How to turn static classes safe
  23. 23. Globalcode – Open4education Static class
  24. 24. Globalcode – Open4education AndroidAPIUtils Usage
  25. 25. Globalcode – Open4education Wrong usage
  26. 26. Globalcode – Open4education AndroidAPIUtils avoiding instantiability
  27. 27. Globalcode – Open4education Avoid Creating Unecessary Objects
  28. 28. Globalcode – Open4education Prefeer Static Factory Methods Constructors will always create a new instance!
  29. 29. Globalcode – Open4education INSIDE CLASSES Avoid Creating Unecessary Objects
  30. 30. Globalcode – Open4education Bad example
  31. 31. Globalcode – Open4education Use Static initializers
  32. 32. 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!
  33. 33. Globalcode – Open4education INSIDE FOR LOOPS Avoid Creating Unecessary Objects
  34. 34. Globalcode – Open4education Bad practice at for loops
  35. 35. Globalcode – Open4education Good allocation example
  36. 36. Globalcode – Open4education WHEN CONCATENATING STRINGS Avoid Creating Unecessary Objects
  37. 37. Globalcode – Open4education Bad Practice
  38. 38. Globalcode – Open4education Bad Practice ● Strings are immutable ● Complexity square n
  39. 39. Globalcode – Open4education Good Practice ● Linear complexity ● 85 times faster than the first one for 8.000 strings.
  40. 40. Globalcode – Open4education QUESTIONS? This is the END

×