by moralestapia · 3y ago · 0 comments
true + ("true" - 0) ?
"" && -0 ?
Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?
If you write code like this, you have much bigger issues than whatever you choose to write your code in.
My code has bugs occasionally, because I'm a human. Which is why I would like my programming language to return errors when I make obvious mistakes.
> Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?
No I cannot, which is why I can't imagine why JavaScript wouldn't return an error for this situation.
How does one enforce a strict subset with an easy setup? Why are people writing non-browser code in this horrible language, often without strict subsets or typescript?
Obviously there are solutions, but the solutions clearly aren't pervasive enough because the problem is still here (and growing, as more JS code is written), so we as an industry still need to keep talking about how problematic and poorly-designed JS is.
The old solution are the good books like the good part. The new solution would be typescript.
It's unlikely the API will get deprecated no matter how you talk about it. It's the windows of internet. Any sane shops would use typescript. U don't even need to write ts, and could just let the compiler do the hinting for you.
it actually aids in writing faster (more JIT-friendly) code by alerting you to these implicit type mutations/coercions.
The point is that in lots of languages these statements would simply result in compile time errors. In JS, though, the type coercion rules are so loose (and in many cases extremely non-obvious) that you can see this head-scratching behavior.
It's the same underlying behavior that leads to very cool/very insane stuff like JSFuck: https://jsfuck.com/
This weak typing is unique to a few languages like JS and PHP. Python is just as dynamic, but will at least throw an error if you try to accidentally add a string and a number. Static languages catch this even earlier.
get_current_username: Returns a nonempty string iff user is logged in.
get_balance: Returns a user's "balance" of some sort of credit or currency. Negative balances are possible.
In Python, where "falsiness" is very clearly defined by the language spec, it would be idomatic to write: should_render_balance = bool(get_current_username() and get_balance())in js, Boolean('' && 0) will give you the same answer as bool('' and 0) in python.
if i code golf in python (like the article does in js), i'll also get weird-looking code and "unexpected" outputs.
valid python: 0*"string"or"other_string"
> Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?
People do stuff like that in Python all the time. Personally, I don't do it in JS, though, because I find the conversion-to-bool rules much less predictable. In this case, though, I guess JS would behave the same.
typescript helps, but i have fired some of these footguns even with typescript.