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

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.

Michael Kohl

April 28, 2018
Tweet

More Decks by Michael Kohl

Other Decks in Programming

Transcript

  1. • 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
  2. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Outline • Recurring

    problems • Communication • Dependencies • Lack of architecture • Easy code, not simple • Good news: easily avoidable
  3. 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
  4. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Communication “You didn’t

    specify the resolution” Leonardo Da Vinci, Oil on poplar panel, 1503
  5. 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
  6. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Dependencies “Updating your

    bundle” Roy Lichtenstein, Lithograph on paper, 1963
  7. 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
  8. 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
  9. 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
  10. "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
  11. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Models

    • Wrapper around datastore • ActiveRecord configuration • Associations • Validations • Finders / scopes • Simple accessors / mutation methods
  12. 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
  13. 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
  14. Ruby X Elixir Conf Taiwan 2018 locksteplabs.com Frameworks - Controllers

    • Abstracting HTTP communication • Authentication • Authorization • Sessions / cookies • Param validation / transformation • Redirection / rendering
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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!
  20. • 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
  21. • 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