Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The scary fairy tale about MVC or How to stop worrying and start to write ruby code

The scary fairy tale about MVC or How to stop worrying and start to write ruby code

Hotcode conference, 5 May 2013

Andrey Savchenko

May 31, 2013
Tweet

More Decks by Andrey Savchenko

Other Decks in Programming

Transcript

  1. The scary fairy tale
    about MVC
    How to stop worrying and start to write ruby code
    or

    View full-size slide

  2. Andrey Savchenko
    @ptico
    @ptico_eng Aejis

    View full-size slide

  3. Cast
    • Little Red Riding Hood - developer
    • Grandmother - customers
    • The map - MVC
    • Compass - Rails
    And others…

    View full-size slide

  4. nce upon a time, a little girl
    goes trough the scary wood to
    deliver pies to her grandmother.
    O

    View full-size slide

  5. here she met the
    Damn Horrible Human
    nicknamed «The Wolf»
    T

    View full-size slide

  6. here you are going, little girl?
    he asked. I’m going to deliver pies
    to my grandma – she answered.
    W

    View full-size slide

  7. have a present for you – wolf
    said. Take this map and this
    compass: it works very well for me.
    I

    View full-size slide

  8. he girl took gifts and moved
    on. And have lost in the wood.
    Because the compass was point to
    the wolf’s grandmother and map
    defines the ways not applicable for
    little girls.
    T

    View full-size slide

  9. Moral: the tools which
    works for other person
    may doesn’t work for
    you

    View full-size slide

  10. MVC is a good example

    View full-size slide

  11. What’s wrong with
    MVC?

    View full-size slide

  12. It has only three letters
    MVC

    View full-size slide

  13. One of them
    superfluous
    MVC

    View full-size slide

  14. Application should know nothing
    about representation

    View full-size slide

  15. Another one means
    a little bit more than
    nothing
    MVC

    View full-size slide

  16. The controller should delegate the work that
    needs to be done to other objects; it coordinates or
    controls the activity. It should not do much work
    itself.
    GRASP controller definition

    View full-size slide

  17. So, what’s left?

    View full-size slide

  18. Please, rise your hand if you have this kind of models

    View full-size slide

  19. So, this is a moment when we should start writing
    the Ruby code

    View full-size slide

  20. What does it mean?

    View full-size slide

  21. Lets take a look at our daily gems

    View full-size slide

  22. state_machine

    View full-size slide

  23. Ok, i catch the idea, lets start!

    View full-size slide

  24. Wait a minute - we need some weapons

    View full-size slide

  25. Metrics
    • WPM - WTFs per minute

    View full-size slide

  26. Metrics
    • WPM - WTFs per minute
    • SLOC/M - Source lines of code per
    method

    View full-size slide

  27. Metrics
    • WPM - WTFs per minute
    • SLOC/M - Source lines of code per
    method
    • SLOC/C - Source lines of code per class

    View full-size slide

  28. Overall complexity
    ((AVG(SLOC/M) + SLOC/C)^WPM)/100

    View full-size slide

  29. Overall complexity
    If you have > 10 – i have a bad news for you

    View full-size slide

  30. Principles
    • SOLID

    View full-size slide

  31. Principles
    • SOLID
    • YAGNI

    View full-size slide

  32. Principles
    • SOLID
    • YAGNI
    • KISS

    View full-size slide

  33. Secret weapon: Testability

    View full-size slide

  34. The simpler your objects to test - the easier
    they can be changed and reused

    View full-size slide

  35. The more complex your objects is - the harder
    to test them

    View full-size slide

  36. Got it! What about strategy?

    View full-size slide

  37. Use-case strategy

    View full-size slide

  38. Use-case strategy
    • Doesn’t work with Rails
    • Requires to build your own stack
    • But once you build this – its bullet-proof

    View full-size slide

  39. Onion strategy

    View full-size slide

  40. Onion strategy
    • Do work with Rails
    • Layer can be just Plain Old Ruby Object
    • Layers is hard to naming

    View full-size slide

  41. Onion strategy
    Model Controller
    View

    View full-size slide

  42. Onion strategy
    Model Controller
    View
    Presenter

    View full-size slide

  43. Onion strategy
    Model Controller
    View
    Presenter
    Form object

    View full-size slide

  44. Onion strategy
    Model Controller
    View
    Presenter
    Form object
    Repository

    View full-size slide

  45. Onion strategy
    Model Controller
    View
    Presenter
    Form object
    Repository
    Service

    View full-size slide

  46. Onion strategy
    Model Controller
    View
    Presenter
    Form object
    Repository
    Service
    Validator

    View full-size slide

  47. Attribution:
    http://www.flickr.com/photos/42255449@N00/7177978063/
    http://www.flickr.com/photos/normanbleventhalmapcenter/2674605704/
    Thank you!

    View full-size slide

  48. Questions?
    Andrey Savchenko
    Aejis*
    * we are hiring**
    ** if you understand the problem described in talk

    View full-size slide