News for hackers. Stuff that matters.
Re:Nerd Sniping: What does the code do at the end of the page? by tptacek on Tuesday November 24, 2009 @06:07PM (#960145)
It returns false (also in Ruby) --- the "finally" (or "ensure") clause runs after the function body, but before the function epilogue.

It's not really a "paradox" so much as it is very bad style to "return" from a "finally".

Edit

My partner Dave has a much better trick question:

    def foo
        begin
            return false
        ensure
            next
        end
    end
"You win", I told him. "Nah, Google won. But nobody really wins if you write code like that."
Re:Nerd Sniping: What does the code do at the end of the page? by wvl on Tuesday November 24, 2009 @07:46PM (#960334)
Here's a good article that explains what happens: http://blog.leshill.org/blog/2009/11/17/ensure-with-explicit...
Re:Nerd Sniping: What does the code do at the end of the page? by ahlatimer on Tuesday November 24, 2009 @07:42PM (#960324)
The last trick question doesn't work in Ruby, at least not Ruby 1.8.7. Gives me a "LocalJumpError: unexpected next".

I realize that's not the point, but I figured I'd mention it anyway.

Re:Nerd Sniping: What does the code do at the end of the page? by notauser on Tuesday November 24, 2009 @06:12PM (#960158)
Huh, interesting. The code supplied is:

try { return true; } finally { return false; }

Which returns false. I guess this is because of the guarantee that the finally block will be run, no matter what. The JS interpreter must be catching the return and holding it pending the execution of the finally block.

Checking with

try { return true; } finally { alert('hi'); }

Shows this to be true. (It both returns true AND alerts 'hi'.)

Re:Nerd Sniping: What does the code do at the end of the page? by pkulak on Tuesday November 24, 2009 @06:27PM (#960192)
I see it that the "finally" is run right before the return. So, the statement "true" would be evaluated, then control would go to the "finally" block, where the method would return false.
Re:Nerd Sniping: What does the code do at the end of the page? by pinstriped_dude on Tuesday November 24, 2009 @06:27PM (#960191)
Its a tricky question, but not something I would use to find a "keeper". From the Java tutorial - "The finally block _always_ executes when the try block exits." After the interpreter executes the "return true;" statement, it exits the try block. And then, if you know that "The finally block _always_ executes when the try block exits.", you know that the code block will return false;
Re:Nerd Sniping: What does the code do at the end of the page? by blasdel on Tuesday November 24, 2009 @08:54PM (#960430)
It's not the answer that makes this a "keeper" screen, but the candidate's progression of facial expressions.
Re:Nerd Sniping: What does the code do at the end of the page? by deekayteez on Tuesday November 24, 2009 @06:18PM (#960166)
in .NET its illegal - "Control cannot leave the body of a finally clause"
Re:Nerd Sniping: What does the code do at the end of the page? by Jupe on Tuesday November 24, 2009 @06:45PM (#960236)
It appears MS thought this through. The contrived example is but ONE way a mediocre programmer could really munge this stuff up if the language is not specific about protecting exit points from a finall{} block.
Re:Nerd Sniping: What does the code do at the end of the page? by echaozh on Wednesday November 25, 2009 @03:02AM (#960822)
What about throwing exceptions in the finally block? Isn't it an exit point too?

When you call non-trivial 3rd party code from your finally block, how do you ensure it doesn't throw? Or what do you do if it does? Indefinitely nested try blocks?

Re:Nerd Sniping: What does the code do at the end of the page? by nollidge on Tuesday November 24, 2009 @07:29PM (#960300)
Indeed. I can't think of a reason as to why any language would even allow it.
Re:Nerd Sniping: What does the code do at the end of the page? by Jach on Tuesday November 24, 2009 @10:56PM (#960596)
I know it's horrible in practice, and I really can't see a valid use for this particular case, but forbidding things like this (or other things like multiple inheritance) just because it can be abused has never seemed like a good idea to me.
Re:Nerd Sniping: What does the code do at the end of the page? by nollidge on Wednesday November 25, 2009 @12:46PM (#961511)
It's not just that it can be abused, though - every use of it is an abuse. Finally{} is not for decisions and manipulating values and program logic, it's for cleaning up resources.
Re:Nerd Sniping: What does the code do at the end of the page? by bantic on Tuesday November 24, 2009 @08:16PM (#960380)
I wonder about the merits of this sort of thing as an interview screen, though. There's not necessarily a strong correlation between knowledge of language esoterica and other more important qualities like perseverance and raw intelligence.
Re:Nerd Sniping: What does the code do at the end of the page? by xsmasher on Tuesday November 24, 2009 @09:54PM (#960517)
The test is not whether you know the correct answer, but whether your brow furrows when you read the code. If you don't find the snippet interesting you fail.

The ultimate screen would be to follow the candidate home and see if they Google or test the answer.

Re:Nerd Sniping: What does the code do at the end of the page? by blasdel on Tuesday November 24, 2009 @08:58PM (#960434)
The point is not to assess language knowledge, but disposition. Even if the candidate is unamused, they should still instantly get what the interviewer found punny about it.
Re:Nerd Sniping: What does the code do at the end of the page? by zephjc on Tuesday November 24, 2009 @06:14PM (#960161)
it's not so mysterious if you know what the intent of a 'finally' block is.
Re:Nerd Sniping: What does the code do at the end of the page? by tptacek on Tuesday November 24, 2009 @06:37PM (#960216)
I agree; the trickiest thing about it is the confusing relationship between the syntax for exception handling and the syntax for cleanup blocks, which are only vaguely related concepts.
Re:Nerd Sniping: What does the code do at the end of the page? by akkartik on Wednesday November 25, 2009 @11:59AM (#961407)
That code uses one of several gotchas in java: http://www.cs.arizona.edu/projects/sumatra/hallofshame
Re:Nerd Sniping: What does the code do at the end of the page? by lurkinggrue on Tuesday November 24, 2009 @07:45PM (#960333)
Error: Norman coordinate.
Re:Nerd Sniping: What does the code do at the end of the page? by j_baker on Tuesday November 24, 2009 @06:19PM (#960168)
It's also worth noting that this won't work in Python before 2.5 (or maybe 2.4).
Re:Nerd Sniping: What does the code do at the end of the page? by josephkern on Tuesday November 24, 2009 @07:18PM (#960285)
You mean after 2.5 right?

try: return True finally: return False

-- There's an error in your program: * 'return' outside function ...

Just sayin'

Re:Nerd Sniping: What does the code do at the end of the page? by paulbaumgart on Tuesday November 24, 2009 @07:41PM (#960323)
It works if you put it in a function (2.5.1):

  >>> def test():
  ...  try: return True
  ...  finally: return False
  ... 
  >>> test()
  False
Re:Nerd Sniping: What does the code do at the end of the page? by Jach on Tuesday November 24, 2009 @10:53PM (#960593)
2.6.2 as well. I liked this little snippet, though I grumbled a little on the inside and would be appalled if I ever found something like that in serious code...
Re:Nerd Sniping: What does the code do at the end of the page? by kevindication on Tuesday November 24, 2009 @06:22PM (#960178)
In 2.3 it does. Why wouldn't it?
Re:Nerd Sniping: What does the code do at the end of the page? by j_baker on Wednesday November 25, 2009 @10:23AM (#961209)
I swear I read that this behavior changed somehow in Python 2.5. But I'm not sure how. But it does at least appear to work in python 2.4.
Re:Nerd Sniping: What does the code do at the end of the page? by kevindication on Wednesday November 25, 2009 @03:26PM (#961744)
In 2.5 they made try/except/finally possible.
Re:Nerd Sniping: What does the code do at the end of the page? by dpcan on Tuesday November 24, 2009 @06:49PM (#960244)
A couple things can happen I guess, it depends on the interpreting language.
Re:Nerd Sniping: What does the code do at the end of the page? by huhtenberg on Tuesday November 24, 2009 @06:12PM (#960159)
It's just a dumbed down version of Duff's device :-)
Re:Nerd Sniping: What does the code do at the end of the page? by docmach on Tuesday November 24, 2009 @06:36PM (#960212)
How is this related to Duff's device?
Re:Nerd Sniping: What does the code do at the end of the page? by tptacek on Tuesday November 24, 2009 @06:38PM (#960219)
It isn't even a very little bit related to Duff's device.
Re:Nerd Sniping: What does the code do at the end of the page? by huhtenberg on Wednesday November 25, 2009 @04:24PM (#961849)
Simple. It too snips nerds, but only the weaker ones.