Clean Code: Should Objects have public properties?

前端 未结 13 1771
礼貌的吻别
礼貌的吻别 2021-01-03 20:40

I\'m reading the book \"Clean Code\" and am struggling with a concept. When discussing Objects and Data Structures, it states the following:

  • Objects hide thei
13条回答
  •  忘掉有多难
    2021-01-03 21:00

    The book is trying describe the theory that an object should not expose how the class is actually implemented. In more complicated objects many of the internal variables don't necessarily convey the right information from an outside perspective and should just have methods that act on them.

    However, making this a hard and fast rule falls apart when you have simple objects. In the case of a rectangle, height and width are basic properties that the user will want to know. And since the implementation of this is straight forward, not using get and set will just make your code more complicated than it needs to be.

提交回复
热议问题