One my former coworkers recently posed the question, does it really matter if you use a field or a property? He immediately clarified he understood the importance of encapsulation, but what if you just need to store a simple piece of data?
I’m writing within the context of C# and Java. There are other languages out there, but they’re what I’m most familiar with. I’m going to discuss public fields first, and nonpublic fields afterwards.
It is impossible to separate the access level of reading and writing a field. When you use a public field, you are allowing any code that might use your class to alter the field value at any time. This means that you should not perform conditional logic based on the value of the field because you cannot control value changes.
Recent comments
1 year 10 weeks ago