Parameter vs. Member variables

前端 未结 9 823
别跟我提以往
别跟我提以往 2021-01-01 11:15

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 t

相关标签:
9条回答
  • 2021-01-01 12:00

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

    0 讨论(0)
  • 2021-01-01 12:01

    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
    0 讨论(0)
  • 2021-01-01 12:02

    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.

    0 讨论(0)
提交回复
热议问题