API's that Suck

March 20, 2009

System.DateTime – Introducing the Ninja-bit

Filed under: Uncategorized — Grauenwolf @ 10:09 pm

Start with three variables, all are System.DateTime.

a: 10/2/2009 2:30:00 PM
b: 10/2/2009 2:30:00 PM
c: 10/2/2009 2:30:00 PM

Compare them to each other.

a=b: True
b=c: True
c=a: True

Ok, we have established that all three dates are equal. So when we convert them all to Universal time, we will get the same result. Right?

a.ToUniversalTime: 10/2/2009 9:30:00 PM
b.ToUniversalTime: 10/2/2009 9:30:00 PM
c.ToUniversalTime: 10/2/2009 2:30:00 PM

Wrong.

In .NET 2, System.DateTime was subtly and yet radically changed. Rather than adding full support for time zones, support for local and UTC time was hacked into two unused bits in the underlying data structure. These bits are the date’s “Kind” property.

a.Kind: Unspecified
b.Kind: Local
c.Kind: Utc

Why does this matter? Well, when you serialize a DateTime the Framework generally uses something like this:

a: 2009-10-02T14:30:00.0000000
b: 2009-10-02T14:30:00.0000000-07:00
c: 2009-10-02T14:30:00.0000000Z

So when your counter-party reads it back in, there are going to see two, maybe three, very different dates. And in case you are wondering, this did happen to me in a production scenario.

Advertisements

Sun still doesn’t understand browser plug-in development

Filed under: Uncategorized — Grauenwolf @ 8:59 pm

The basic rules for browser plug-in development are simple. Plug-ins should be:

  • Lightweight
  • Fast
  • Secure

I’m no security expert, so I’m not going to talk about the last point. Bur for the first two, there is a really easy objective test. All you have to do is ask the companies what they think of their product.

Windows

  Flash Silverlight JavaFX
CPU Intel® Pentium® II 450MHz, AMD Athlon™ 600MHz or faster processor X86 or x64 500-megahertz (MHz) or higher processor Intel Pentium 4, Intel Centrino, Intel Xeon, or Intel Core Duo (or compatible) 1.8 GHz minimum
RAM 128MB 128-megabytes (MB) of RAM 512 MB of RAM

Seems to me that asking for three times the CPU speed and four times the RAM as your competitors is an automatic fail in the “lightweight” and “fast” categories.

March 6, 2009

WCF Clients – 1 line to create, 16 to destory

Filed under: Uncategorized — Grauenwolf @ 10:40 pm

What most people write when using WCF.

Using service As New SampleClient
    service.DoSomething()
End Using

The right way according to Microsoft. (Assuming you don’t want unexpected exceptions, lingering connections, and other bad things.)

Dim service As New SampleClient
Try
    service.DoSomething()
Finally
    Try
        If service IsNot Nothing Then
            If service.State <> CommunicationState.Faulted Then
                service.Close()
            Else
                service.Abort()
            End If
        End If
    Catch ex As TimeoutException
        service.Abort()
    Catch ex As CommunicationException
        service.Abort()
    Catch ex As Exception
        service.Abort()
        Throw
    End Try
End Try

Operator Overloads Done Correctly

Filed under: Uncategorized — Grauenwolf @ 9:53 pm

APIs that suck lead to subtle errors or overly complicated code. Here is an example of one such API for checking the OS version.

The wrong way.

bool SupportsTaskProgress()
{
    if (Environment.OSVersion.Version.Major >= 6)
    {
        if (Environment.OSVersion.Version.Minor >= 1)
        {
            return true;
        }
    }
    return false;
}

The “surprisingly hard way”.
bool SupportsTaskProgress()
{
    if (Environment.OSVersion.Version.Major >= 6)
    {
        if (Environment.OSVersion.Version.Major == 6)
        {
            if (Environment.OSVersion.Version.Minor >= 1)
            {
                return true;
            }
            return false;
        }
        return true;
    }
    return false;
}

Fortunately in this case, this isn’t an “API that Sucks”. Rather, the solution is surprisingly simple.
bool SupportsTaskProgress()
{
    return (Environment.OSVersion.Version >= new Version(6,1);
}

That’s it folks. All that nonsense just melts away with the help of a simple operator overload.

Create a free website or blog at WordPress.com.