I have a control flow question. In my company we create a lot of bool methods that return false if there was an error. Example:
public bool Foo(string path, string fileName, ref string error)
{
if (path == null)
{
error = "path is null";
return false;
}
path += fileName;
return true;
}
As you can see it’s ugly. I want to use it with exceptions like so:
public voidFoo(string path, string fileName, ref string error)
{
if (path == null)
{
throw new SomeException("Path is null.");
}
path += fileName;
return true;
}
But we worry about the overhead. Should we?
If the exception is not thrown, then the overhead of your
try...catchis negligable. So, the rule of thumb is:path == nullis a “supported” scenario), use the return value.path == nullusually only happens if the developer using your function makes a mistake, then use an exception.