Thursday, May 12, 2005

Lesson for the day - No matter how fancy-dancy your programming language, it still must insterface with the real world if it is going to be of any use to anyone. That is when all the nice little abstractions start falling apart. Joel Spolsky calls this "The Law of Leaky Abstractions."

My developers love C#. They want to do everything in C# because they see it as so much easier than C or C++. Case in point - networking. C# has all these nice networking classes NetworkStreams, BinaryReader, etc. that make it soooo easy to create little networking applications. The problem is, as advanced as C# is, it cannot ignore the reality that is network programming. Things break down, packets are lost, there are ghosts in the machine.

In particular, just because you send 1024 bytes using

writer.Write(buff, 0, length);

it doesn't mean that

reader.Read(buff, 0, length);

on the other end is going to always give you 1024 bytes in one go. BinaryReader::Read has a return value that you cannot ignore - it is the actual amount of data it was able to read. Basic stuff, but we tend to forget the basic stuff when we think our programming lanuage is so much easier to use. I fell into the trap myself, and spent an hour trying to help debug the above networking code, assuming that C# was just taking care of all the little niggling bits. At one point, I was convinced the bug was in the BinaryReader class. New languages don't eliminate the need to remember the most basic cenventions, such as: Don't ignore return values!

0 Comments:

Post a Comment

<< Home