I am reviewing another developer’s code and he has written a lot of code for class level variables that is similar to the following:
/// <summary>
/// how often to check for messages
/// </summary>
private int CheckForMessagesMilliSeconds { get; set; }
/// <summary>
/// application path
/// </summary>
private string AppPath { get; set; }
Doesn’t coding this way add unnecessary overhead since the variable is private?
Am I not considering a situation where this pattern of coding is required for private variables?
Private properties provide you a level of abstraction when you assign the value to the private variable (which is created for you by the compiler). It’s not less efficient to do so, and if you need to change how the assignment is done in the future, you only have to worry about updating it in one place.
It can also provide consistency in an application. If all assignments are done through properties, then some can validate the assignment values while others not. To the person interfacing with the property, there is no difference between those which validate and those which do not. (and this way someone doesn’t accidentally assign a value to a local variable which doesn’t validate.)