Successfully reported this slideshow.
Your SlideShare is downloading. ×

Refactoring the third commandment

Ad

Nicola Pietroluongo @niklongstone
REFACTORING
THE THIRD
COMMANDMENT

Ad

Nicola Pietroluongo @niklongstone
TDD cycle

Ad

Nicola Pietroluongo @niklongstone
Nicola Pietroluongo
@niklongstone
▪ Lead developer/Solution architect
▪ Tech article wri...

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Upcoming SlideShare
Refactoring 重构
Refactoring 重构
Loading in …3
×

Check these out next

1 of 51 Ad
1 of 51 Ad

Refactoring the third commandment

Download to read offline

If it works doesn’t imply it’s good. Improving the internal structure of a project is important for many reasons. Refactor also means simplify the debug processes, tuning up performances and make the code readable to avoid a gradual spaghetti project failure. This talk shows some important bad code smells, how to assign priorities, tools useful in refactoring and code quality.

If it works doesn’t imply it’s good. Improving the internal structure of a project is important for many reasons. Refactor also means simplify the debug processes, tuning up performances and make the code readable to avoid a gradual spaghetti project failure. This talk shows some important bad code smells, how to assign priorities, tools useful in refactoring and code quality.

Advertisement
Advertisement

Refactoring the third commandment

  1. 1. Nicola Pietroluongo @niklongstone REFACTORING THE THIRD COMMANDMENT
  2. 2. Nicola Pietroluongo @niklongstone TDD cycle
  3. 3. Nicola Pietroluongo @niklongstone Nicola Pietroluongo @niklongstone ▪ Lead developer/Solution architect ▪ Tech article writer ▪ Open Source contributor ▪ Author of “Learning PHP7” by Packt Publishing ▪ +10y of PHP programming ▪ Amazon Alexa skill challenge award winner
  4. 4. Nicola Pietroluongo @niklongstone Refactoring the 3rd commandment Sections ▪ Introduction ▪ Code smells ▪ Summary and tools
  5. 5. Nicola Pietroluongo @niklongstone “It’s a process of restructuring an existing code, altering its internal structure without changing its external behavior.
  6. 6. Nicola Pietroluongo @niklongstone Why should you refactor? Benefits Your code: ▪ will look clean ▪ easy to understand ▪ easy to edit/maintain You: ▪ will code faster ▪ will spot bug easier
  7. 7. Nicola Pietroluongo @niklongstone Happy days
  8. 8. Nicola Pietroluongo @niklongstone When should you refactor? Rule of three two ▪ Everytime you add a feature ▪ Everytime you fix a bug ▪ During the review process
  9. 9. Nicola Pietroluongo @niklongstone When Shouldn't You Refactor? It depends ▪ Spaghetti code ▪ Extremely coupled ▪ Code too buggy ▪ Close to deadline
  10. 10. Nicola Pietroluongo @niklongstone What do I tell to the business?
  11. 11. Nicola Pietroluongo @niklongstone Happy days
  12. 12. Nicola Pietroluongo @niklongstone What to do before? Starting steps ▪ Announce it to your teammate ▪ Use a VCS ▪ Remember coding standard ▪ Remember OO and SOLID principles ▪ Write or improve the test
  13. 13. Nicola Pietroluongo @niklongstone Code smells Warning signs in your code
  14. 14. Nicola Pietroluongo @niklongstone Code smells If it stinks change it ▪ Bloaters ▪ Object-Orientation Abusers ▪ Change Preventers ▪ Unneeded ▪ Couplers
  15. 15. Nicola Pietroluongo @niklongstone Bloaters
  16. 16. Nicola Pietroluongo @niklongstone Method / Class Size ▪ Long method ▪ Large class ▪ Long parameter list
  17. 17. Nicola Pietroluongo @niklongstone Primitive Obsession Use object instead class Authentication { const ROLE_GUEST = 0; const ROLE_MEMBER = 1; const ROLE_ADMIN = 2; const USER_ROLE = ‘role’; public function getRole($user) { $role = $user[self::USER_ROLE]; // other operations }
  18. 18. Nicola Pietroluongo @niklongstone Data Clumps Use object instead public function createPermission($user, $pass, $read, $write) { // other operations } public function updatePermission($user, $pass, $read, $write) { // other operations }
  19. 19. Nicola Pietroluongo @niklongstone Object-Orientation Abusers
  20. 20. Nicola Pietroluongo @niklongstone Switch Statement Use object instead switch ($city) { case 'Brussels': return getWinterClothes(); case 'Miami': return getSummerClothes(); case 'Lisbon': return getNormalClothes(); }
  21. 21. Nicola Pietroluongo @niklongstone Switch Statement Use object instead interface City { public function getClothes(); } class Brussels implements City { function getClothes() { return 'normalClothes'; } } class Miami implements City{ //code...} $cityClothes = new $city(); // i.e. $city = ‘Brussels’; $cityClothes->getClothes();
  22. 22. Nicola Pietroluongo @niklongstone Refuse Badquest A subclass uses only some methods interface Animal { public function fly(); public function swim(); public function eat(); } class Fish implements Animal { public function swim() { // … } public function fly() { //trow Exception } // …
  23. 23. Nicola Pietroluongo @niklongstone I exists...
  24. 24. Nicola Pietroluongo @niklongstone Refuse Badquest A subclass uses only some methods interface GenericAnimal { public function eat() } interface SeaAnimal { public function swim() } class Fish implements GenericAnimal, SeaAnimal
  25. 25. Nicola Pietroluongo @niklongstone Change Preventers
  26. 26. Nicola Pietroluongo @niklongstone Change Preventers When you make many changes in other places 10101101010 10101101010 10101101010
  27. 27. Nicola Pietroluongo @niklongstone Change Preventers How to avoid ▪ Keep the class hierarchy small ▪ Avoid classes with same behaviour ▪ Enforce the Single responsibility principle
  28. 28. Nicola Pietroluongo @niklongstone Unneeded
  29. 29. Nicola Pietroluongo @niklongstone Unneeded You can live without that ▪ Comments ▪ Dead code ▪ Future code not in use
  30. 30. Nicola Pietroluongo @niklongstone Couplers
  31. 31. Nicola Pietroluongo @niklongstone Couplers You spend too much time together... ▪ Message Chain ▪ Feature Envy ▪ Inappropriate Intimacy ▪ Middle Man ▪ Traits freak
  32. 32. Nicola Pietroluongo @niklongstone Message chain You can live without that CLIENT EMPLOYEE getOffice() returns class Office OFFICE getManager() $manager = $employee->getOffice()->getManager();
  33. 33. Nicola Pietroluongo @niklongstone Message chain You can live without that CLIENT EMPLOYEE getManager() OFFICE $manager = $employee->getManager();
  34. 34. Nicola Pietroluongo @niklongstone Traits freak Be careful
  35. 35. Nicola Pietroluongo @niklongstone I am a trait
  36. 36. Nicola Pietroluongo @niklongstone Traits freak Bad
  37. 37. Nicola Pietroluongo @niklongstone Traits freak ...
  38. 38. Nicola Pietroluongo @niklongstone More examples PHP Code smells https://github.com/niklongstone/php-refactoring
  39. 39. Nicola Pietroluongo @niklongstone Summary How to prevent code smells in PHP
  40. 40. Nicola Pietroluongo @niklongstone How to improve Part of your development process ▪ Short methods and classes ▪ Avoid duplications (DRY) ▪ SOLID principle ▪ Composition over inheritance ▪ Objects over simple structures ▪ declare(strict_types=1); ▪ Share your findings
  41. 41. Nicola Pietroluongo @niklongstone Tools Tools and automation
  42. 42. Nicola Pietroluongo @niklongstone PHP CSF
  43. 43. Nicola Pietroluongo @niklongstone Exakat
  44. 44. Nicola Pietroluongo @niklongstone PhpMetrics
  45. 45. Nicola Pietroluongo @niklongstone PHP MD
  46. 46. Nicola Pietroluongo @niklongstone PHPMD $ ./phpmd.phar filesOrDir reportFormat rules $ ./phpmd.phar fileA.php,fileB.php text cleancode,codesize
  47. 47. Nicola Pietroluongo @niklongstone Git diff $ git diff --name-only --diff-filter=d master src/CinemaBundle/Controller/CinemaController.php src/CinemaBundle/Entity/Screening.php src/CinemaBundle/Entity/ScreeningVenue.php src/CinemaBundle/Entity/Showing.php
  48. 48. Nicola Pietroluongo @niklongstone Sed $ sed ':a; $!N; s/n/,/; ta' :a creates a pattern $!N appends lines to the pattern if not last line s/n/,/ replaces new line with comma ta repeat the a
  49. 49. Nicola Pietroluongo @niklongstone Pull it together *All in one line ./phpmd.phar $(git diff --name-only --diff-filter=d master | sed ':a; $!N; s/n/,/; ta') text cleancode,codesize
  50. 50. Nicola Pietroluongo @niklongstone “To improve yourself, Improve your code.
  51. 51. Nicola Pietroluongo @niklongstone Thanks! ! NicolaPietroluongo.com @niklongstone https://github.com/niklongstone https://joind.in/talk/a7aa2

×