Dealing With Duplicated Code

Dealing With Duplicated Code

Slides for my presentation at Bulgaria Web Summit 2015 about Code Duplication, what it is, problems, how to find and how to deal with it.

E003fec30e97c29a47c16d2d8b36ce93?s=128

Evaldo Junior

April 18, 2015
Tweet

Transcript

  1. DealingWith DuplicatedCode Duplicated Duplicated BulgariaWebSummit April 2014 Duplicated

  2. Evaldo Junior @InFog9 evaldojunior.com #brazil #germany #php #linux #opensource

  3. BambergGermany We'reHiring!

  4. DéjàVu i. jBBZ2i .kBB: ,BBBBBBUi iFBBBBB BBZMMBBBBBZMBBBB88EBP ,BGNE0GZMMBBMG8EZqZBr ;B00NEqE0ZNGNEN0q0N8MZ7. BMZNEqENENEqEqZNZNEEOBBBBME0ZMMEGF5U15J7L7L7vLS25Uq0ZMBMBMBMBBBBBBBBBBBBBMBZPuLi,

    iB80ZNEN0qEqENEN0qZNENZE8OBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBMBBBMMMMOMOM8M8MMBMBBBBBBBBBEU;. iBZZqENENEqEqZNZN0NEqENENE0ZNZ0G0GEG0Z0GEZEGEZ0ZEG0G0ZNZ0Z0Z0Z0ZNE0EqENENZ0ZNZEZEOGMMBBBBBGU, ;BONEq0N0NZNENENENENEqEN0qZNENEqEqZNEqENENZNEqEqENZqENEqZNEq0NEqZq0NENEq0NENZNENEqZ0Z0GZOOBBBBG7. B8GNEqENENENENENENEqZqZN0qZNENENZNENENEN0NEN0NZNENZNZNZNZqENZqZqZNEqEN0qENENENZNEqZqEqENZ0ZZMBBBM; XBZEN0q0qZqZNZNENEqENEN0NENZNEqENZNENEqEqEqEqZNENENZqEN0NENEqZNENENENENENEqENZNEqZqENENEq0NZNZZMBBB2 BBGZN0NENGEO8G0ZNENEqENENZNEqZqENEq0NZNZqEN0NENEqENEqZNEqZqZN0qZqEqENENENEq0NENZNENZqENENENEN8BBBBBB1 BBM8Z8OBBBBBBM0EqEqEqENZN0NENENENENENEN0NENEqENENEqEqENEN0q0qENENZNEN0NENENEqENZNENEqEqZN0NOMv5BBBBBBL 2BBBBBMU: ;BOEEqEq0q0qEq0NEqENZqENENEqZNEq0qENENENZqENEN0qENZqZN0NENZNZNEq0NZqEN0NEq0qENGM8 .YBBBBO rvi YBO0ENEqENEN0N0N0NZqZNEqEqEqENENEqENZqENEq0qENENENEqENENZqENEqENZqZNEqENEN0N00BU 7BBBB: BBGNENEqZqEN0NENZqEqENZNENEN0NEN0qENEN0NENENENENEqZqENZN0q0NENENENZNEqEqEN0qGB7 XBBBi .BB8NEq0qZNEN0NZNZqEqZNENEqENEqENENEqENENEqENEqZNZqENENEqEN0NEqENENEqZNZN0N00Bi ;BBBY ;BBBEGNEEGNEN0NEN0N0NZNENENENEq0N0NEqENZq0NZNENENZqENENENZN0qENENEqZNZNEqENGBU .BBBj FBBBM88FGNENENZqENENENENENEq0NEqENENENEqENEN0NEqEN0NENENEqEqZqEqZNZNZqEqENBq BBBY rMBBrUGEN0NZNEqEN0NENEN0qENENEqEqENENENEN0qZNEq0NEN0NEq0N0qZNEqENENEN0NZOB BBBY :EYB8NZNENENZNZN0N0NZqENZ0ZNENENENZNEN0NENEqENENZNENEqENEqEN0NEN0NEN0q8B; BBBv BOEq0q0N0NENENENENZ0OOMZ8Z80ZEZ0Z0ENENENENENEqEN0NEqENENENEqZNEqENEEBM .BBBr qBNEqENEqENEqZNENGMBBBBBBBBBBBBBBBBBO8EZ0ZqENZNENEqEqENENEq0qENENENZZBr :BBBi MOZNENENZNE0ENENGBB: ,:7YS52LSqMBBBBBBOG0EqENENENENEqE0ENEN0NENENGMB vBBB: SB8NEq0qGZBMGNEqZMB .:7XBB8NZNEqENENENEN8O80ZqZN0NEqGBB 2BBB: ;BO0ZNENGMBq2BGN00B7 kBM0ZqEN0N0NEN8BMBBG80EN0N008BG XBBB ,BBEEqZEMBB7 BOE0OB 8BMEENENEqENGB7 :MBBBOO0ENE08BBj qBB BBEEqEEBBS NB0EOB FBBZZN0NENEEB .2MBBBBMZG08MBB qB NBZZNEEBBi 7BGNB0 ;BBGZ00NZN8B. :jMBBBBZG8B. S 7BGZNE0BB. .BZZB0 OBGZN0qEEBL :UBBMOBU .BOGNZEMB, BM0Bq 2B8ZNENZMB iBBMB BBG0ZZBBj BMGB8 vBZENE0MB: BBB1 OB8EOMBBi XBEBP YB0E08BB :BMB kBMGBBB7 FBMB: JBZ0OBB, BBBi SBM8BBr :BBBN YBMZBBX BMB2 i:rYBBMMBJ OBBBBBB, MBMMBB, BBBE BBBBBBBBBr 5BBBZj : ,5BBBBBY 7BBBBBL YSNM8BMB5 iBBBBBBBBBB1 1BBBB2 7BBBBBBBO;
  5. Wikipedia Duplicate code is a computer programming term for a

    sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity.
  6. CodeSmells #1

  7. DRY Don't Repeat Yourself

  8. WET We Enjoy Typing Write Everything Twice

  9. Duplication is not only “DuplicatedCode”

  10. Why do we duplicate?

  11. Why? Copy/Paste is easy ctrl+c / ctrl+v

  12. Why? In a hurry to deliver You know, it's for

    yesterday!
  13. Why? Lack of codebase knowledge - Hm, I didn't know

    our codebase had this already.
  14. Duplication's Problems

  15. Problems Increase codebase's size Five thousand lines' controllers.

  16. Problems Fix the same bug more than once - Ok,

    now it works in that page, but not in this one.
  17. Problems Adding not standard new features - Why are the

    dates not formated using my preference?
  18. Is duplication always a bad thing?

  19. Example of duplication

  20. None
  21. Hey! We need to support USA users!

  22. None
  23. Support user defined date format

  24. None
  25. One possible solution

  26. None
  27. Should I actively search for duplications?

  28. PHPCPD

  29. PHPCPD

  30. I found some duplications! What should I do now?

  31. Apply Some Patterns

  32. Different methods

  33. Different methods

  34. Extract Method

  35. Extract Method

  36. Different classes

  37. Extract Class

  38. Different projects

  39. Extract Package

  40. Same attribute in different classes

  41. Pull Up Field

  42. Thank you! благодаря MEGF5U15J7L7L7vLS25Uq0ZMBMBMBMBBBBBBBBBBBBBMBZPuLi, BBBBBBBBBBBBBBBBBBBBBBBBBBMBBBMMMMOMOM8M8MMBMBBBBBBBBBEU;. G0GEG0Z0GEZEGEZ0ZEG0G0ZNZ0Z0Z0Z0ZNE0EqENENZ0ZNZEZEOGMMBBBBBGU, qEqZNEqENENZNEqEqENZqENEqZNEq0NEqZq0NENEq0NENZNENEqZ0Z0GZOOBBBBG7. ENZNENENEN0NEN0NZNENZNZNZNZqENZqZqZNEqEN0qENENENZNEqZqEqENZ0ZZMBBBM; qENZNENEqEqEqEqZNENENZqEN0NENEqZNENENENENENEqENZNEqZqENENEq0NZNZZMBBB2 ZqENEq0NZNZqEN0NENEqENEqZNEqZqZN0qZqEqENENENEq0NENZNENZqENENENEN8BBBBBB1

    NENENENENEN0NENEqENENEqEqENEN0q0qENENZNEN0NENENEqENZNENEqEqZN0NOMv5BBBBBBL EqENZqENENEqZNEq0qENENENZqENEN0qENZqZN0NENZNZNEq0NZqEN0NEq0qENGM8 .YBBBBO N0NZqZNEqEqEqENENEqENZqENEq0qENENENEqENENZqENEqENZqZNEqENEN0N00BU 7BBBB: ENZqEqENZNENEN0NEN0qENEN0NENENENENEqZqENZN0q0NENENENZNEqEqEN0qGB7 XBBBi NZNZqEqZNENEqENEqENENEqENENEqENEqZNZqENENEqEN0NEqENENEqZNZN0N00Bi ;BBBY 0NEN0N0NZNENENENEq0N0NEqENZq0NZNENENZqENENENZN0qENENEqZNZNEqENGBU .BBBj NENZqENENENENENEq0NEqENENENEqENEN0NEqEN0NENENEqEqZqEqZNZNZqEqENBq BBBY 0NZNEqEN0NENEN0qENENEqEqENENENEN0qZNEq0NEN0NEq0N0qZNEqENENEN0NZOB BBBY NENENZNZN0N0NZqENZ0ZNENENENZNEN0NENEqENENZNENEqENEqEN0NEN0NEN0q8B; BBBv 0q0N0NENENENENZ0OOMZ8Z80ZEZ0Z0ENENENENENEqEN0NEqENENENEqZNEqENEEBM .BBBr qENEqENEqZNENGMBBBBBBBBBBBBBBBBBO8EZ0ZqENZNENEqEqENENEq0qENENENZZBr :BBBi ENENZNE0ENENGBB: ,:7YS52LSqMBBBBBBOG0EqENENENENEqE0ENEN0NENENGMB vBBB: q0qGZBMGNEqZMB .:7XBB8NZNEqENENENEN8O80ZqZN0NEqGBB 2BBB: ENGMBq2BGN00B7 kBM0ZqEN0N0NEN8BMBBG80EN0N008BG XBBBi EMBB7 BOE0OB 8BMEENENEqENGB7 :MBBBOO0ENE08BBj qBBB; BBS NB0EOB FBBZZN0NENEEB .2MBBBBMZG08MBB qBBBv Bi 7BGNB0 ;BBGZ00NZN8B. :jMBBBBZG8B. SBBBq . .BZZB0 OBGZN0qEEBL :UBBMOBU ;BBBB7 BM0Bq 2B8ZNENZMB iBBMB 2BBBB1i BMGB8 vBZENE0MB: BBB1 YBBBBBBq: XBEBP YB0E08BB :BMB :5BBBBB FBMB: JBZ0OBB, BBBi .ru7 :BBBN YBMZBBX BMB2 OBBBBBB, MBMMBB, BBBE @InFog9 evaldojunior.com speakerdeck.com/infog github.com/InFog i. jBBZ2i .kBB: ,BBBBBBUi iFBBBBB BBZMMBBBBBZMBBBB88EBP ,BGNE0GZMMBBMG8EZqZBr ;B00NEqE0ZNGNEN0q0N8MZ7. BMZNEqENENEqEqZNZNEEOBBBBME0ZMMEGF5U15J7L iB80ZNEN0qEqENEN0qZNENZE8OBBBBBBBBBBBBBBBB iBZZqENENEqEqZNZN0NEqENENE0ZNZ0G0GEG0Z0GEZ ;BONEq0N0NZNENENENENEqEN0qZNENEqEqZNEqENEN B8GNEqENENENENENENEqZqZN0qZNENENZNENENEN0 XBZEN0q0qZqZNZNENEqENEN0NENZNEqENZNENEqEq BBGZN0NENGEO8G0ZNENEqENENZNEqZqENEq0NZNZ BBM8Z8OBBBBBBM0EqEqEqENZN0NENENENENENEN 2BBBBBMU: ;BOEEqEq0q0qEq0NEqENZqENENE rvi YBO0ENEqENEN0N0N0NZqZNEqEq BBGNENEqZqEN0NENZqEqENZNE .BB8NEq0qZNEN0NZNZqEqZNEN ;BBBEGNEEGNEN0NEN0N0NZNE FBBBM88FGNENENZqENENEN rMBBrUGEN0NZNEqEN0NE :EYB8NZNENENZNZN0N BOEq0q0N0NENENE qBNEqENEqENEqZN MOZNENENZNE0ENE SB8NEq0qGZBMGNEq ;BO0ZNENGMBq2BGN0 ,BBEEqZEMBB7 BOE0 BBEEqEEBBS NB0E NBZZNEEBBi 7BGN 7BGZNE0BB. .BZZ .BOGNZEMB, BM0 BBG0ZZBBj BMG OB8EOMBBi XBE kBMGBBB7 FBM SBM8BBr :BBB i:rYBBMMBJ OBBBBBB