AnswerWhat does this code do?

time to read 2 min | 225 words

This was a surprising shock, this code seems so simple, but it does something very different than what I would expect.

The question is why?

As it turns out, we are missing one character here:

image

Notice the lack of the comma? Let us see how the compiler is treating this code by breaking it apart into steps, shall we?

First, let us break it into two statements:

Note that we moved the name line into the first statement, since there isn’t a command here. But what is it actually doing? This looks very strange, but that is just because we have the dictionary initializer here. If we drop it, we get:

image

And that make a lot more sense. If we’ll break it all down, we have:

And that explains it all, make perfect sense, and a very nasty trap. We run into it accidently in production code, and it was near impossible to figure out what was going on or why it happened.

More posts in "Answer" series:

  1. (20 Jan 2017) What does this code do?
  2. (16 Aug 2011) Modifying execution approaches
  3. (30 Apr 2011) Stopping the leaks
  4. (24 Dec 2010) This code should never hit production
  5. (21 Dec 2010) Your own ThreadLocal
  6. (11 Feb 2010) Debugging a resource leak
  7. (03 Sep 2009) The lazy loaded inheritance many to one association OR/M conundrum
  8. (04 Sep 2008) Don't stop with the first DSL abstraction
  9. (12 Jun 2008) How many tests?