Shared posts

03 Jan 16:47

CodeSOD: A Type of HPC

by Remy Porter

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 : struct
{
        if (string.IsNullOrEmpty(value))
        {
                return null;
        }

        T? result = null;

        try
        {
                result = (T) Convert.ChangeType(value, typeof (T));
        }
        catch
        {
        }

        return result;
}

It's the same method, but this time where the default value is a nulled nullable. The real bonus here is that this time, they qualified the generic to require it to be a struct- which in C# terms actually requires it to be a "value type". Which, as an aside, Convert.ChangeType requires its type parameter to be a value type- which means for consistency, they probably should have put that qualifier on ToType.

Again, this code, alone, isn't really a full on WTF. So we need to put it into context: this is the Highly Paid Consultant's home-grown JSON library, which handles both parsing and generating JSON.

Now, we're getting the sense that someone's been reinventing the wheel, awkwardly. So let's look at one last method- ToJS. What do you think ToJS does? If you said, "clearly, this must be what converts an object to JSON", you'd be wrong.

public static string ToJS(this string text)
{
        return text.Replace(@"\", @"\\")
                .Replace(@"'", @"\'")
                .Replace("\"", "\\\"");
}

This just escapes your strings for you.

There's one last piece of context: all of the code this developer wrote, every method that could throw an exception had a handy, dandy, exception handler. That handler logged the exception and did nothing else.

Like a damp fart, the HPC hung around the office for awhile, and then slowly dissipated to another contract. The stench, however, is going to take Matteo some scrubbing to remove.

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.
23 May 05:13

Milieu has a clean West Loop slate to begin construction

by danieldschell

Milieu West Loop

Demolition is complete at the corner of Adams and Peoria Streets in the West Loop, leaving just a silky-smooth lot with which to begin Milieu, the 275-unit apartment project from co-developers White Oak Realty Partners and Crayton Advisors.

It looks like Stalworth Underground will hit the site first, as they’re equipment is already on the lot. They’ll help take care of foundation work for GC Power Construction, tasked with sending Milieu up to its desired 19-story height. Designed by the West Loop’s own FitzGerald, Milieu brings 13,000 square fete of retail space, as well as parking spots for nearly 200 cars.

For now, Power has a permit to build up to the 5th floor. Soon they’ll get a tower crane permit as well, then the full-build will follow. Milieu is expecting a Summer 2019 opening.

17 Oct 23:46

3:16 All scripture is given by a procedural argument to instantiate.

3:16 All scripture is given by a procedural argument to instantiate.

04 Feb 06:54

Who’s Tracking Your Smartphone?

by Camila Souza

We use our smartphones constantly for daily activities like chatting with friends, banking and shopping, and even monitoring our health. There’s an app for everything you can think of.  If you’re like nearly 80% of smartphone users, you have your phone with you nearly 24/7. One in every five people in the world own a smartphone.

Since our phones are always by our sides, they reveal a lot of information about us. We’ve heard stories about hackers and even government agencies being able to access smartphones for personal information, so maybe it’s wise to keep it safe.

There are ways to protect that wealth of information; check out this infographic and find out how:
Who

Who’s Tracking Your Smartphone? [Infographic] by the team at Quality Nonsense Ltd