Using properties instead of fields in class methods of the same unit is a bad practice?

前端 未结 3 1867
太阳男子
太阳男子 2020-12-20 20:36

I have declared a private field and a public property for a given class.

From other units I can access the field through the public property that provides access to

3条回答
  •  清歌不尽
    2020-12-20 20:58

    Well, this is probably a matter largely for personal taste.

    Myself I would always opt for the property, even when coding internal to the class that declares the property. For example, Count rather than FCount reads better, in my view.

    Another perspective would be that if you have exposed a property to the public, and it is good enough for public consumption, then it should be fine for private consumption.

    Yet another take would be that if you opt to use the most publicly available interface wherever possible, then it will be more obvious when you are using something private. So, if you find that you need to write FCount because there is no Count, then you have a gentle reminder that this is a private name that you are using.

    So, as I said, no definitive answer, just my own personal opinions and preferences.

提交回复
热议问题