Ben M recently inherited some code from some Highly Paid Consultants. These consultants needed to be able to set some flags to control the behavior of the application, and for whatever reason, these flags needed to be strings. It probably wasn’t a good reason, but there was some reason. The consultants wrote the module which set the flags, and guaranteed that the flags were only ever To parse those flags back into boolean values, they did it the true Highly Paid Consultant way: they used a generic “string to boolean” solution they copied from Stack Overflow instead of private bool ConvertBoolean(string value, bool defaultValue = false) { bool returnValue = defaultValue; if (value == null) { return returnValue; } if ("y".Equals(value, StringComparison.OrdinalIgnoreCase) || "yes".Equals(value, StringComparison.OrdinalIgnoreCase) || "true".Equals(value, StringComparison.OrdinalIgnoreCase) || "1".Equals(value, StringComparison.OrdinalIgnoreCase) || "on".Equals(value, StringComparison.OrdinalIgnoreCase)) { returnValue = true; } else if ("n".Equals(value, StringComparison.OrdinalIgnoreCase) || "no".Equals(value, StringComparison.OrdinalIgnoreCase) || "false".Equals(value, StringComparison.OrdinalIgnoreCase) || "0".Equals(value, StringComparison.OrdinalIgnoreCase) || "off".Equals(value, StringComparison.OrdinalIgnoreCase)) { returnValue = false; } return returnValue; }
//thedailywtf.com/images/highlight/highlight.pack.js
[Advertisement]
Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!
via Tumblr CodeSOD: Be True to Yourself
0 Comments
Leave a Reply. |
About Us:Dive deep into a topic with Appeared, learning both how and why things work. Come and be a part of our group if you are looking for a collection of articles on software development and computer programming. I am an experienced and enthusiastic computer programmer who prefers high-level languages and simple solutions. Other Profiles:
|