Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Antipatterns with Ruby on Rails [closed]

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

like image 642
Jakub Arnold Avatar asked Aug 25 '09 17:08

Jakub Arnold


4 Answers

Not learning Ruby.

like image 118
Jörg W Mittag Avatar answered Nov 12 '22 05:11

Jörg W Mittag


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.

like image 13
rcoder Avatar answered Nov 12 '22 05:11

rcoder


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
like image 2
Jazmin Avatar answered Nov 12 '22 04:11

Jazmin


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.

like image 1
Justin Niessner Avatar answered Nov 12 '22 04:11

Justin Niessner