8

What are most common Ruby on Rails antipatterns and how to avoid them?

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327

4 Answers4

14

Not learning Ruby.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
13

There are two major anti-patterns I've seen in a lot of Rails code:

  1. Lots of "heavy lifting" in views. Anything more complicated than simple iteration over collections or interpolation of strings should be in helpers or model methods. Don't query for model objects, construct big JSON arrays, or update session variables from your ERB templates.

  2. Model objects which aren't usable for scripting or API implementation. Your models define the domain semantics for your application. You should be able to fire up script/console, or write service API wrappers, which reuse existing, functional model methods to manipulate all of the key data in your application. Controller functionality is only available in the HTTP request/response cycle, which is only part of any full-featured site's lifecycle.

rcoder
  • 12,229
  • 2
  • 23
  • 19
2

USING unless WITH else

Antipattern:

unless is_the_weekend?
  do stuff that you do during the week
else
  do stuff that you do on weekends
end

Alternative:

if is_the_weekend?
  do stuff that you do on weekends
else
  do stuff that you do during the week
end
Jazmin
  • 271
  • 2
  • 6
1

Alphabet Soup?

(No type declared and meaningless variable naming which leads to nearly un-readable code)

Pattern name comes from variables names as 'a','b','c','d', etc.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 5
    is this somehow RoR-specific? – SilentGhost Aug 25 '09 at 18:22
  • Not in the least. It's something I've noticed a lot more in Ruby than other langauges (and the majority of what I've seen Ruby used for is RoR). – Justin Niessner Aug 25 '09 at 18:51
  • 1
    of course there is no type declared, it is a dynamic language. and anyone who uses a single letter as a variable as anything but an iterator in any language needs something hard thrown at them – Matt Briggs Aug 25 '09 at 19:30