S
Sam Kong
Hello!
I have a question about using instance member variables in a method.
This is rather a general OO implementation question than specific Ruby
one.
As you know, all instance methods can access insatance member variables
freely.
Thus, instance member variables are like global variables in a class (I
mean... in an object).
So you don't have to pass those data beween methods in a class.
However, I feel more comfortable when I make those data passed as
arguments.
See the examples (very simple and stupid one):
class Tax1
def initialize(emp_id)
@emp_id = emp_id
end
def tax
#explicitly pass the argument even if get_salary can access @emp_id
salary = get_salary(@emp_id)
return salary * 0.1
end
def get_salary(emp_id)
#do some calcuation using emp_id and data from db
#...
return salary
end
private :get_salary
end
class Tax2
def initialize(emp_id)
@emp_id = emp_id
end
def tax
salary = get_salary #no argument
return salary * 0.1
end
def get_salary
#do some calcuation using @emp_id and data from db
#...
return salary
end
private :get_salary
end
For public methods, it's obvious that you shouldn't put arguments as
the format affects objects' clients.
But for private methods, I like to make the methods more standalone
(not depending the member variables).
Is this an acceptable habit?
Do you feel the same way as I do?
Thanks.
Sam
I have a question about using instance member variables in a method.
This is rather a general OO implementation question than specific Ruby
one.
As you know, all instance methods can access insatance member variables
freely.
Thus, instance member variables are like global variables in a class (I
mean... in an object).
So you don't have to pass those data beween methods in a class.
However, I feel more comfortable when I make those data passed as
arguments.
See the examples (very simple and stupid one):
class Tax1
def initialize(emp_id)
@emp_id = emp_id
end
def tax
#explicitly pass the argument even if get_salary can access @emp_id
salary = get_salary(@emp_id)
return salary * 0.1
end
def get_salary(emp_id)
#do some calcuation using emp_id and data from db
#...
return salary
end
private :get_salary
end
class Tax2
def initialize(emp_id)
@emp_id = emp_id
end
def tax
salary = get_salary #no argument
return salary * 0.1
end
def get_salary
#do some calcuation using @emp_id and data from db
#...
return salary
end
private :get_salary
end
For public methods, it's obvious that you shouldn't put arguments as
the format affects objects' clients.
But for private methods, I like to make the methods more standalone
(not depending the member variables).
Is this an acceptable habit?
Do you feel the same way as I do?
Thanks.
Sam