Iterating an Enum?
-
Coding horror?
private System.IO.Ports.StopBits stopBitsFromString(string stopBitsAsString)
{
System.IO.Ports.StopBits stopBits = StopBits.None;
foreach (StopBits sb in Enum.GetValues(typeof(StopBits)))
{
if(sb.ToString()== stopBitsAsString)
{
return sb;
}
}
return stopBits;
}Is this better?
private System.IO.Ports.StopBits stopBitsFromString2(string stopBitsAsString)
{
try
{
return (StopBits)Enum.Parse(typeof(StopBits), stopBitsAsString, true);
}
catch {return StopBits.None;}
}JustAStupidGurl
-
Coding horror?
private System.IO.Ports.StopBits stopBitsFromString(string stopBitsAsString)
{
System.IO.Ports.StopBits stopBits = StopBits.None;
foreach (StopBits sb in Enum.GetValues(typeof(StopBits)))
{
if(sb.ToString()== stopBitsAsString)
{
return sb;
}
}
return stopBits;
}Is this better?
private System.IO.Ports.StopBits stopBitsFromString2(string stopBitsAsString)
{
try
{
return (StopBits)Enum.Parse(typeof(StopBits), stopBitsAsString, true);
}
catch {return StopBits.None;}
}JustAStupidGurl
Yes, and yes. But if you do that frequently, better to cache the names and values; may I suggest my EnumTransmogrifier[^]? And in the spirit of the season I won't complain about the multiple
return
statements. :-D -
Yes, and yes. But if you do that frequently, better to cache the names and values; may I suggest my EnumTransmogrifier[^]? And in the spirit of the season I won't complain about the multiple
return
statements. :-DWell I've tried to stay out of that. I think it's a mistake to become obsessive about 'programming rules' in cases where they are of marginal benefit. There is of course a difference between this (the two returns are pretty obvious) and yards of obscure code salted with multiple return statements.
JustAStupidGurl
-
Well I've tried to stay out of that. I think it's a mistake to become obsessive about 'programming rules' in cases where they are of marginal benefit. There is of course a difference between this (the two returns are pretty obvious) and yards of obscure code salted with multiple return statements.
JustAStupidGurl
justastupidgurl wrote:
There is of course a difference between this (the two returns are pretty obvious) and yards of obscure code salted with multiple return statements.
Yes indeed. Definitely easier to take this way.
-
Yes, and yes. But if you do that frequently, better to cache the names and values; may I suggest my EnumTransmogrifier[^]? And in the spirit of the season I won't complain about the multiple
return
statements. :-DYou are right. Building of lookup table improves performance.
static Dictionary<string, StopBits> parsedStopBits = new Dictionary<string, StopBits>(); static ClassName() { foreach (StopBits sb in Enum.GetValues(typeof(StopBits))) { parsedStopBits.Add(sb.ToString(), sb); } } static StopBits stopBitsFromString3(string stopBitsAsString) { StopBits result; if (!parsedStopBits.TryGetValue(stopBitsAsString, out result)) result = StopBits.None; return result; }
Difference between performance of this code and Enum.Parse one is 10 times faster. Difference between perfromance of this code and horror one is 100 times faster. It is not multiple return you should worry about. It's try-catch. If non-enum value name will be passed in the stopBitsFromString2 function, it's performance will be the same as stopBitsFromString's. Knowing when not to use try-catch is priceless. :)
modified on Monday, December 15, 2008 12:32 PM
-
You are right. Building of lookup table improves performance.
static Dictionary<string, StopBits> parsedStopBits = new Dictionary<string, StopBits>(); static ClassName() { foreach (StopBits sb in Enum.GetValues(typeof(StopBits))) { parsedStopBits.Add(sb.ToString(), sb); } } static StopBits stopBitsFromString3(string stopBitsAsString) { StopBits result; if (!parsedStopBits.TryGetValue(stopBitsAsString, out result)) result = StopBits.None; return result; }
Difference between performance of this code and Enum.Parse one is 10 times faster. Difference between perfromance of this code and horror one is 100 times faster. It is not multiple return you should worry about. It's try-catch. If non-enum value name will be passed in the stopBitsFromString2 function, it's performance will be the same as stopBitsFromString's. Knowing when not to use try-catch is priceless. :)
modified on Monday, December 15, 2008 12:32 PM