Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean Code: Should Objects have public properties?

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 their data behind abstractions and expose functions that operate on that data.
  • Data Structures expose their data and have no meaningful functions.

So, what I'm getting from this is that I shouldn't have any public properties on my object, I should only have methods that perform operations on the properties. If I do need to access properties, they should be on a Data Structure, which could be returned from a method on my object? With this approach, it seems that I would need a GetHeight() and SetHeight() method for my Height property on my object, rather than just using get and set of the property.

Maybe I'm not understanding exactly what is being suggested, but this is my understanding of "Objects hide their data." If you could help me understand this, I'd greatly appreciate it!

Thanks in advance!

like image 834
JSprang Avatar asked Jul 07 '10 13:07

JSprang


1 Answers

Indeed a C# property is not data, is an accessor, so it's a function operating on data.

You should avoid public fields, not public properties.

like image 80
onof Avatar answered Oct 03 '22 01:10

onof