Sanity on Rails

Sanity on Rails

Talk given at Ruby X Elixir Conf Taiwan 2018. An updated version of the talk from Deccan RubyConf 2017, including new material on Phoenix and Hanami.

B3881a28fe402dd2d1de44717486cae8?s=128

Michael Kohl

April 28, 2018
Tweet

Transcript

  1. Sanity on Rails Michael Kohl Ruby X Elixir Conf Taiwan

    2018
  2. • Michael Kohl (@citizen428) • CTO @ Lockstep Labs •

    Based in Bangkok, Thailand • Ruby developer since 200[3|4] • Really into FP (Clojure, Elixir etc) • BUT: I still ❤ Ruby Ruby X Elixir Conf Taiwan 2018 locksteplabs.com whoami
  3. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Outline • Recurring

    problems • Communication • Dependencies • Lack of architecture • Easy code, not simple • Good news: easily avoidable
  4. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com BUT…

  5. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com The other problem

    Sometimes we forget how good we have it!
  6. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Things to remember

    #1 It’s Ruby! On Rails.
  7. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Things to remember

    #2 It’s Ruby. On Rails!
  8. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Things to remember

    #3 We are Ruby on Rails!
  9. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Agency requirements •

    Clients pay for results, not tech • Handoff project • Easy to onboard new developers • Usable across projects • Fits team & work flows
  10. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Communication “You didn’t

    specify the resolution” Leonardo Da Vinci, Oil on poplar panel, 1503
  11. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Communication “First, solve

    the problem. Then, write the code.” – John Johnson “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler
  12. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Dependencies “Updating your

    bundle” Roy Lichtenstein, Lithograph on paper, 1963
  13. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Dependencies + Help

    bootstrap the app faster + Avoid reinventing the wheel + Get peer reviewed - Easily get outdated - Memory footprint & startup times - Have more dependencies
  14. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Dependencies • Be

    selective about dependencies • Do reinvent certain wheels • Look for actively maintained gems • Prefer developers with vested interest • Use gems for common tasks
  15. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Reinventing the wheel

    Original wheel Potentially better wheel
  16. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Dependencies

  17. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Lack of architecture

    “Programmers at work maintaining a Ruby on Rails application" Eero Järnefelt, Oil on canvas, 1893
  18. "A basic conceptional structure (as of ideas)" – Merriam Webster

    "A basic structure underlying a system, concept, or text." – Oxford English dictionary Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks
  19. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks

  20. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Models

    • Wrapper around datastore • ActiveRecord configuration • Associations • Validations • Finders / scopes • Simple accessors / mutation methods
  21. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Models

    • Extract POROs • Use namespacing • Various extraction patterns • Service objects • Form objects • Query objects • many more
  22. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Models

    ! tree app/models/cv app/models/cv "## file_mover.rb "## importer.rb "## parser.rb "## repository.rb "## suggestion_handler.rb $## viewer.rb
  23. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Models

  24. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Models

  25. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Models

  26. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Controllers

    • Abstracting HTTP communication • Authentication • Authorization • Sessions / cookies • Param validation / transformation • Redirection / rendering
  27. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Controllers

  28. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Controllers

    - Hanami
  29. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Views

    • Response generation • Should be mostly logic free • Rails “views” are templates • Helpers become dumping grounds
  30. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Views

  31. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Views

  32. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Views

  33. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Implicit code “Reading

    your own code after 3 months” Edvard Munch, Oil, tempera, pastel and crayon on cardboard, 1893
  34. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Implicit code •

    “Convention over configuration” • “Magic”: • AR configuration • Instance variables in views • Implicit code: • AR callbacks • before_action/after_action
  35. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Implicit code •

    Hard for newcomers to understand • Hard to follow code flow • Easy, not simple • Doesn’t apply in all cases
  36. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Simple vs. easy

  37. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Implicit code •

    Be more explicit! • Reading is more important than writing! • POROs to encapsulate processes • Don’t try to be too clever!
  38. • Communicate! • Manage your dependencies! • Stay change positive!

    • Optimize for reading! • Make your own rules! • Have fun! Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Summary
  39. • How DHH Organizes His Rails Controllers
 http://jeromedalbert.com/how-dhh-organizes-his-rails- controllers/ •

    7 Patterns to Refactor Fat ActiveRecord Models
 http://blog.codeclimate.com/blog/2012/10/17/7-ways- to-decompose-fat-activerecord-models/ • Objects on Rails
 http://objectsonrails.com • Exploding Rails
 https://speakerdeck.com/radar/exploding-rails Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Resources
  40. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Thank you!