cwallenpoole
cwallenpoole

Reputation: 82068

Parameter vs. Member variables

I've recently been working with someone else's code and I realized that this individual has a very different philosophy regarding private variables and method parameters than I do. I generally feel that private variables should only be used in a case when:

  1. The variable needs to be stored for recall later.
  2. The data stored in the variable is used globally in the class.
  3. When the variable needs to be globally manipulated (something decidedly different from the need to read the variable by every class method).
  4. When it will make programming substantially easier. (Admittedly vague, but one has to be in many circumstances to avoid painting oneself into a corner).

(I admit, that many of the above are slightly repetitive, but they each seem different enough to merit such treatment... )

It just seems that this is the most efficient means of preventing changing a variable by accident. It also seems like following these standards will allow for the eventual manipulation of external references (if the class is eventually modified), thus leaving you with further options in the future. Is this simply a style issue (like one true bracket or Hungarian naming conventions), or do I have justification in this belief? Is there actually a best practice in this case?

edit
I think this needs to be corrected. I used "globally" above where I actually meant, "globally by instance methods" not "globally accessible by anything, anywhere".

edit2
An example was asked for:

class foo
{
    private $_my_private_variable;

    public function __constructor__()
    {
    }

    public function useFoo( $variable )
    {
        // This is the line I am wondering about,
        // there does not seem to be a need for storing it.
        $this->_my_private_variable = $variable; 
        $this->_doSometing();
    }

    private function _doSomething()
    {

        /*
          do something with $this->_my_private_variable.
        */
        // This is the only place _my_private_variable is used.
        echo $this->_my_private_variable;
    }
}

This is the way I would have done it:

class foo
{

    public function __constructor__()
    {
    }

    public function useFoo( $variable )
    {
        $this->_doSometing( $variable );
    }

    private function _doSomething( $passed_variable )
    {
        /*
          do something with the parameter.
        */
        echo $passed_variable;
    }
}

Upvotes: 29

Views: 12860

Answers (9)

Chase Liu
Chase Liu

Reputation: 46

I think the answer is straightforward if you are familiar with C++ destructors. All member variables should be assigned a way to be destructed while function parameters are not. So that's why member variables are usually the states or dependicies of an object having some kind of relation regarding their lifecycle.

Upvotes: 1

nikk wong
nikk wong

Reputation: 8690

Class members should be any of the following:

  • A dependency of a class
  • A variable that represents the state of the class
  • A method of the class

Upvotes: 3

DanMan
DanMan

Reputation: 11561

Since object properties are meant to hold state, as stated by the others, my policy is to have all of them private by default unless I have a good reason to expose them.

It's much easier to make them public later on, if you have to, simply by writing a getter method for example (which i also don't have to think about right at the beginning of writing a class). But reeling in a public property later on may require a huge amount of code to be re-written.

I like to keep it flexible while not having to think about this more than needed.

Upvotes: -1

Michael Burr
Michael Burr

Reputation: 340426

In general, class members should represent state of the class object.

They are not temporary locations for method parameters (that's what method parameters are for).

Upvotes: 37

Michael Glenn
Michael Glenn

Reputation: 1882

I would disagree with implementing it for global access or to make programming easier. By exposing these globally without filtering of any kind make it more difficult to determine access in the future.

Upvotes: -1

Scott Dorman
Scott Dorman

Reputation: 42526

I'm not sure there is a stated best-practice for using globally scoped variables versus always passing as method parameters. (By "private variables", I'm assuming you mean globally scoped variables.)

Using a globally scoped variable is the only way to implement properties in .NET (even automatic properties ultimately use a globally scoped variable, just not one you have to declare yourself).

There is a line of arguement for always using method parameters because it makes it completely clear where the value is coming from. I don't think it really helps prevent the method from making changes to the underlying value and it can, in my opinion, make things more difficult to read at times.

Upvotes: 0

Alan Mullett
Alan Mullett

Reputation: 1116

Having a member variable implies that it will be holding state that needs to be held between method calls. If the value doesn't need to live between calls it has no reason to exist outside of the scope of a single call, and thus (if it exists at all) should be a variable within the method itself.

Style is always a hard one, once you develop one you can get stuck in a bit of a rut and it can be difficult to see why what you do may not be the best way.

Upvotes: 8

Konrad Rudolph
Konrad Rudolph

Reputation: 546015

I claim that it isn't a style issue but rather a readability/maintainability issue. One variable should have one use, and one use only. “Recycling” variables for different purposes just because they happen to require the same type doesn't make any sense.

From your description it sounds as if the other person's code you worked on does exactly this, since all other uses are basically covered by your list. Put simply, it uses private member variables to act as temporaries depending on situation. Am I right to assume this? If so, the code is horrible.

The smaller the lexical scope and lifetime of any given variable, the less possiblity of erroneous use and the better for resource disposal.

Upvotes: 9

David Anderson
David Anderson

Reputation: 13680

You should only create variables when and where they are needed, and dispose of them when you are done. If the class doesn't need a class level variable to function, then it just doesn't need one. Creating variables where you don't need them is very bad practice.

Upvotes: 4

Related Questions