Obie Fernandez - Worst Rails Code

Preview:

DESCRIPTION

railsconf

Citation preview

The Worst Rails Code You've Ever Seen

(and how not to write it yourself)

Obie FernandezRein Henrichs

RAILS FAIL

By “worst” I don’t mean stupid

3

FAIL

“stupid”is a really subjective term

FAIL?

I do mean not following

the Ruby or Rails Way

of doing things

Ruby BasicsLearn them before calling yourself a Rails developer

hash.values

pastie-grab

Wheel Mark IIDon’t reinvent stuff that Ruby or Rails already gives you

return if self.include? keyword

The Problem with Reinvention

Your version is probably worse

javascript_include_tag :all

RoutesHeard of them?

AssociationsHeard of them?

VerbosityWhy do in one line what can be done in five?

Lazinessleads to sloppiness

<%= "Count #{users.count + "!#{"!!" if users.count > 10}" unless users.count == 0}" %>

Integration of Concerns

Multi-purpose methods are best?!

Premature Deoptimization

Why make things slower later when you can make them slower now?

SRSLY?

Application-wide Actions

Abusing application.rb for fail and losses

Database AbuseHow to create thousands of queries in a just a few lines

of code...

coordinate_turns.sort! do |x,y| #orders coordinates for this turn from the one with the most inscriptions to the one with the least. Assignment.find_all_by_start_and_coordinate_id(turn,x).inject(0){|a,s| a += s.inscriptions.size } <=> Assignment. find_all_by_start_and_coordinate_id(turn,y).inject(0){|a,s| a += s.inscriptions.size }end

cache_snafuHow not to cache...

Grotesquely Obese Controller Methods

Fat? Yeah, right...

Deliberate ObfuscationFor no good reason...

Why?

Additional Examples(time permitting)

The Shopping Cart Controller

When non-restful controllers go bad...

aav_codePlugins...

photos_helperWhen HTML generation goes way too far

listing.rbA method named finderer O RLY?

ui_user_controller.rbHow not to use the flash hash

users_controller.rb1,131 lines of pure FAIL

Conclusion

FundamentalsLearn them

Senior PeopleSeek them out and pair with them

Read Some BooksBesides mine, there are some essentials

Recommended