What Does This Code Do?
“What does this code do?” is often a bit of a gotcha sort of question. OK sometimes it is a simple thing to make sure a student understands the syntax but many times it is really asking if the student understands some underlying concepts of either the programming language or how computers work. Rob Miles asked his students a question like that recently and posted it in his blog. The C# code was:
short i;
for (i = 0; i < 10; i--)
{
Console.WriteLine("i is: " + i);
}
Console.WriteLine("i is: " + i);
Well being the Visual Basic fan that I am I decided to write the VB version. Not so easy as it turns out. You can try a For loop and get something like this:
Dim i As Short = 0
For i = 0 To 10 Step -1
Console.WriteLine("i is: " + i.ToString())
Next i
And it looks the same but VB is too smart to run that loop. The C# for loop in this case is really as much a While loop as what I like to think about as a For loop. So you can use a loop like this one and get the same basic result as the C# code with one exception.
Dim i As Short = 0
While i < 10
i -= 1
Console.WriteLine("i is: " + i.ToString())
End While
Yep, Visual Basic throws an exception – overflow!
So now you get a really interesting discussion. The bonus question is which way of handling the overflow is “better.” Is it a good idea to take advantage of things like C# (and I believe C, C++ and Java) allowing the wrap around to happen and treating it as if that were ok? Or is it better to throw an exception on an overflow so that the program knows something is up?
Sure the C-family way lets you do things that are cute and tricky but is that a good thing or a bad thing? How does it impact maintainability, supportability and future development? Does throwing an exception add protection or just get in the way? Feel free to discuss both sides here. Or bring it up in class and let me know what that discussion goes like. I’d be interested to here about it.
Comments
Anonymous
October 22, 2008
I guess this is a case of conflicting defaults: VB defaults to overflow checking, but the compiler allows you to turn it off (project props/compile/advanced compile options). C# allows you to explicitly specify the behavior you want (see checked/unchecked keywords), but if you don't it will listen to the /checked compiler option (which is off by default). -- HenkkAnonymous
October 22, 2008
Good/bad is NOT the point. But expected behavior. And that's different from language to language. You have to learn the characteristics of each language, NEVER guess from what you know from others.Anonymous
October 23, 2008
It's a very difficult question cause no one has the same answer.. it's the proof :-) Thomas is saying "just learn correctly the language and you'll never have problem". I fully disagree this point of view. Of course in-depth knowledge of the language is a starting point for every serious developer, but this "falsy logical" point of view will lead to avoid strongly typed languages for example. After all, if you exactly know what you do you don't need a strongly typed language ... So I understand Thomas point of view but it is not a good reason to explain why C# is allowing overflow without any exception by default. So my opinion is, on this point, C# is on the wrong way. We want and we need not ony strongly typed languages but also strongly "controled" languages. I dislike VB, every version since the first till the most recent one, but I must admit that on this point VB behaves better because it does what a moder language is supposed to do : strong controls and throwing exception when needed. When Anders created C# from Delphi he took the best ideas of the later (properties for example) but the C temptation was certainly too strong and he added some C/C++ features to C# not in Delphi, and most often it has been a mistake. Apart from this, I was a Delphi fan, and I'm a C# fan, and I'll will not switch to VB :-)