D
David Veeneman
Should a member variable be passed to a private method in the same class as
a method argument, or should the method simply call the member variable?
For years, I have passed member variables to private methods in the same
class as method arguments. For example, if Public method Foo() calls private
method Bar(), and if Bar() uses member variable m_MyVariable, I declare the
private method with the signature:
private void Bar(SomeType myVariable)
{
...
}
Foo() calls Bar() as:
this.Bar(m_MyVariable);
In other words, even though Bar() could call m_MyVariable directly, I don't
do it. Instead, I create a local variable within Bar() to hold the value.
I've done this to keep my private methods as self-contained as possible, and
to make it obvious from the method signature just what the method needs in
order to do its job. Arguably, this approach makes it easier to follow the
flow of control within the program.
But now I'm not so sure it's such a good idea. I often end up with long
method signatures with a half-dozen or more arguments, and I find that I'm
passing the same member variables (typically helper objects) to every
private method in a class.
So, what do you think? Am I better off passing member variables to private
methods as arguments, or should I simply let the methods call member
variables directly? Thanks for your input.
David Veeneman
Foresight Systems
a method argument, or should the method simply call the member variable?
For years, I have passed member variables to private methods in the same
class as method arguments. For example, if Public method Foo() calls private
method Bar(), and if Bar() uses member variable m_MyVariable, I declare the
private method with the signature:
private void Bar(SomeType myVariable)
{
...
}
Foo() calls Bar() as:
this.Bar(m_MyVariable);
In other words, even though Bar() could call m_MyVariable directly, I don't
do it. Instead, I create a local variable within Bar() to hold the value.
I've done this to keep my private methods as self-contained as possible, and
to make it obvious from the method signature just what the method needs in
order to do its job. Arguably, this approach makes it easier to follow the
flow of control within the program.
But now I'm not so sure it's such a good idea. I often end up with long
method signatures with a half-dozen or more arguments, and I find that I'm
passing the same member variables (typically helper objects) to every
private method in a class.
So, what do you think? Am I better off passing member variables to private
methods as arguments, or should I simply let the methods call member
variables directly? Thanks for your input.
David Veeneman
Foresight Systems