Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;     } } 
like image 411
cwallenpoole Avatar asked Feb 09 '09 19:02

cwallenpoole


People also ask

What is the difference between a parameter and a variable?

There is a clear difference between variables and parameters. A variable represents a model state, and may change during simulation. A parameter is commonly used to describe objects statically. A parameter is normally a constant in a single simulation, and is changed only when you need to adjust your model behavior.

What is the difference between member and variable?

A local variable is the variable you declare in a function. Its lifespan is on that Function only. A member variable is the variable you declare in a class definition. Its lifespan is inside that class only.It is Global Variable.It can be access by any function inside that same class.

What is a parameter variable?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: function example(parameter) { console.

What is meant by member variable?

In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (member functions).


2 Answers

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).

like image 198
Michael Burr Avatar answered Sep 19 '22 08:09

Michael Burr


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.

like image 37
Konrad Rudolph Avatar answered Sep 21 '22 08:09

Konrad Rudolph