0

I notice in Rails tutorials sometimes the authors make helper methods that are only used one time. This strikes me as ridiculous but while learning ActionCable I noticed that DHH did the same thing in his introduction for ActionCable 2 years ago. So maybe there is a point to it that I am missing. Just for illustration, here is the code from DHH.

# app/jobs/message_broadcast_job.rb
class MessageBroadcastJob < ApplicationJob
  queue_as :default

  def perform(message)
    ActionCable.server.broadcast 'room_channel', message: render_message(message)
  end

  private
    def render_message(message)
      ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
    end
end

Now I would just write the perform method something like this, and skip the helper method:

def perform(message)
  ActionCable.server.broadcast 'room_channel', { 
    message: ApplicationController.renderer.render(
      partial: 'messages/message', 
      locals: { message: message }
    )
  }
end

Is there any benefit to the first structure over mine? I only see a pointless abstraction.

Steve Carey
  • 2,216
  • 17
  • 22
  • 1
    I find the first one easier to read and understand. And, in the first, each method does exactly one thing. Others (or, perhaps, the future you) might appreciate the greater clarity and separation of responsibilities. – jvillian Oct 30 '17 at 18:49
  • Exactly, DRY is only one reason to extract functionality to seperate methods. Not the only reason. – max Oct 30 '17 at 18:52
  • Its really easy to see at a glance that `render_message` takes a message object and returns a string. Telling what your perform method does in example two is more difficult. – max Oct 30 '17 at 18:54
  • The comments so far seem to be personal preference which is fine. Doesn't seem to be anything more than that. – Steve Carey Oct 30 '17 at 19:12

2 Answers2

1

What you have written is fine but in ruby on rails there are processes to follow & it's your own choice to follow or not.

We use helpers for following reasons .

  1. Organise the code well in the application.
  2. Use DRY concept so that , if require same helper method can be use by multiple methods or actions .

Hope it clear the purpose of using helpers in ruby on rails application.

Thanks & Regards Harender

  • DRY is not applicable here since it is only used once. In terms of processes to follow, what process are you speaking of? I'm still not seeing any point to the abstraction, particularly since it requires more lines of code, not less. – Steve Carey Oct 30 '17 at 19:09
0

There's two valid reasons for creating a helper method (or any method for that matter):

  1. To make a piece of code reusable
  2. To make the purpose of the code clear to anyone reading it

I think both are valid on their own. You should ask yourself if creating a helper method will add clarity to the application logic, and make it easier for anybody (even you) to understand what's going on. If that's the case, I think it's fine to create a helper that will only be used once.

In a large application, I find clarity to be generally more important than a few extra lines of code. Someone might even find another use for it later.

Roma149
  • 1,331
  • 1
  • 9
  • 10