Skip to content

Instantly share code, notes, and snippets.

@jdunne
Last active December 20, 2016 02:21
Show Gist options
  • Save jdunne/3819c4903f3f6239bbeb3a6ae123ac7c to your computer and use it in GitHub Desktop.
Save jdunne/3819c4903f3f6239bbeb3a6ae123ac7c to your computer and use it in GitHub Desktop.
// One solution is to pass a default value into the method.
string uri = configFile.GetValueOrDefault(“serviceUri”,
“http://example.com/service");
ConnectToService(uri);
// Another possibility is to introduce a Maybe type to hold the return value.
Maybe<string> uri = configFile.GetValue(“serviceUri”); 
if(uri.HasValue)
{
ConnectToService(uri.HasValue ?
uri.Value :
“http://example.com/service"); 
}
// If a missing return value is highly unlikely or
// there can be no default handling of a missing value,
// an exception can be thrown.
try
{
  ConnectToService(configFile.GetValue(“serviceUri”)); 
}
catch(MissingConfigurationException exception)
{
  // handle the error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment