← Back to context

Comment by friendzis

6 days ago

> Sufficiently unexpected (surprising) is cursed.

I think there three distinct classes of surprising that fall under this umbrella: 1. a behavior overrides another behavior due to some precedence rules 2. behavior of a construct in language x is different than behavior of similar construct in most mainstream languages 3. who in their right mind would design the thing to behave like this.

Examples could be: string "arithmetic" in most dynamically typed languages, PHP's ternary operator order of precedence and Python's handling of default function arguments. In all of these cases when you sit down, think about it and ask yourself "what's the alternative?", there's always a very reasonable answer to it, therefore, I think it is reasonable to call these behaviors cursed.

In this case the surprise factor comes in, because we are used to equating finally block with cleanup and I concur that many would trip on this the first time. But if you gave this exercise some time and asked "what should happen if finally block contains control flow statements?" the reasonable answer should be "take precedence", because the behavior would be surprising/cursed otherwise.

That's my reasoning.

> In this case the surprise factor comes in, because we are used to equating finally block with cleanup and I concur that many would trip on this the first time. But if you gave this exercise some time and asked "what should happen if finally block contains control flow statements?" the reasonable answer should be "take precedence", because the behavior would be surprising/cursed otherwise.

Wouldn't the reasonable behaviour be "throw a compiler error"?

  • Can the compiler perform sufficiently deep static analysis to cover all possible cases? At least in Java it is by definition impossible.

    • If it's that hard to figure out what the code should do by looking at, then it's probably not a great thing to allow in the language.

    • I don't see why not. If it's in a finally block, disallow control flow. Or am I missing something?

> But if you gave this exercise some time and asked "what should happen...

This applies to any cursed JavaScript code too (see https://jswtf.com). The increased cognitive load required to figure out unintuitive/counterintuitive code is what makes it cursed.