Embracing change

Embracing change

PHP Sount West, 17th June 2015

1a4e1f98f3aeef310273366c8c785207?s=128

Jakub Zalas

June 17, 2015
Tweet

Transcript

  1. EMBRACING CHANGE JAKUB ZALAS 17th June 2015 @jakub_zalas @jakzal

  2. None
  3. None
  4. None
  5. DEFER COMMITMENT Decisions are hard to change if software components

    are tightly coupled.
  6. DEFER COMMITMENT We need loosely coupled and highly cohesive components

    to introduce changes easily.
  7. COUPLING & COHESION

  8. COHESION Cohesion describes how closely are elements in a module

    related
  9. c l a s s P a c k a

    g e C r a w l e r { p u b l i c f u n c t i o n c r a w l ( ) { $ c h = c u r l _ i n i t ( ) ; c u r l _ s e t o p t ( $ c h , C U R L O P T _ U R L , ' h t t p : / / a c m e . c o m / ' ) ; c u r l _ s e t o p t ( $ c h , C U R L O P T _ R E T U R N T R A N S F E R , 1 ) ; $ r e s p o n s e = c u r l _ e x e c ( $ c h ) ; c u r l _ c l o s e ( $ c h ) ; / / @ t o d o e x t r a c t d e t a i l s f r o m t h e r e s p o n s e . . . } }
  10. SINGLE RESPONSIBILITY PRINCIPLE A module should have one reason to

    change.
  11. SINGLE RESPONSIBILITY PRINCIPLE Gather together the things that change for

    the same reasons. Separate those things that change for different reasons. http://blog.8thlight.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html
  12. None
  13. COUPLING Modules are coupled if changing one of them requires

    changing another one. Martin Fowler http://martinfowler.com/ieeeSoftware/coupling.pdf
  14. COUPLING

  15. COUPLING THROUGH A PROPERTY u s e B u z

    z \ B r o w s e r ; c l a s s C r a w l e r { / * * * @ v a r B r o w s e r * / p r i v a t e $ b r o w s e r ; }
  16. COUPLING THROUGH A METHOD CALL c l a s s

    C r a w l e r { p r i v a t e $ c ; p u b l i c f u n c t i o n c r a w l ( $ u r l ) { $ t h i s - > c - > g e t B r o w s e r ( ) - > g e t ( $ u r l ) ; } }
  17. COUPLING THROUGH A REFERENCE u s e B u z

    z \ B r o w s e r ; c l a s s C r a w l e r { p u b l i c f u n c t i o n c r a w l ( $ u r l , B r o w s e r $ b ) { $ r e s p o n s e = $ b - > g e t ( $ u r l ) ; } }
  18. COUPLING THROUGH A REFERENCE u s e B u z

    z \ B r o w s e r ; c l a s s C r a w l e r { / * * * @ r e t u r n B r o w s e r * / p u b l i c f u n c t i o n c r a w l ( $ u r l ) { / / . . . r e t u r n $ b r o w s e r ; } }
  19. COUPLING THROUGH AN IMPLEMENTATION / EXTENSION u s e B

    u z z \ B r o w s e r ; c l a s s C r a w l e r e x t e n d s B r o w s e r { p u b l i c f u n c t i o n c r a w l ( $ u r l ) { $ t h i s - > g e t ( $ u r l ) ; } }
  20. INCREASING COHESION u s e B u z z \

    B r o w s e r ; c l a s s P a c k a g e C r a w l e r { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n c r a w l ( ) { $ r e s p o n s e = $ t h i s - > b r o w s e r - > g e t ( ' / ' ) ; / / . . . } }
  21. None
  22. None
  23. None
  24. Count dependencies c l a s s P r o

    d u c t C o n t r o l l e r e x t e n d s C o n t r o l l e r { p u b l i c f u n c t i o n s e a r c h A c t i o n ( $ k e y w o r d s ) { $ p r o d u c t s = $ t h i s - > g e t ( ' d o c t r i n e ' ) - > g e t R e p o s i t o r y ( ' A c m e : P r o d u c t ' ) ; - > s e a r c h ( $ k e y w o r d s ) ; r e t u r n $ t h i s - > r e n d e r ( ' p r o d u c t / s e a r c h . h t m l . t w i g ' , [ ' p r o d u c t s ' = > $ p r o d u c t s ] ) ; } }
  25. None
  26. THE DEPENDENCY INVERSION PRINCIPLE

  27. HIGH-LEVEL MODULES SHOULD NOT DEPEND ON LOW-LEVEL MODULES. u s

    e B u z z \ B r o w s e r ; c l a s s P a c k a g e C r a w l e r { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n c r a w l ( $ r e s o u r c e ) { $ r e s p o n s e = $ t h i s - > b r o w s e r - > g e t ( $ r e s o u r c e ) ; / / . . . } }
  28. HIGH-LEVEL MODULES SHOULD NOT DEPEND ON LOW-LEVEL MODULES.

  29. BOTH SHOULD DEPEND ON ABSTRACTIONS.

  30. BOTH SHOULD DEPEND ON ABSTRACTIONS. i n t e r

    f a c e H t t p C l i e n t { p u b l i c f u n c t i o n g e t ( $ r e s o u r c e ) ; }
  31. BOTH SHOULD DEPEND ON ABSTRACTIONS. c l a s s

    P a c k a g e C r a w l e r { p r i v a t e $ h t t p C l i e n t ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( H t t p C l i e n t $ h t t p C l i e n t ) { $ t h i s - > h t t p C l i e n t = $ h t t p C l i e n t ; } p u b l i c f u n c t i o n c r a w l ( ) { $ r e s p o n s e = $ t h i s - > h t t p C l i e n t - > g e t ( ' / ' ) ; / / @ t o d o p a r s e } }
  32. BOTH SHOULD DEPEND ON ABSTRACTIONS.

  33. BOTH SHOULD DEPEND ON ABSTRACTIONS. u s e B u

    z z \ B r o w s e r ; c l a s s B u z z H t t p C l i e n t i m p l e m e n t s H t t p C l i e n t { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n g e t ( $ r e s o u r c e ) { r e t u r n ( s t r i n g ) $ t h i s - > b r o w s e r - > g e t ( $ r e s o u r c e ) ; } }
  34. ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS.

  35. DETAILS SHOULD DEPEND UPON ABSTRACTIONS.

  36. i n t e r f a c e C

    o n t e n t P r o v i d e r { p u b l i c f u n c t i o n f e t c h ( $ r e s o u r c e ) ; }
  37. c l a s s P a c k a

    g e C r a w l e r { p r i v a t e $ c o n t e n t P r o v i d e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( C o n t e n t P r o v i d e r $ p r o v i d e r ) { $ t h i s - > c o n t e n t P r o v i d e r = $ p r o v i d e r ; } p u b l i c f u n c t i o n c r a w l ( ) { $ c o n t e n t = $ t h i s - > c o n t e n t P r o v i d e r - > f e t c h ( ' / ' ) ; / / @ t o d o p a r s e } }
  38. u s e B u z z \ B r

    o w s e r ; c l a s s B u z z C o n t e n t P r o v i d e r i m p l e m e n t s C o n t e n t P r o v i d e r { p r i v a t e $ b r o w s e r ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B r o w s e r $ b r o w s e r ) { $ t h i s - > b r o w s e r = $ b r o w s e r ; } p u b l i c f u n c t i o n f e t c h ( ) { r e t u r n ( s t r i n g ) $ t h i s - > b r o w s e r - > g e t ( ' / ' ) ; } }
  39. None
  40. None
  41. u s e G u z z l e \

    C l i e n t ; c l a s s G u z z l e C o n t e n t P r o v i d e r i m p l e m e n t s C o n t e n t P r o v i d e r { p r i v a t e $ g u z z l e ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( C l i e n t $ g u z z l e ) { $ t h i s - > g u z z l e = $ g u z z l e ; } p u b l i c f u n c t i o n f e t c h ( $ r e s o u r c e ) { $ r e q u e s t = $ t h i s - > g u z z l e - > c r e a t e R e q u e s t ( ' G E T ' , $ r e s o u r c e ) ; $ r e s p o n s e = $ r e q u e s t - > s e n d ( ) ; r e t u r n $ r e s p o n s e - > g e t B o d y ( ) ; } }
  42. None
  43. What about the framework? c l a s s P

    r o d u c t C o n t r o l l e r e x t e n d s C o n t r o l l e r { p u b l i c f u n c t i o n s e a r c h A c t i o n ( $ k e y w o r d s ) { $ p r o d u c t s = $ t h i s - > g e t ( ' d o c t r i n e ' ) - > g e t R e p o s i t o r y ( ' A c m e : P r o d u c t ' ) ; - > s e a r c h ( $ k e y w o r d s ) ; r e t u r n $ t h i s - > r e n d e r ( ' p r o d u c t / s e a r c h . h t m l . t w i g ' , [ ' p r o d u c t s ' = > $ p r o d u c t s ] ) ; } }
  44. None
  45. i n t e r f a c e P

    r o d u c t R e p o s i t o r y { / * * * @ r e t u r n P r o d u c t [ ] * / p u b l i c f u n c t i o n s e a r c h ( $ k e y w o r d s ) ; }
  46. c l a s s D o c t r

    i n e P r o d u c t R e p o s i t o r y i m p l e m e n t s P r o d u c t R e p o s i t o r y { p r i v a t e $ e m ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( E n t i t y M a n a g e r $ e m ) { $ t h i s - > e m = $ e m ; } / * * * @ r e t u r n P r o d u c t [ ] * / p u b l i c f u n c t i o n s e a r c h ( $ k e y w o r d s ) { r e t u r n $ t h i s - > e m - > c r e a t e Q u e r y B u i l d e r ( ) - > s e l e c t ( ' p ' ) - > f r o m ( P r o d u c t : : c l a s s , ' p ' ) - > w h e r e ( ' p . t i t l e L I K E : k e y w o r d s ' ) - > s e t P a r a m e t e r ( ' k e y w o r d s ' , ' % ' . $ k e y w o r d s . ' % ' ) - > g e t Q u e r y ( ) - > g e t R e s u l t ( ) ; } }
  47. c l a s s P r o d u

    c t C o n t r o l l e r { p r i v a t e $ p r o d u c t R e p o s i t o r y ; p r i v a t e $ t e m p l a t i n g ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( P r o d u c t R e p o s i t o r y $ p r o d u c t R e p o s i t o r y , E n g i n e I n t e r f a c e $ t e m p l a t i n g ) { $ t h i s - > p r o d u c t R e p o s i t o r y = $ p r o d u c t R e p o s i t o r y ; $ t h i s - > t e m p l a t i n g = $ t e m p l a t i n g ; } / / . . . }
  48. c l a s s P r o d u

    c t C o n t r o l l e r { / / . . . p u b l i c f u n c t i o n s e a r c h A c t i o n ( $ k e y w o r d s ) { $ p r o d u c t s = $ t h i s - > p r o d u c t R e p o s i t o r y - > s e a r c h ( $ k e y w o r d s ) ; r e t u r n $ t h i s - > t e m p l a t i n g - > r e n d e r R e s p o n s e ( ' p r o d u c t / s e a r c h . h t m l . t w i g ' , [ ' p r o d u c t s ' = > $ p r o d u c t s ] ) ; } }
  49. None
  50. None
  51. None
  52. None
  53. Good code doesn't take more time to write. What takes

    a lot of time is the learning how to write good code.
  54. THANK YOU! @jakub_zalas @jakzal