Suppose I have an object with two logical properties:
public bool AdvancedMode{ get; set; }
public bool DumpDiagnostics{ get; set; }
Domain rules state that DumpDiagnostics is not allowed unless the AdvancedMode parameter is specified. Therefore, in the whole code you can see the following statements:
if( options.AdvancedMode && options.DumpDiagnostics ){ ... }
The problem is that it's easy to forget to add validation for AdvancedMode and accidentally validate only for DumpDiagnostics.
if( options.DumpDiagnostics ) // Oh no!
So there are two solutions that I can think of:
Option 1: DumpDiagnostics returns true only when AdvancedMode is set.
public bool DumpDiagnostics
{
get{ return AdvancedMode && _dumpDiagnostics; }
set{ _dumpDiagnostics = value; }
}
Option 2: a new property that reflects the state I'm looking for
public bool CanDumpDiagnostics{ get{ return AdvancedMode && DumpDiagnostics; } }
Question: which is better?
№1 . , . set → get, DumpDiagnostics = true, if (DumpDiagnostics), false.
№ 2 , , . , , , DumpDiagnostics CanDumpDiagnostics.
p.s. , - , .