3 ms·
public static UInt32 CheckNextVersion([...]) { Contract.Requires(previousVersions != null); Contract.Requires(versionString != null); UInt32 vers
by danbruc 11y ago
public static UInt32 CheckNextVersion([...])
{
Contract.Requires(previousVersions != null);
Contract.Requires(versionString != null);
UInt32 version;
if (!UInt32.TryParse(versionString, out version)) throw new ParseException();
var min = previousVersions.Cast<UInt32?>().Min();
if (min == 0) throw new NewReleaseImpossibleException();
if (version >= min) throw new InvalidVersionException();
return version;
}
I still wouldn't use this code in production but all the exception handling in the article is completely unnecessary and even throwing those exceptions in the error cases is a design decision. You could as well create your own Result<T> type either containing a value of type T or an error message and return that. Obviously the more support by the language and the compiler you get the better, but you can still do it even without.
public static Result<UInt32> CheckNextVersion([...])
{
Contract.Requires(previousVersions != null);
Contract.Requires(versionString != null);
UInt32 version;
if (!UInt32.TryParse(versionString, out version))
{
return Result.Error<UInt32>("Bad version string.");
}
else
{
var minimumPreviousVersion = previousVersions.Minimum();
if (minimumPreviousVersion == 0)
{
return Result.Error<UInt32>("New release impossible.");
}
else if (version >= minimumPreviousVersion)
{
return Result.Error<UInt32>("Invalid version.");
}
else
{
return Result.Success(version);
}
}