What's a concise way of returning true
or false
along with an error message?
Here's a concrete example - I'm building a parser that takes in a file. ParseReplayData
does some validation before proceeding with the actual parsing:
public class Parser
{
private const string SUPPORTED_FILE_EXTENSION = "w3g";
private const string ERROR_FILE_DOES_NOT_EXIST = "Replay file does not exist: {0}";
private const string ERROR_FILE_EXTENSION_NOT_SUPPORTED = "File extension is not a w3g replay file: {0}";
private const string ERROR_FILE_BYTE_LENGTH_TOO_SMALL = "Replay file's byte length is too small: {0}";
private const int REPLAY_MINIMUM_BYTE_LENGTH = 288;
private ReplayData _replayData;
private readonly string _replayFilePath;
public ReplayData ReplayData
{
get { return _replayData; }
}
public Parser(string replayFilePath)
{
_replayFilePath = replayFilePath;
}
public bool ParseReplayData(out string errorReason)
{
errorReason = String.Empty;
if (!File.Exists(_replayFilePath))
{
errorReason = String.Format(ERROR_FILE_DOES_NOT_EXIST, _replayFilePath);
return false;
}
if (Path.GetExtension(_replayFilePath) != SUPPORTED_FILE_EXTENSION)
{
errorReason = String.Format(ERROR_FILE_EXTENSION_NOT_SUPPORTED, _replayFilePath);
return false;
}
byte[] replayFileBytes = File.ReadAllBytes(_replayFilePath);
if (replayFileBytes.Length < REPLAY_MINIMUM_BYTE_LENGTH)
{
errorReason = String.Format(ERROR_FILE_BYTE_LENGTH_TOO_SMALL, _replayFilePath);
return false;
}
_replayData = new ReplayData();
..... //Parse Replay
return true;
}
}
I can't shake the feeling that there's a lot of code smell in this class, and I'd appreciate any other suggestions you could give me.
false
? You could also define aResult
class which contains the return-value and the reason. \$\endgroup\$