Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I recently found myself writing the following:

task :spec do |t, args|
  require_relative 'spec/helper'
  helper = Library::Specs::Helper
  helper.start_coverage
  if condition
    helper.do_something(foo)
  else
    arr.each do |el|
      helper.do_something(el)
    end
  end
end

My question is: is it bad practice to assign the name of a class to a variable to save space? Clearly, it makes the code much terser and readable. Are there any downsides?

share|improve this question
3  
What kind of downside are you thinking of? that it's less clear what you are calling? I cannot think of anything, people do it all the time and it just works. –  tokland Dec 12 '12 at 7:01

1 Answer 1

Normally you would put helper methods in a module not in a class and include them where needed, so you could use them directly (especially if you're concerned about terseness).

If you need a class for some reason this might be also a static case of the "feature envy" code smell, because you're constantly calling methods of the other class. In this case you can implement another helper method directly in the class and you're left with only one call from the outside.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.