CodeSOD: A Type of HPC

This post was originally published on this site

The Daily WTF

Matteo‘s company hired a Highly Paid Consultant. The HPC came in, took one look at their C# codebase, and said, “You’re doing everything wrong. But don’t worry, I can fix it!”

So he “fixed” it.

For example, what if you had a string, and needed to parse it to some concrete type. Why, you just do this:

public static T ToType<T>(this string value, T defaultValue) { if (string.IsNullOrEmpty(value)) { return defaultValue; } T result = defaultValue; try { result = (T) Convert.ChangeType(value, typeof (T)); } catch { } return result; }

This particular code doesn’t do very much at all- a basic null check followed by an attempt to convert, a swallowed exception and a default value output. There are built-in methods, like Int32.TryParse, but that wouldn’t work well with using generics. I don’t love it, but it’s not WTF.

public static T? ToNullable<T>(this string value) where T :

To read the full article click on the 'post' link at the top.