开发者

Which is better? Creating a instance variable or passing around a local variable in Ruby?

开发者 https://www.devze.com 2023-01-26 16:19 出处:网络
In general what is the best practice and pro/cons to creating an instance variable that can be accessed from multiple methods or creating an instance variable that is simply passed as an argument to t

In general what is the best practice and pro/cons to creating an instance variable that can be accessed from multiple methods or creating an instance variable that is simply passed as an argument to those methods. Functionally they are equivalent since the methods are still able to do开发者_开发百科 the work using the variable. While I could see a benefit if you were updating the variable and wanted to return the updated value but in my specific case the variable is never updated only read by each method to decide how to operate.

Example code to be clear:

class Test
  @foo = "something"

  def self.a
    if @foo == "something"
      puts "do #{@foo}"
    end
  end

  a()
end

vs

class Test
  foo = "something"

  def self.a(foo)
    if foo == "something"
      puts "do #{foo}"
    end
  end

  a(foo)
end


I don't pass instance variable around. They are state values for the instance.

Think of them as part of the DNA of that particular object, so they'll always be part of what makes the object be what it is. If I call a method of that object, it will already know how to access its own DNA and will do it internally, not through some parameter being passed in.

If I want to apply something that is foreign to the object, then I'll have to pass it in via the parameters.


As you mentioned, this is a non-functional issue about the code. With that in mind...

It's hard to give a definitive rule about it since it depends entirely on the context. Is the variable set once and forgotten about it, or constantly updated? How many methods share the same variable? How will the code be used?

In my experience, variables that drive behavior of the object but are seldom (if at all) modified are set in the initialize method, or given to the method that will cascade behavior. Libraries and leaf methods tend to have the variable passed in, as it's likely somebody will want to call it in isolation.

I'd suggest you start by passing everything first, and then refactoring if you notice the same variable being passed around all over the class.


If I need a variable that is scoped at the instance level, I use an instance variable, set in the initialize method.

If I need a variable that is scoped at the method level (that is, a value that is passed from one method to another method) I create the variable at the method level.

So the answer to your question is "When should my variable be in scope" and I can't really answer that without seeing all of your code and knowing what you plan to do with it.

If your object behavior should be statically set in the initialization phase, I would use an instance variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消