Skip to content

Instantly share code, notes, and snippets.

@hmemcpy
Created May 3, 2012 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmemcpy/2585890 to your computer and use it in GitHub Desktop.
Save hmemcpy/2585890 to your computer and use it in GitHub Desktop.
Which do you like better?
// #1
public IModule GetTargetModule(ICSharpExpression expression)
{
var referenceExpression = expression as IReferenceExpression;
if (referenceExpression != null)
{
var typeofExpression = referenceExpression.QualifierExpression as ITypeofExpression;
if (typeofExpression != null)
{
IDeclaredType declaredType = typeofExpression.ArgumentType.GetScalarType();
if (declaredType != null)
{
ITypeElement typeElement = declaredType.GetTypeElement();
if (typeElement != null)
{
return typeElement.Module.ContainingProjectModule;
}
}
}
}
return null;
}
// or #2
public IModule GetTargetModule(ICSharpExpression expression)
{
// see http://devtalk.net/csharp/chained-null-checks-and-the-maybe-monad/
return this.With(x => expression as IReferenceExpression)
.With(x => x.QualifierExpression as ITypeofExpression)
.With(x => x.ArgumentType.GetScalarType())
.With(x => x.GetTypeElement())
.Return(x => x.Module.ContainingProjectModule, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment