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

Architectural Antipatterns in Rails applications

Architectural Antipatterns in Rails applications

Slides for the talk presented at Chennai.rb's March 2014 meetup.



March 15, 2014

More Decks by codepodu

Other Decks in Programming


  1. Architectural Antipatterns in Rails apps

  2. Dheeraj Kumar @codepodu Chief Troll Officer Bang the Table !

  3. Name one similarity between

  4. It takes only two minutes to have a mass of

  5. checklist wish list 1. Less complexity 2. Unit-testable 3. Less

    coupling 4. SOLID 1. Single Responsibility Principle 2. Open/Closed Principle 3. Liskov Substitution Principle 4. Interface Segregation Principle 5. Dependency Inversion Principle
  6. Problems 1. Fat controllers (and mailers) 2. Fat models 3.

    Intelligent views 4. Unreliable tests 5. The Monorail

  8. #1 fat Controllers I have It belongs to Queries across

    models Query object Scopes Public method in model Form-handling code Form object Instance variables Methods, Exposure Class-level common behaviour Modules Complex method Service object Instance-level common behaviour Service object A long inheritance chain Nowhere. Replace with composition Complex strong parameter definitions Permitters Complex conditionals Policy objects View-handling code View objects
  9. #2 fat models I have It belongs to Queries across

    models Query object Scopes called everywhere! Make them private accepts_nested_attributes_for Form object AR methods called everywhere! Make them private Class-level common behaviour Modules or Inheritance Complex method Service object Instance-level common behaviour Modules, Inheritance or Service object Outgrown STI Use abstract base classes or Composition attr_accessible Strong Parameters or Permitters Complex conditionals Policy objects View-handling code View objects
  10. #2 fat models I have It belongs to AR &

    Domain logic mixed 1 AR model + one or more domain models Validations with context Form objects Slow callbacks Background job or Messaging Many callbacks Messaging Callbacks talking to other models Messaging Callbacks talking to same model Wherever it is. Good job! :) Observers See callbacks.
  11. #3 INTELLIGENT VIEWS I have It belongs to Complex conditionals

    Policy objects or View objects Hacks to get forms to work Form objects Many instance variables Partials, Methods, View objects, Templates Many helpers Inherited/Composed View objects Repeated code View objects or partials Ruby code being abused Nowhere. Use stricter templates Many JS variables set/inlined Nowhere. Render JSON Hacks to render HTML from AJAX Nowhere. Render JSON & use a good JS framework Ruby code in JS Nowhere. Render JSON & use a good JS framework
  12. #4 Unreliable Tests I have What should I do? Low

    code coverage Write more tests App failing, tests passing Red, Green, Refactor. Remove old tests. Use rspec-fire/minitest-firemock Code without tests Write more tests Logic in view files See previous slide, then write tests. $LIBRARY being tested Stub, Spy Code to be refactored without tests Write black box tests, refactor/rewrite, write unit tests Slow tests Replace factories with plain objects Fixtures Replace with plain objects Slow tests Decouple. Remove test_helper/spec_helper Brittle feature tests Page Object Model
  13. #5 The monorail 1. Functional Architecture 2. Gems 3. Engines

    4. Multiple apps + channel 5. Channels: HTTP, MQ
  14. References Name Examples Form object Reform, Virtus View objects Draper,

    The four Decorators Permitters Permitters Private AR methods Figleaf Strong Parameters Strong Parameters Background Jobs Resque, Sidekiq Messaging Patterns AS::N, Wisper, RabbitMQ Stricter templating languages Liquid, Handlebars, Curly Exposure Inherited Resources, Irie, Decent Exposure, Obviews Service objects Interactor Query objects Filterer Page Object Model Site Prism
  15. Learning resources 1. Avdi Grimm’s blog 2. Objects on Rails

    3. Practical Object Oriented Design in Ruby 4. Fast Rails Tests 5. Ruby Weekly
  16. Help us follow these! careers@bangthetable.com ! ! or, you know,

    just come and talk to me :) ! Perks: 1. Multi-national startup. 2. ♥ new technologies 3. Great learning environment. 4. Competitive salary 5. Free lunch, a fridge full of food, PF, insurance, and a MacBook Pro/ Thinkpad. 6. Flexible work timings 7. Work remotely 8. We are young, quirky, and seek adventure. Sorta like Bilbo Baggins.