Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
« You Can't Beat a bit of Rehearsal | Main | Lousy Software »
Monday
Dec082008

Programming Pop Quiz

These two snippets of code are supposed to advance a pointer along a buffer until they find a double quote character or reach the end of the buffer.

while (input[startPos] != '"' && startPos < input.Length) startPos++;

or

while (startPos < input.Length && input[startPos] != '"') startPos++;

One of them works fine. One throws an exception sometimes. Why?

Reader Comments (8)

You have to check that the start position is not null before you can check the length. Because if it is null the length can't be calculated so "while (input[startPos] != '"' && startPos < input.Length) startPos++;" would work and the other would not.
December 11, 2008 | Unregistered CommenterItDoesntMatterWhatMyNameIS
The second *might* work fine. I am saying might because I am just being picky :-) for the fact that startPos is allowed to be < 0 which will cause an ArgumentOutOfRangeException or similar.

The first is simply broken you are accessing the indexer before validating the position first. Assuming that startPos is valid to start with for the last iteration of the while loop where no " has been found and you are at the last position in input being:

startPos == input.Lenth - 1

you would increment and access an invalid index +1 after the upper bounds of the input array when reentering the while loop which will throw an exception.

What do I win?
December 11, 2008 | Unregistered CommenterIvan
No I was first.
December 11, 2008 | Unregistered CommenterItDoesntMatterWhatMyNameIS
Clearly being 5 hours behind you all is a huge disadvantage in terms of getting the answer in first. Of course it was obvious from the start. A related issue is why does Visual Basic have both And and AndAlso ?
December 11, 2008 | Unregistered CommenterAlfred Thompson
I would think a possibility of null, and the first one could be the one that throws an error on occasion....though I am not positive at all.

@Alfred - http://panopticoncentral.net/articles/919.aspx

I almost think of the AndAlso as a nested If....if that makes any sense, :)
December 12, 2008 | Unregistered CommenterShawn
Hmmm. Far too easy. The first answer came in just a few minutes after I posted the question.

For the record, I really don't like code like this. It might be the most efficient way to do it, but you should not write code that relies on a language quirk that every reader might not know. My code would be a bit longer, but would be much clearer.
December 12, 2008 | Registered CommenterRob
At the risk of being daft...what is the answer then, and what do you mean a language quirk?
December 12, 2008 | Unregistered CommenterShawn
Dear Shawn,
The first piece of code says that while the first character of the input is not null and the start position is less than the length of the string, then increment the start position.

The Second piece of code says that while the start position is less than the length of the string and the first character of the input is not null, then increment the start position.

However if the string is null, then when you ask for the length of the string it will throw an exception because it doesn't have a length. The first piece of code would break out of the loop before it asked for the length, whereas the second piece of code asks for the length first and throws the exception.

The reason this is a language quirk is to do with the way the compiler works. Really what it should do is check both logical statements and crash both times, and the correct code should require an if statement before the while loop:

if (input[startPos] != '"') {
while (startPos < input.Length) startPos++;
}

The reason that rob does this is because when you compile this it makes the code run faster as there isn't an extra if in the code.
December 13, 2008 | Unregistered CommenterItDoesntMatterWhatMyNameIS

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.