Home > Uncategorized > The fundamental problem

The fundamental problem

I was browsing an analysis of the Zune 30’s 31 December 2008 bug, and I found myself wondering where it was going. See, it’s a thorough treatment of the subject that goes in to exactly what code failed and why, and then begins to re-write the C code.

The problem (well, my problem) is that the re-writer is a C programmer. Great for writing C code, but not for writing blog posts. Because the C programmer proceeds to write a version of the code that fixes the bug and is rather long, a bit verbose, and rather easy to read.

while (days > 365)
    {
        if (IsLeapYear(year))
        {
            if (days > 366)
            {
                days -= 366;
                year += 1;
            }
            else
            {
                break;
            }
        }
        else
        {
            days -= 365;
            year += 1;
        }
    }

And then the C programmer does what C programmers like to do, and starts compressing. Trinary operators are involved.

int daysThisYear = (IsLeapYear(year) ? 366 : 365);
    //The number of days in the current year is now calculated instead. 
    while (days > daysThisYear)
    {
        days -= daysThisYear;
        year += 1;
        daysThisYear = (IsLeapYear(year) ? 366 : 365);
    }

Much more compact in lines of code, but also much more difficult to read, in my humble, Python-loving, Java-using mind.

And as the author/programmer began to trim the code down, I wanted to just say “Stop! This is unnecessary!” but it was too late. It had already been done. But, it seems to me, that code like the more terse example is more error prone, or at the very least, harder to troubleshoot. And I just don’t think that way.

Maybe that’s my problem with C. Just as I would never think that distilling the code the way that that blog post did would be a good idea, I never think that other “features” of C are good ideas to use. Maybe I just don’t get it.

Advertisements
Categories: Uncategorized
  1. January 5, 2009 at 2:16 am

    Sorry about that! 😦 I’m actually far more fluent at Java, but these are pretty basic concepts which also persist throughout Java, so I figured applying them here and checking against C programmers in my range of friends might be the safest route.

    I never liked C to begin with, so you’re not alone. I was just trying to make the code slightly more efficient for use on a mobile platform, hence “extra credit.”

    Thanks for reading!

  1. January 7, 2009 at 6:28 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: