Are instance variables new global variables?

Recently, I have the feeling that variable instances have the same problems with global variables, I looked for this information and found this old article , more or less describes the potential problem that I am seeing.

What good practices do you use to avoid the same global variable problems affect instance variables or class variables?

+5
source share
3 answers

Classes are much smaller than the global structure, so the effect of the instance variable is much less. By maintaining the small size of classes and adhering to the principle of shared responsibility, most of the shortcomings of the global variable are prevented. If an instance variable is created from the passed parameter, I often make this parameter required in the constructor, making the dependency explicit. In addition, the instance variable is encapsulated well, never being directly modified outside the instance methods, which makes it very easy to determine where the instance variable changes. Finally, the instance variable should make sense for the class as a whole or should be private.

+2
source

. , , . , , , , .

0

Nor Instance, , - ""... . , " ". , , .

-, CurrentUserName... , , CurrentUserName - - . " " CurrentUserName . , , , - ( , )...

- , , .

, , : , , , , Student Person and Teacher Person. , ...

Person - , . ... , , , , , . ....

, "" ... , , "" , , ...

, , ....

, , ... , ... , t , , , .

By the way: I also think that a User having a list of email addresses is erroneous, but for a completely different set of motives. I would use

class ContactInformation
{
    User contact;
    EMailAddress email;
}

and remember that objects do not "own" and do not "have" other objects ... This is an implementation decision ... Objects simply "know" other objects ...

0
source

All Articles