Rails code review by Jamis Buck and Marcel Molina, Jr.

I watched a Rails code review by Jamis Buck and Marcel Molina Jr.

Here are my own highlights:

  • difference between to_i and Integer()
    • value.to_i is lenient
    • Integer(value) will raise an exception if the value is not an integer
  • strive for “symbolics”
    • wrap complex condition statements into intention-revealing methods
    • keep abstractions of the same level together in code (1 level of abstraction for a specific scope)
  • problem: checking a few params and validate them (inside a create for example)
    • solution: use the rescue clause and create an exists! inside the model, and let the controller handle the flow
  • use && instead of and
    • See this for a good example.
  • skinny controllers, short action methods
  • don’t be afraid to create models that don’t inherit ActiveRecord::Base
  • make simple actions explicit in the controller
  • Avoid implicit (absent) actions in the controller; make it explicit:

    def contact
    render
    end

  • use the block helper pattern:


  • with view helper code that looks like:

    def editable_post(&block)
    concat(block.call, block.binding) if condition
    end

    instead of using arbitrary instance flag which was set in the controller:



    This allows you to do pre/post processing (e.g. encapsulating the editable content inside a div), without repeating it everywhere in your view template (since it would only be in your view helper).

Advertisements

2 comments

  1. what do you mean in your last comment about ‘block helper pattern’ – seems like the code sample isn’t rendered correctly.

  2. I believe the code sample was rendered correctly.However, I tried to make things clearer and added the code that would go into the view helper. Hope this helps.

Got a comment?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: