patterncsharpMinor
just making sure this is efficient, for the null checks
Viewed 0 times
thisthechecksjustnullefficientsureformaking
Problem
This is inside a method (basically its just this), I try to get the neededValue otherwise I pass back what was passed in...
private static string GetNeededValue(string param)
{
string tValue = "";
if (!string.IsNullOrEmpty(param))
{
var map = repo.Item(string.Format("CoverageName LIKE '{0}'",param));
tValue = map == null ? param : map.theNeededValue;
}
return tValue;
}Solution
Yes, it seems fine from an efficiency point of view. Here's a couple of simple alternatives you could consider.
Remove the need for the ! condition. I quite like this option as I think it flows nicely and is easy to read.
Or another alternative might be to be more explicit about when tValue is set.
Remove the need for the ! condition. I quite like this option as I think it flows nicely and is easy to read.
private static string GetNeededValue(string param)
{
// do we care about whitespace. If so you could use IsNullOrWhiteSpace
if(string.IsNullOrEmpty(param)) return param;
var map = repo.Item(string.Format("CoverageName LIKE '{0}'",param));
return map == null ? param : map.theNeededValue;
}Or another alternative might be to be more explicit about when tValue is set.
private static string GetNeededValue(string param)
{
var tValue = param;
// do we care about whitespace. If so you could use IsNullOrWhiteSpace
if(!string.IsNullOrEmpty(tValue))
var map = repo.Item(string.Format("CoverageName LIKE '{0}'",tValue));
if(map != null)
tValue = map.theNeededValue;
}
return tValue;
}Code Snippets
private static string GetNeededValue(string param)
{
// do we care about whitespace. If so you could use IsNullOrWhiteSpace
if(string.IsNullOrEmpty(param)) return param;
var map = repo.Item(string.Format("CoverageName LIKE '{0}'",param));
return map == null ? param : map.theNeededValue;
}private static string GetNeededValue(string param)
{
var tValue = param;
// do we care about whitespace. If so you could use IsNullOrWhiteSpace
if(!string.IsNullOrEmpty(tValue))
var map = repo.Item(string.Format("CoverageName LIKE '{0}'",tValue));
if(map != null)
tValue = map.theNeededValue;
}
return tValue;
}Context
StackExchange Code Review Q#24055, answer score: 5
Revisions (0)
No revisions yet.