-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional assertions #7
Comments
@ELLIOTTCABLE Thank you for reporting and good catch! I agree with you that some conditionals have side-effects and it's risky to remove them. if (foo(bar))
assert(widget); becomes if (foo(bar))
; or in babel if (foo(bar))
undefined;
Yeah thanks! Would you send issue and PR to babel-plugin-unassert? |
Also:
It would be better to be removed or throws semantic errors also in these cases for consistency. Otherwise, users cannot predict unassert behaviors. |
@falsandtru as is now unassert removes assertions if and only if it is a statement (its parent is an In your case, var foo = bar ? assert(0) : 0
The easiest way to detect which call is to remove is, the case when By the way, you must be using unassert-cli and I'm glad to see it's so useful for one-liner explanation :) |
So, I've put … rather a lot of thought into this today; and I've come to the following conclusion: There's no good way to do this by default. I think, whatever you do, it's going to be dangerously surprising in a not-inconsequential number of edge-cases. In situations like this, the only good user-experience, is a combination of requiring intervention, and educating the user; so here's my current suggestion:
I know that sounds drastic, but I really think it's necessary for a pleasant UX: (Once the user has established which one they wish to use, then it's up to them to maintain that; and it becomes something |
(Clarification: The reason I think this is such a big deal, is because we're literally talking about code differences between development/testing and production. That's a pretty serious opportunity for subtle bugs, if Enormous Effort isn't expended educating the developer as exactly what will change.) |
@ELLIOTTCABLE Thank you for clarification.
In terms of ECMAScript AST, a single Your case, if statement without curly braces, is a few edge case that if (foo(bar))
assert(widget); parsed AST of code above is not the same as code below if (foo(bar)) {
assert(widget);
} In a few cases like that, I'll go with your "Retain" strategy. It's a bugfix. BTW, It's nice to have to fail first at compile time. So I'll consider implementing it too. Thanks. |
Well, I don't see it as the same. This is pretty important, because Here's a real-world example of my own: I've a directed-cyclic-graph
Such methods already exist, but have a high runtime-complexity: say the Now, some engines will optimize-away an EmptyStatement conditional that This is a really common pattern in assertion-based development (I seem to
|
IMO, conditional assertions is NOT common. Writing assertion with conditional is error prone practice and is not effective, performant. assert(node.isSomeComplicatedGraphState && node.isSomeOtherComplicatedState()); |
I'm always using that style. |
The problem is, that example will fail: sure, you can short-circuit the runtime complexity of the second function, but at the cost of that assertion actually failing, when the point is that it shouldn't occur at all when node.isSomeComplicatedGraphState isn't the case:
And again, as above, I realize I can wrap
… and in both situations, this means writing external support code for my assertions: in the worst case, each I suspect I'm simply not making my case well, because … it genuinely seems extremely obvious to me that this is Really Important? Edit: To be clear, I understand the value of convention-over-configuration; and I also think the behaviour you already implemented / described is the best default behaviour. I disagree with your assertion (ha ha) that this isn't a problem; but if you understand that this is a real thing, that real people need, then we're on the same page … and I totally understand prioritizing the usual case, along with the maintenance-cost of adding configuration options to unassert, over this relatively edge-case situation.
|
@ELLIOTTCABLE Thank you for your suggestion again. First, I apologize that I misunderstood your intention of what "conditional" means. Now I understood. Yeah it's a real problem sometimes.
Second. I apologize that my explanation is not a nice one. It's not a fair one. I'm so sorry for that. Now I'm going to find the way to solve this problem. Then I'll tackle with conditional assertions in my way. So please stay tuned. |
So, at the moment, using babel-plugin-unassert, I can't write an assertion within a conditional:
So, I understand the dangers of messing with code around the actual call to the
assert
; but this particular one is really, really egregious (assertions occur alone behind a conditional all the time!)Here's my thought for how to deal with it:
By default, if assertion-statement being removed is the sole child of a conditional branch, then remove that conditional branch, and insert the condition-expression as a bare statement (incase it was written to have any side-effects):
And provide an option to entirely remove conditionals whose only contents are an assertion (i.e. “I am acknowledging that I know that I need to write my conditional-assertions to have no side-effects, or to include a second statement within their body if they do.”
If this is welcome, I'm happy to write a few failing test-cases and pull-request them. (=
The text was updated successfully, but these errors were encountered: