I've got a case where I need to call a couple of methods, checking if they return null, and return some property from the result of the second when neither returns null.
I've thought of two ways to do this - one "verbose" and one "concise". For the purposes of this question, I'll just call the return types 'dynamic' since I'm not interested in those details here.
private dynamic VerboseVersion()
{
var a = CallMethodA();
if ( a == null )
{
return null;
}
var b = CallMethodB( a );
if ( b == null )
{
return null;
}
return b.SomeProperty;
}
private dynamic ConciseVersion()
{
dynamic a;
dynamic b;
if ( ( a = CallMethodA() ) == null ||
( b = CallMethodB( a ) ) == null )
{
return null;
}
return b.SomeProperty;
}
I the first because the intent at each step is very obvious but I find it somewhat verbose; I like the second because the intent of the function seems more obvious and it is less verbose but it may be a bit harder to follow at first.
Thoughts?
Update: Corrected a typo and corrected the formatting of the "Verbose" version to what I would actually use in committed code.
return null;
\$\endgroup\$ – NetMage Aug 3 '11 at 19:19