« It's a Hash-rock't Life For Us! | Main | Every generation needs a new revolution »

May 19, 2008

Truly Awful Rails Code (Send it to me!)

There's tons of material out there telling us how to properly write code (or attempting to anyway), but in an effort to be creatively different, I decided that my Railsconf talk this year will be all about bad Rails code. Entitled "The Worst Rails Code You've Ever Seen...", I'm basing it on both code and practices that I've both done myself, stumbled across as a neutral observer or rescued during the last few years.

After working through a draft, I'm feeling a little short on examples, so I'm reaching out to you for some help. Please email me your heinous Rails coding examples, stuff that you're truly embarrassed about. I'm looking for stuff that has obvious "better ways" or not, the latter being good topics for discussions of how Rails occasionally leads developers down the wrong path.

If you want to be a little bit more anonymous than email permits, you can also post a comment to this entry linking to snippets on Pastie. Not to worry, none of the example code in the talk will be direct copy-paste of actual client code or stuff that you submit. I plan on changing variable names and anonymizing stuff to the best of my ability and for my legal protection.

If I get enough material, I plan to make the content of the talk and submissions into a regular series of posts on this blog. Thanks in advance for your help!

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54fdca911883300e5525648ae8834

Listed below are links to weblogs that reference Truly Awful Rails Code (Send it to me!):

I want to be clear I didn't write these, and when I saw them I had to stare at them for awhile and let it perculate in my brain like when you fill your mouth with coca cola and pop rocks:

Example1
http://pastie.caboo.se/199740

Example 2
http://pastie.caboo.se/199742

For the last year we've documented a bunch of atrocities at blogyourshame.com.

Nested interpolations:
http://pastie.caboo.se/199829

Just because sometimes we're too lazy to write a helper method that will only be used once, or to write a 12-line conditional tree... only to realize a couple of months later that we've copy/pasted that same expression a dozen times.

Had to write the following code yesterday evening:

http://pastie.caboo.se/200074

Because a simple "update_attributes" or "attributes=" followed by "save(false)" wouldn't work (ignoring the newly added columns) when migrating down several migrations.

"update_attributes" works nicely when migrating down only this single migration...

There are things I don't understand sometimes :P
Maybe someone has got an answer :)

Well it seems ActiveRecord::Base#reset_column_information is the answer to the awful code I've written :D

http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001408

I guess this is how most of us start coming to rails from other technology..

http://pastie.caboo.se/200140

This is some of the worst code I've ever seen. It's an action on a controller.

Take note of the global variables. I had to ask around on the atlrug irc channel because I'd never seen a global variable in Ruby before. I figured the dollar signs were just from a former PHP developer.

Can you imagine whoever wrote this protesting that "it works fine on my local machine..."?

Ugh...

http://pastie.org/200200

I found one this morning in ActiveResource::Base#method_missing:

case method_name.last
when "="
attributes[method_name.first(-1)] = arguments.first

...uh...

first(-1) ?!?!? What's that supposed to do?

And looking into ActiveSupport::CoreExtensions::String::Access the
plot thickens:

def first(limit = 1)
chars[0..(limit - 1)].to_s
end

My favorite aspect of the whole thing might be that -1 becomes -2 along the way.

for the safety of you and those you love, please wear the appropriate goggles when viewing this code.

http://pastie.caboo.se/200240

I'm pretty sure this is going to take the cake. It's already been featured in a RailsWTF?

Apparently, the developer(s) who wrote this decided that filters just weren't hip enough. Instead, they pushed all responsibility for permissions into one method in the super class, the Application Controller.

It's so bad that it's awesome in its own way. :)

http://pastie.caboo.se/200406

Post a comment

If you have a TypeKey or TypePad account, please Sign In

My Company

My Conference

Bizconf is the first and only business conference specifically for owners and managers of small to mid-sized web design and development firms.

August 20-21 at the Ritz-Carlton Amelia Island Resort in Florida

My Book Series

My Travel