I have this C# solution in which we use a certain pattern. A function returns true/false whether it succeed, and some value. If the function returned false, the value may not be used. Thus in the scope where such a function (with this particular pattern) is called, the IsSuccessful
boolean must be evaluated.
We work with VS2013 + ReSharper 9. Is there a way to automatically check whether this pattern is obeyed in the code? If it is not possible with these tools, are there other tools? My last resort would be to write a unit test that performs this static code analysis.
Code example:
public ReturnValue<T> MyMethod()
{
try
{
....
return new ReturnValue<T>(someValue);
}
catch(Exception ex)
{
return new ReturnValue<T>(ex);
}
}
var returnValue = MyMethod();
if(!returnValue.IsSuccesful)
{
//Log error
return; //Can't go on, previous function failed
}
//Is successful continue code
public class ReturnValue<T>
{
public bool IsSuccessful { get; private set; }
public T Value { get; private set; }
public ReturnValue<T>(Exception ex)
{
IsSuccessful = false;
}
public ReturnValue<T>(T valueToReturn)
{
IsSuccessful = true;
Value = valueToReturn;
}
}
Edit:
I semi-solved my issue. Upon creation of the object the current stacktrace is saved (new StackTrace()
). Upon destruction of the object (when the garbage collectors cleans it up), it is checked whether the IsSuccesful
property of the object was ever evaluated during its lifespan. If not, a warning with the stacktrace is logged.