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 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 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 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 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...
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:
"You win", I told him. "Nah, Google won. But nobody really wins if you write code like that."I realize that's not the point, but I figured I'd mention it anyway.
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'.)
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?
The ultimate screen would be to follow the candidate home and see if they Google or test the answer.
try: return True finally: return False
-- There's an error in your program: * 'return' outside function ...
Just sayin'