What are most common Ruby on Rails antipatterns and how to avoid them?
Not learning Ruby.
There are two major anti-patterns I've seen in a lot of Rails code:
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With