Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 11 | Comments: 40

filter by tags archive

ChallengeWhat does this code do?

time to read 1 min | 55 words

Without compiling this, can you answer me whatever this piece of code will compile? And if so, what does it do?

var dummyVariable1 = 1;
var dummyVariable2 = 3;
var a = dummyVariable1
+-+-+-+-+ + + + + + +-+-+-+-+-+
dummyVariable2;

Oh, and I want to hear reasons, too.

More posts in "Challenge" series:

  1. (28 Apr 2015) What is the meaning of this change?
  2. (26 Sep 2013) Spot the bug
  3. (27 May 2013) The problem of locking down tasks…
  4. (17 Oct 2011) Minimum number of round trips
  5. (23 Aug 2011) Recent Comments with Future Posts
  6. (02 Aug 2011) Modifying execution approaches
  7. (29 Apr 2011) Stop the leaks
  8. (23 Dec 2010) This code should never hit production
  9. (17 Dec 2010) Your own ThreadLocal
  10. (03 Dec 2010) Querying relative information with RavenDB
  11. (29 Jun 2010) Find the bug
  12. (23 Jun 2010) Dynamically dynamic
  13. (28 Apr 2010) What killed the application?
  14. (19 Mar 2010) What does this code do?
  15. (04 Mar 2010) Robust enumeration over external code
  16. (16 Feb 2010) Premature optimization, and all of that…
  17. (12 Feb 2010) Efficient querying
  18. (10 Feb 2010) Find the resource leak
  19. (21 Oct 2009) Can you spot the bug?
  20. (18 Oct 2009) Why is this wrong?
  21. (17 Oct 2009) Write the check in comment
  22. (15 Sep 2009) NH Prof Exporting Reports
  23. (02 Sep 2009) The lazy loaded inheritance many to one association OR/M conundrum
  24. (01 Sep 2009) Why isn’t select broken?
  25. (06 Aug 2009) Find the bug fixes
  26. (26 May 2009) Find the bug
  27. (14 May 2009) multi threaded test failure
  28. (11 May 2009) The regex that doesn’t match
  29. (24 Mar 2009) probability based selection
  30. (13 Mar 2009) C# Rewriting
  31. (18 Feb 2009) write a self extracting program
  32. (04 Sep 2008) Don't stop with the first DSL abstraction
  33. (02 Aug 2008) What is the problem?
  34. (28 Jul 2008) What does this code do?
  35. (26 Jul 2008) Find the bug fix
  36. (05 Jul 2008) Find the deadlock
  37. (03 Jul 2008) Find the bug
  38. (02 Jul 2008) What is wrong with this code
  39. (05 Jun 2008) why did the tests fail?
  40. (27 May 2008) Striving for better syntax
  41. (13 Apr 2008) calling generics without the generic type
  42. (12 Apr 2008) The directory tree
  43. (24 Mar 2008) Find the version
  44. (21 Jan 2008) Strongly typing weakly typed code
  45. (28 Jun 2007) Windsor Null Object Dependency Facility

Comments

firefly

First I thought I will compile but after I look at the MSDN it said "Variables declared by using var cannot be used in the initialization expression" so the last line probably throw an error.

Dave

I had to cheat and fire up the compiler :-\ (feel free to delete this if I'm spoiling the surprise). I guessed no as I didn't realise that + could be a unary operator.

You end up with dummyVariable1 + (expr), where expression consists of alternating the sign of dummyVariable2 using the unary forms of + and -. It ends up as dummyVariable1 + (-dummyVariable2), or -2.

I don't want to think about how you came up with this :-S :-)

Jeremy

One for the old pen and paper.. It should boil down to 1 + -3 as there are nine negative operators in the expression, the positive operators can be reduced which should make it equivalent to dummyVariable1 + -(-(-(-( -(-(-(-(-(dummyVariable2))))) )))) if I got all the brackets in there correctly or more simply 1 + ---------3 == 1 + -3 = -2. So yes it should have no problem compiling :)

Thomas Eyde

I don't know, and don't care. There should be laws against code like this.

Sammy

LOL! Why is that Thomas?

JC
JC

@Dave - "Without compiling this..."

So amazing that you could run this and come up with the answer.

Ken Sykora

@Sammy - Because it's not readable... at all :) But that's not the point to this post anyway.

Kevin

I suspect it does compile, but I don't care. This code causes any one reading such code to become homicidal and begin searching for the masochist who wrote such code. The reason? My God, if you have to ask .... :-P

Dave

@JC,

I wasn't trying to amaze. I can't help my lack of innate intelligence, hence I resorted to cheating :)

The reason for my comment was that my "without compiling this..." guess was dead wrong. I thought I'd cover the "giving reasons" part of the challenge instead :) .

I could have just omitted to mention I compiled it, but wanted to illustrate one key way someone could get the problem wrong. Sorry if I spoiled it for you :-\

Dave

@JC, you're quite right though -- it was a silly thing to do. Note to self: think before pressing submit. Consider my intraweb pass revoked :)

Driesie

I've had questions like this on interviews and always feel like saying "if anybody in my team wrote code like this, I'd tell them not to", there's no point ...

JC
JC

@Dave - no prob, I just felt like being rude that day :)

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. Production postmortem: The case of the lying configuration file - one day from now
  2. Production postmortem: The industry at large - about one day from now
  3. The insidious cost of allocations - 3 days from now
  4. Find the bug: The concurrent memory buster - 4 days from now
  5. Buffer allocation strategies: A possible solution - 7 days from now

And 3 more posts are pending...

There are posts all the way to Sep 10, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    31 Aug 2015 - The case of the memory eater and high load
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats