VB.NET Chapter #1 - The Babysitter from Hell

[I'm no VB enthusiast, so pardon me if this post is a bit biased.]

When I started out programming, a long time ago, the motivation was utter control. I wanted to master the computer, see how far I could get and how much I could get a away with. It was all about circumventing any obstacle in my path. I’ve done a lot of bad things. Some on purpose, some by pure ignorance, some on pure luck. And usually I learn something from it (moral, ugh).

Anyway, any language or platform has built-in protection to stop developers from making stupid mistakes. In my opinion it’s usually a good balance between what you can do, and what you cannot. But today I saw something plain stupid - in VB.NET. I was working on this hashing function. A simple function returning this seemingly random number. To accomplish my goal I needed to use the left shift operator (<<), an operator I’ve use extensively over the years. Not in VB.NET, but in other languages like C, C++, C# and Java. So, I wrote the function, tested my application and it didn’t work. I debugged my application and saw some strange results coming from my hashing function. I added breakpoints, reordered my code, stepped through the code and pulled my hair until I finally accepted this as a bug in VB.NET and told myself that VB.NET is just plain stupid.

Then I opened the documenation (MSDN):

Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. In an arithmetic right shift, the bits shifted beyond the rightmost bit position are discarded, and the leftmost (sign) bit is propagated into the bit positions vacated at the left.

Ok, ok, ok… but wait:

To prevent shifting by more bits than the result can hold, Visual Basic masks the value of amount with a size mask corresponding to the data type of pattern.

What was that? “To prevent”? If I want to shift all 8 bits in a byte, then no one is in the position to “prevent”. I’m not only prevented, I’m prevented silently. Actually what happens is that if I shift more to the left (or the right) than the resulting type can hold, then the whole operation is just silently ignored.

Give me a warning, compile-time error or an exception, but not a shift operation that render itself useless and becomes a NOP.

Just plain stupid.

One Response to “VB.NET Chapter #1 - The Babysitter from Hell”

  1. Vetle Says:

    VB.NET?! You should get a _real_ job. ;)
    http://opera.com/jobs/

Leave a comment...

Powered by WordPress. Entries (RSS) and Comments (RSS).