Ayende @ Rahien

Unnatural acts on source code

Riddle me this: String outputs

What would be the result of this?

public string Wrap(string x, string y)
{
   return "[" + x ?? y + "]";
}

Can you see the bug?

Comments

Henning Anderssen
05/06/2010 09:11 AM by
Henning Anderssen

Both x and y can be null, which means the output will be "[]"?

FallenGameR
05/06/2010 09:12 AM by
FallenGameR

I'll receive an exception when both x and y are null.

James Gregory
05/06/2010 09:14 AM by
James Gregory

You'd end up with either "[" or "[value-of-x" depending on if x is null.

Mike
05/06/2010 09:15 AM by
Mike

the "[" + x operation will be processed before the ?? operation?

Rytmis
05/06/2010 09:15 AM by
Rytmis

I'm guessing operator precedence. It should be "[" + (x ?? y) + "]".

eti
05/06/2010 09:16 AM by
eti

Nice one.

First guess is that the operator + has a higher priority than the operator ?? and ("[" +x) is always != null and the second part is never used.

Philipp
05/06/2010 09:16 AM by
Philipp

I'd say you'd probably want to put the 'x ?? y' into parantheses. The first expression is never null.

Paco
05/06/2010 09:17 AM by
Paco

Operator precedence? It won't wrap but prefix and always ignore y.

Dave
05/06/2010 09:41 AM by
Dave

That why I use String.Format:

public string Wrap(string x, string y)

{

return string.Format("[{0}]", x ?? y);

}

julian jelfs
05/06/2010 09:46 AM by
julian jelfs

I think that's the first one I've been able to get!

Marc Gravell
05/06/2010 09:56 AM by
Marc Gravell

@Chris - because 7.1 is the .NET 1.1 documentation. In the current spec this table is 7.3.1, and it is very low in the table; almost anything trumps null-coalescing.

Peter Zsoldos
05/06/2010 11:05 AM by
Peter Zsoldos

+1 for operator precedence

Davy Landman
05/06/2010 11:18 AM by
Davy Landman

isn't there an extra problem, what if y is also null?

public string Wrap(string x, string y)

{

return "[" + (x ?? y) + "]";

}

could cause a bug when both x and y are null :)

Felipe Fujiy
05/06/2010 12:03 PM by
Felipe Fujiy

Its the same that:

public string Wrap(string x, string y)

{

return ("[" + x) ?? (y + "]");

}

Refactoring:

public string Wrap(string x, string y)

{

return "[" + x;

}

Jason Slocomb
05/06/2010 03:13 PM by
Jason Slocomb

?? will never fail the null check because each side of the operation has a bracket concatenated to it.

Diogo Menezes
05/06/2010 03:30 PM by
Diogo Menezes

All the code after x will be ignored !

The output should be "[x"

Phil
05/06/2010 03:38 PM by
Phil

The code will throw an exception if both x and y are null (you cannot add a string to null). Otherwise, it will be x's value (if not null, y's value otherwise) surrounded by '[' and ']'. Nice bug and not something easily caught if you have a thousand other line to write ASAP.

Jason Y
05/06/2010 04:57 PM by
Jason Y

My guess is what James Gregory said--operator precedence bug.

Frank Quednau
05/06/2010 06:14 PM by
Frank Quednau

Has got a tiny bit of "Ouch", though...

Nick Berardi
05/06/2010 06:51 PM by
Nick Berardi

It follows the phrase that my Math teacher taught me in 9th grade for determining order or operations.

Please Excuse My Dear Aunt Sally

Parens

Exponentials

Multiplication

Division

Addition

Subtraction

Then everything else.

So it would be

if x is null it is

"["

if y is null it is

"[x"

if both are null it is

"["

Phil
05/06/2010 07:52 PM by
Phil

I made a mistake, my answer is wrong.

zvolkov
05/06/2010 09:14 PM by
zvolkov

Yeah I had this one in production. I was really surprised, I think it's pretty intuitive to expect ?? being the highest priority...

Comments have been closed on this topic.