Sunday, August 08, 2010

Python vs. C# - Exceptions

I briefly mentioned in an earlier post that I had left Cogi to start working for The Trade Desk. One of the many changes that this entailed was to stop working in Python, my preferred programing language, and start programming in C#. Both languages are quite popular, and each have their own merits: Python's "batteries included" standard library and programmer-centric philosophy allow a programmer to focus on producing software, while C#'s .NET-backed runtime and Microsoft integration give it a great backbone for software development.

As I've spent a good deal of time programming in both languages, it seems like it might be interesting to compare some aspects of the two languages. To avoid overwhelming either myself or my readers, I'll limit the discussion to one feature at a time. Assuming that anyone finds the articles interesting or informative, I will periodically post comparisons between the two languages.

Today's feature: Exceptions.

First off, I'm going to assume that it's a given that exception handling is a Good Thing. The only major alternative that I'm aware of is a combination of return codes and segmentation faults (for the lay-reader: crashes). Return codes mean you have to check the result of any potentially failing method call, making your code focus more on what can go wrong than on what should go right. This leads to fragmented code which leads to more bugs. Exception frameworks allow us to focus on what should happen, and deal with the consequences in alternative blocks of code higher up the stack where we can make more logical decisions. Both Python and C# fall into the exception-oriented camp (for the most part).

Both languages treat exceptions as first class objects and provide a good try/catch/finally mechanism that allows a developer to handle and deal with exceptions of both specific and general types:

def TestMethod():
        print "Oops!"

public void TestMethod()
    catch ( Exception )
        Console.WriteLine( "Oops!" );

Both Python (as of 3.0) and C# also support the idea of a "nested" or "inner" exception. This is a critical component of robust exception handling, as it can be used to ensure that an exception can always be traced back to its source. In my previous jobs, working with both C++ and earlier versions of Python, we implemented and maintained extensive libraries of code just to provide this critical debugging feature.

One area where Python and C# significantly differ from one another is in the detail included in exceptions. In particular, I'm referring to the built-in exceptions in the language libraries, since the language obviously has no control over how a developer chooses to write their code. Consider the following algorithm:

  • Create a dictionary of key-value pairs.
  • Populate the dictionary with some well known or required data.
    • Let's say the keys we're trying to put in are "a", "b", and "c".
  • Get input from somewhere.
    • Let's say we get "c".
  • Use the input to attempt to retrieve a key from the dictionary.
    • The key doesn't exist, so an exception is raised.

"Ok", you think, "that's no big deal. I just have to figure out which key it was that was looked up and figure out why it didn't exist in the dictionary." In Python, you would have no problem, as the exception tells you what you were looking for:

    Traceback (most recent call last):
      File "", line 1, in
    KeyError: 'c'

Given that info, we know that something was wrong with the code populating the dictionary, since the expected key "c" didn't exist. If that KeyError had indicated that "x" was looked up, we would know that the input we were given was bad, since we didn't expect "x" to exist. From there, it's just a matter of tracing down the code that caused the problem.

On the other hand, in C# all you're given is that the data wasn't found:

    Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at ExceptionExample.Program.Main(String[] args) in     C:\Users\chris\AppData\Local\Temporary Projects\ExceptionExample\Program.cs:line 13

Well, what do we do with that? At this point, we have to start debugging manually -- either by adding some print statements or by setting a breakpoint at the indicated line. Remember that in Python, we already had a starting place -- we knew in which direction we had to look for the bug, and possibly could have solved the problem without ever starting a debugger. In C#, we can't even begin to investigate the problem without a debugger because we weren't given enough information to start with -- information that was obviously available at the time the exception was thrown!

And it's not just KeyNotFoundException that has this problem; it's nearly every exception I've seen thrown by .NET that could contain information about the problem that caused it. Another example is the dreaded NullReferenceException. Where C# forces you to drop into the code and hope the line numbers line up (and let's hope you're not trying to use reflection -- but that's a Python vs. C# topic all on its own!), Python handles its equivalent beautifully:

    >>> a = None
    >>> a.hi
    Traceback (most recent call last):
      File "", line 1, in
    AttributeError: 'NoneType' object has no attribute 'hi'

Winner: Python.

Monday, July 19, 2010

Moblie Device Antennas

