The Daily WTF
Nora found a curious little C# helper method. It was called in exactly one place and it is… not exactly necessary in any case.
/// <summary> /// Return the Filter Index /// </summary> /// <param name=”item”></param> /// <returns></returns> private int GetFilterIndex(bool item) { int index = 0; switch (item) { case true: index = 1; break; default: index = 0; break; } return index; }
This method wants to take a boolean and convert it into an integer- 1 if true, 0 if false. Right off the bat, this method isn’t needed- Convert.ToInt32 would do the job. Well, maybe our developer didn’t know about that method, which isn’t really an excuse, but then they write this logic in the most verbose way possible, favoring a switch over a simpler conditional, doing the dance of “only one return from a method” (which I’m not a fan of as a
To read the full article click on the 'post' link at the top.