patterncsharpCritical
Mutually exclusive properties
Viewed 0 times
exclusivepropertiesmutually
Problem
The question is probably quite simple, but I would like to hear what drawbacks we will have with our code.
So, we had a simple implementation and interface for it:
The property shows the application state and used in many conditions. The interface in our case is required for implementing settings for different platforms.
My colleague insists on implementing an additional property:
Is it a good practice to have such additional properties just for convenient code-reading?
So, we had a simple implementation and interface for it:
public interface ISettings
{
bool IsOnline { get; set; }
}
public class Settings : ISettings
{
public bool IsOnline { get; set; }
}The property shows the application state and used in many conditions. The interface in our case is required for implementing settings for different platforms.
My colleague insists on implementing an additional property:
public interface ISettings
{
bool IsOnline { get; set; }
bool IsOffline { get; set; }
}
public class Settings : ISettings
{
public bool IsOnline { get; set; }
public bool IsOffline
{
get { return !IsOnline; }
set { IsOnline = !value; }
}
}Is it a good practice to have such additional properties just for convenient code-reading?
Solution
I think that having both properties is actually somewhat harmful. If there is just single property, then it's clear what it means: settings can be either online or not.
But when you have two properties (and don't know their implementation), then you start to wonder: is there some third case? Can both properties be
Good documentation would answer all those questions, but it's always better if something is clear from the start and doesn't have to be explained.
But when you have two properties (and don't know their implementation), then you start to wonder: is there some third case? Can both properties be
true, or both false? Are the two properties actually related, or is it just confusing naming?Good documentation would answer all those questions, but it's always better if something is clear from the start and doesn't have to be explained.
Context
StackExchange Code Review Q#32743, answer score: 112
Revisions (0)
No revisions yet.