I'm neither a mobile device expert nor a wireless antenna expert, however I'm getting a bit tired of this iPhone 4 attenuation issue and am now ready to weigh in. In particular, I'm quite annoyed with Apple's dodge of the issue. Yes, all antennas have attenuation issues when they come into contact with a conductor (like a person's hand). However, despite all the manuals that have discussed this and the many phones that have had this issue in the past, it's never become a big deal until the iPhone 4.

The reason it's different in this case is Apple's innovative design that places the entire antenna on the outside edge of the phone.

I don't personally mind that they did this in terms of design -- Apple is great at design, everyone knows that. However, in this case the design bit them in the metaphorical ass and they're not willing to own up to it. Pointing out that all phones have the problem just shifts the blame; it reminds me of a mother's saying "If everyone else jumped off a bridge, would you?"

Now Apple has managed to get all of their fan boys parroting out their phrases and trying to catch everyone else -- as if other people claimed that there were no attenuation issues in other phones.

I don't think Apple necessarily should have had a full recall for the phone -- but then, I didn't drop $500 on it either. I don't think the free bumpers were a "bad" solution, just the way the presented them. The honest response to the problem would have been to say: "The phone has a problem which will be addressed in the next revision of the phone. We feel that the best response for our customers and stockholders is to give out the rubber Apple bumpers or a full refund for unsatisfied customers." It's essentially exactly the same conclusion without the dishonest misdirection that has resulted from their response.

Saturday, June 12, 2010

Waking up from the dream

Yesterday afternoon I woke up from a dream I've been trapped in for ten long years. In this dream I would look across the room at a clock and not be able to tell what time it was or look at a TV and not be able to make sense of what I was seeing. That has all changed.

This dream, of course, is my poor eyesight and reliance on glasses to be able to read anything further than a foot away from me. I started wearing glasses in high school ( I think during my junior or senior year ) when Madeline, Katrina, and Stephanie kept harassing me about my squinting to read what was on the board in class. Since then, I've been wearing glasses or contacts every day.

A few months ago, I finally decided that I had had enough of it. I was tired of having to put on glasses first thing in the morning, just to be able to see myself clearly in the mirror; of having to clean my glasses every time I accidentally touched them and left a smudge; of worrying about losing or breaking my glasses and not being able to function until I got replacements. So, I decided to start looking into LASIK surgery.

So I did some reading and discovered that LASIK is expensive but not prohibitively so. I scheduled an appointment with my optometrist for a general check up and consultation about LASIK. He indicated that I was an "ideal candidate" for LASIK and recommended me to an ophthalmologic surgeon named Dr. Winthrop in Santa Barbara.

A few days later I went in to see Dr. Winthrop who confirmed that my eyes were perfect for the operation and outlined the procedure for me. I decided to have the procedure done and we agreed on a date one week in the future.

Yesterday was that day.

Since Dr. Winthrop's office is near Megan's office, the morning of the procedure I went into work with her and waited around her office until time for the procedure. At eleven, I went over to Dr. Winthrop's and began. After some standard administrivia, they gave me some Valium and took me into the surgery room.

During the surgery, you are laying on your back, looking up at an array of red and white lights, with one green light in the center. The surgeon tells you that your only job is to look at that green light.

The first part of the procedure is the most uncomfortable -- and the main reason they give Valium to their patients -- the surgeon uses a suction device to hold the eye in place and cuts a small "flap" in the cornea. Since the eye has been anesthetized it doesn't really hurt but it's a bit unnerving and uncomfortable. However, 15 to 20 seconds later that part is done.

The next part is simple for the patient -- and actually kind of fun. The surgeon pulls back the flap he's just created and the lights you're looking at become really blurry. Then, for the next 10 seconds or so, the laser does it's work -- you can't feel anything, but your eye is being reshaped. Then the surgeon puts the flap back and makes sure it's clean and smooth.

Then repeat for the next eye. The whole procedure takes less than 10 minutes.

Afterwards, I was carefully walked back into a dim room with a comfortable chair to await the doctor. The nurse gave me some Vicodin to help me relax and brought my wife  in to sit with me. After a few minutes the doctor came in, took a look at my eyes, gave me some eyedrops, and sent me home with instructions to keep my eyes closed and try to sleep for the next four hours.

So Megan took me home and I slept.

When she came back home a few hours later, I opened my eyes and could see almost completely normally again.

Today I drove myself into the doctor's office for a follow up exam without glasses and was quickly told that I have at least 20/20 vision again.

The metaphor of a dream I used to start this post may seem a little over-poetic but it's apt. That same feeling of a dream being hard to remember after you've been awake for a few minutes is about how I feel now -- my eyes see so normally now that it's almost hard to remember them being any other way.

Friday, May 28, 2010

Three hours of debugging...

...for nothing.

My professor sent me an email this evening saying he had managed to get the application I'm writing for him running without much trouble but many of the important effects were missing. He only uses Ubuntu and I've been working in Windows, so I wasn't terribly surprised and resigned myself to installing Ubuntu somewhere and testing out the app in that environment.

I've had some trouble getting Ubuntu installed on this computer in the past, so I decided to try the Wubi Installer to install Ubuntu this time. Immediately after downloading it and trying to run the executable I'm presented with errors that seem to indicate that my hard drive is missing... and dismissing the dialog just causes another one to pop up. Eventually, after several attempts and a little web crawling, I discovered that the repeating dialogs would come to an end if I dismissed them enough. Then the installer seemed to do the right thing. It went on it's merry little way until it came time to restart the computer.

I restarted the computer... and it didn't come back up.

Not only did it not boot into Ubuntu like it was supposed to, it didn't even give me a chance to boot into Windows. Sigh.

My first thought at this point was to be sure I could save my Windows installation, so I grabbed my Windows 7 boot disk and rebooted into the recovery tools on it. Since my computer was out to lunch, I did a little googling on my phone to figure out how to restore the boot loader and restored the default Windows bootloader.

I figured at this point my erstwhile Ubuntu install was lost, since I wasn't using the Grub bootloader anymore, but to my surprise, when I rebooted again I was given options for both Windows and Ubuntu. After a quick boot into Windows to make sure everything was working well, I rebooted again, and this time selected Ubuntu.

Ubuntu finished up it's install and dropped me on a nice, clean, purple desktop. A somewhat irritating graphical glitch in the upper left portion of the screen, but it was liveable.

A few minutes later, I had installed the necessary tools in the environment and was ready to dig into the problem. I got all the dependencies installed and launched my application. Sure enough, the problem Ben was having showed up right away. At least it was being cooperative with me.

My first thought was that something I was drawing wasn't being drawn in the proper place -- it wouldn't be surprising since I hadn't done anything special to control it. So I started down that line of investigation only to discover that my objects weren't even attempting to be drawn. That's strange...

Oh... the SVG file I'm looking into doesn't seem to have my expected elements in it... am I generating it correctly?


Oh, but Graphviz isn't writing the file correctly... strange, since it worked just fine in Windows. A few quick tests and I proved to myself that Graphviz had a giant bug that was preventing my code from naming elements properly. Ugh.

I started searching around the internet but couldn't find any reference to the bug. Out of options, I decided to uninstall Ubuntu's version of the package and install it from source instead. Ten minutes later I had the package re-installed from source and ran my application again -- and it worked perfectly!

So, the current version of Graphviz distributed with Ubuntu's has a bug introduced that prevents the "id" attribute from properly naming nodes, at least with the SVG output format. I'm not sure why no one else had run into that problem but it's fairly significant for anyone that's using that attribute, since they're probably using it for a reason.

All that to say, I just spent 3 hours from starting to investigate the problem by installing Ubuntu until tracking down the problem, only to find that it wasn't a problem with my application at all.

C'est la vie.

Tuesday, May 25, 2010

Graduate Studies at UCSB

As I mentioned in my last post, a week ago I took a big test that encompassed material from the time I've spent earning my graduate degree.

Today I was told that I passed.

Trumpets! Fireworks! Exclamations! After three long years of lectures and homework piled on top of everyday life I'll finally have some semblance of regular free time again! Thank you to everyone for your support, encouragement, and prayers.

(If you're considering working on a post-graduate degree while working full time... don't. Stop working, take out loans, whatever you need to do. Focus on the schoolwork if you're going to do it: you'll get more out of it, enjoy it more, and be done sooner.)

I've actually got about two weeks of "class" left in the quarter, which consists of:

  • One more meeting of my seminar (on Memorial Day)
    • Entails reading one more paper and a short analysis of that paper.
  • Two more meetings for my research project
    • Entails a laundry list of features to be implemented and fixes to be made.
After that, I'm done at UCSB.

My time at UCSB has encompassed a lot of changes in my life. Before being accepted to the graduate program, I was:
  • Unmarried.
  • Living with my college friends in the Jesmary house.
(I couldn't find a Jesmary house group picture... at least this was in the Jesmary house!)
  • Working at CallWave.
  • A Yuk Gup (6th degree student -- green belt) in Soo Bahk Do (the specific type of karate I practice).
  • A "Layreader-in-charge" for my local Anglican parish.
Since then, a lot has happened:

  • I married Megan.
  • We rented a condo in Goleta and bought a condo in Carpinteria.
  • I left CallWave, worked at Cogi for a year and a half, and now work at The Trade Desk.
  • I have advanced to Cho Dan (1st degree black belt) in Soo Bahk Do (at Jang's Karate).
  • I am an average parishioner in my church.
  • I taught a course at Westmont.
  • My sister got married and had a baby. 
I love this picture of Kylie and Melanie!

So now it's onto the next set of challenges. As far as I can foresee, that includes: 
  • Helping to make The Trade Desk massively successful.
  • Spending more time focusing on karate (possibly applying and testing to be a Jo Kyo and then Kyo Sa -- which are ranks of instructor certification).
  • Seriously considering getting LASIK surgery. (I am so tired of wearing glasses.)
Finally, I'll be spending lots of quality time with my wife, who has been so amazingly supportive of me through this experience. 

I love you Megan!

Tuesday, May 18, 2010

The Test

So, do you remember the specific details of things you learned 3 years ago? I ask because that's what I had to do for the comprehensive exam I took this morning out at UCSB as one of the last remaining requirements of my master's degree in computer science.

In the UCSB computer science master's program, you have three "tracks" you can take to earn your degree:

  • Thesis - lots of original research, minimum amount of classwork
  • Project - some original or continued project, average amount of classwork
  • Exam - no research, maximum amount of classwork
Now, don't let it fool you -- even though there's more classwork in the exam, it's my belief that the project and thesis tracks require more work overall, it's just done on your own time, outside of classes. That's why I chose to take the exam track for my degree.

For the exam itself, you (the student) nominate four graduate level classes you took as a part of your studies in the program. The professors of those classes create a "single" question for your exam. When you take the exam, you're given two hours and fifteen minutes to answer three of your four questions (for an average of forty-five minutes per question). At the end of the exam, you turn in your responses and go home and wait.

That's what I'm doing now... waiting.

Over the next week, the faculty that created the questions will grade the responses on a ten point scale and decide if I successfully answered their question or not. (I'm not really sure why it's a "ten point scale" if the professors decide what "passing" is -- it doesn't really "standardize" anything...) Next week, any professors in the department that are interested (apparently it's usually only a handful) will get together and discuss the exam results. Once they're done, the results will be voted on and finalized. After that, I get to find out whether or not I passed (and thus get to graduate this quarter or not).

The four classes I chose were:
  1. Advanced Networking (Fall '07)
  2. Advanced Security (Fall '07)
  3. Program Analysis (Winter '10)
  4. Multimedia Networking (Winter '10)
(Notice the bookend-ish nature of those classes -- those were the first two and last two classes I took at UCSB. I chose those classes mostly because they were my favorites during my time at the university.)

Typically, I don't study all that much for tests -- usually only an hour or two. However, given the importance of this test and the breadth of material it covered, I devoted the bulk of a weekend to studying for this one. I think one of the reasons I typically don't feel the need to study much for a test is that I'm fairly good at predicting what material a test is going to contain and the types of questions that are likely to be asked. Knowing that, it's fairly simple to be confident about a test and not need to study for the test. 

In this case, while I was fairly well prepared, I anticipated the questions on the test much more poorly than usual. Maybe it was because the test was written by four different professors that hadn't just taught the class to me or maybe it was because the professors thought that this test should be particularly difficult -- either way, I misjudged the test content.

I was worried most about the Advanced Networking question going into the test, since that class covered a ton of material. (Seriously, I have like 500 pages worth of papers in my notes from that class!) When I read the question for that class, I sighed and put it down. I hadn't expected the professor to question me about minute nuances of specific implementations of protocols that we talked about in a single class session three years ago. The "single" question consisted of 5 or 6 sub-points discussing the inner workings of various internet protocols, of which I thought I could successfully answer 2 or 3. Ouch.

I next looked at the Advanced Security question... and thought I might succumb to panic. Another multi-part question and I didn't think I could answer the first part ("Describe a return-to-libc attack..."). The second part ("Describe a SQL injection attack...") I knew quite well and I could get through half of the third part ("Describe three ways to attack this SUID program..."). Since I had already given up on the networking question, I didn't really have a choice: I had to attempt this question. So I wrote down my answer for the second and third parts and set the question aside. 

I'm now somewhere around 30 or 35 minutes into the test, so despite having already used up my "pass", I was right about on schedule (since I still had to come back to finish parts of this question).

Next I picked up the Program Analysis question and nearly cheered -- there were 8 parts to the question, covering nearly 90% of the material in the class, but I knew all of them cold. I spent the next 45 minutes writing out my answer to this question in complete confidence.

One more question to go: Multimedia Networking. In hindsight, I should have realized that this was going to be an extremely vague, open ended question that tracked academic thought more than a particular topic or set of facts -- the entire class was like that, so why wouldn't the test question be like that. However, upon seeing the question I wanted to give up and walk out of the room -- it felt completely unfair.

To give myself time to think, I returned to the question for Advanced Security. To my surprise, I now knew the answer to the first part and knew how to finish the third part! Happy days! I quickly jotted down those answers and returned to the Multimedia Networking question and started writing...

...and writing...

...and writing...

...for the next 45 minutes. 

I think I filled 7 or 8 blank pages with writing pounding out a half-thought-out, linear essay describing research issues in the peer-to-peer streaming area and future directions and issues that research in that area will face.

And then I was done, with a couple minutes to spare.

All in all, I'm happy with how the test went. I'm fairly confident that I passed and relieved to be done with it all. Here's hoping that next Tuesday I receive the results and go out to celebrate.

Saturday, May 15, 2010

New Look, Fresh Start

Ok, I'll be the first to admit that when it comes to consistency, this blog has a pretty poor track record. Since its creation, I think it has been idle more than it has been active. But starting today I'm going to try to change all that.

To help kick things off, I've updated the template for the site to something a bit lighter, while still maintaining the same kind of feel.

Why would I even try to start writing again? The most immediate influence is probably Megan's new blog and the handful of thoughtful, well written posts she's put up there over the last couple of weeks. Also, the short time I had to write on the Cogi Engineering blog, which exposed me more directly to writing about software development. (By the way, Dave or Shawn, if you're reading this, it's way past time for you to write something there!) Finally, recently I have increased the scope of blogs that I follow online, and find myself wanting to take a larger role in those communities.

What makes me think that this time will be any different? In the past, I've encountered two major blocks in blogging: what to write and how to write it.

The problem of "how" to write has become much more clear to me lately. In the past, I've tended to write posts as if they should only take me a few minutes and then be immediately posted for the whole world to see. I'm not really sure where I got that idea -- it doesn't match with anything I've ever been taught or my real-world experience with how good writing works. Instead, I'm going to start writing more involved posts that will generally take me more than a single sitting to fully flesh out. The hope is that this style will help maintain my interest enough to produce something that you will want to read. 

There's still the problem of a topic, or focus, for the blog. Megan's Geeky Bliss and Sunshine leaves her with a decent amount of freedom while still focusing her thoughts and intent. Other blogs I read tend to be focused on a theme in software development (.NET, Python, etc.) or other general computing issues. So, what's the niche for

I don't know.

The hardest part is choosing to write things that I think my friends, family, co-workers, and wider community will want to read. There are certainly enough things about which I am passionate enough to write the type of posts I think people will want to read: karate, software development, philosopy, religion. Since I don't know ahead of time which posts will be interesting to readers, I guess I'll just keep writing about things that interest me.

So how about it?

Friday, February 05, 2010


Maybe, just maybe, I'll begin to write here again...

However, to start off, I'm just going to link to the Cogi development blog; my team and I occasionally write entries there related to topics we encounter in software development.

Most recently, I wrote an entry about Python performance with trace messages that other Python developers may find interesting.

Any topics you'd like me to write about, either here or there? Let me know in the comments!