-
Notifications
You must be signed in to change notification settings - Fork 23
Replace with keyword "exists" for clarity? #33
Comments
[deleted because the author have edited original post] |
I really dislike a postfix keyword operator. |
Would the same keyword work in any other context? |
To be clear, this is a different operator that you're proposing (not just a different syntax). Nullish coalescing is a binary operator, and you're proposing a postfix unary operator. I also think this operator just doesn't work for the use cases that nullish coalescing is trying to solve. In the CoffeeScript does use |
@alangpierce Appreciate the feedback. I admit my statusMessage example originally was half-baked and put together at 1 am. Updated the original post with more specific and clarifying examples to address the comments so far. One thing I found as I was writing the rudimentary test case examples with
|
e.g.
|
So your |
@hax yes that is right. The above equality would not hold true for non-nullish falsy values and you would have to place parentheses to change the order, like you did, and like we often already do with other complex boolean evaluation chains. |
We do it for not relying on operator precedences which is hard to remember. But it's not the case for It's very weird to understand So why not |
Because readability and clarity. We write code for other people who may or may not be the same skill level as us or used to programming in the same language as us. But most commonly-used programming languages (except TeX) are written in English. That is a reality of our profession. Whether you agree or not, I hope you see that this comes from a place of empathy. I think a trade-off of an edge case of falsy non-null values where one must add two parentheses is worth making if it means we aggregately save budding programmers even 2 minutes of time in having to look up and understand a new, confusing, potentially unnecessary operator with little precedence. Those are not my beliefs but they are well justified. Multiply the above time savings by the millions of new programmers who will be learning JavaScript in the coming decades and the trade-off in my mind heavily favors readability without a shadow of a doubt. |
Given the former speaker, personally I believe that there would be a lot of already existing syntax in need of renaming if the goal is to have them human language based. So obviously that has not been top priority previously and shouldn’t be now either IMO. I’ve had the benefit of already utilizing the proposed syntax ( My point is that syntax in general, to me, already are quite abstract. But that’s fine, since you have to learn them anyway. Although turning them concrete (read human language based) just for the sake of it will make them lose other capabilities |
As others explained above, Then again, if it's a keyword, why not just make it a global function? Nothing is clearer than a function call: the order of execution is well-defined, there are no gotchas with operator priority, etc. But it's so. damn. long! The whole idea of having symbols for operators is that they are common enough and are used often. It turns out that humans are pretty great at remembering symbols and understanding them in context, without a full explanation repeated each time. Just think about it: not just in code ( I think that |
@lazarljubenovic Great points, and also a great argument for reusing symbols from other nearby languages. |
I really like the idea of using
Additionally, I am strongly against the argument that "we should use this symbol because all other languages with this feature (and there aren't many) use this symbol". That line of thinking is the opposite of innovation, progress, and independent thinking. For example, Python uses |
The rationale is not "we should use this symbol because all other languages with this feature (and there aren't many) use this symbol". The rationale is "we should not invent a totally different surface syntax without very strong reason if other languages with this feature already have the solutions". |
Closing, since this proposal is at stage 4. |
The discussion in #17 gets to the heart of the confusion and use case of this operator. The opening statement and this comment #17 (comment) present both sides of the case really well. The operator's narrow purpose make it hard to understand its use case to a beginner. To add more clarity, maybe it would be better to just use a keyword: "exists". Highly readable and can work easily within js.
or an equally readable ternary version:
To execute a statement after the operator one can just continue using the regular
||
syntax.@alangpierce pointed out that this is a unary operator instead of a binary operator. In reality, it would function as both. Here are a few test cases to make this behavior clearer. In terms of the implementation, the unary version,
exists
, can either evaluate tofalse
or the value directly before the operator. It's not a fully boolean return value like||
. Another clarification is thatexists ||
is a combination operator similar toelse if
. It behaves slightly differently when used in conjunction than either one individually for enhanced readability. So in reality, there is both a unary version and a binary version.I realize the binary version can be confusing because of using
||
so there can be a different keyword or operator for it too. The prior art for this in Swift biases us to think that it has to be a binary operator but Swift is a statically / strongly typed language whereas with JavaScript we can have a perfectly functional and useful unary version.The text was updated successfully, but these errors were encountered: