-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Opt-out of Confusing use of '!'.
#455
Comments
Could you give us a valid usage example, please? |
I'm also curious about the IE6 sniffing. |
Closing this ticket since we didn't get any valid usage example. Feel free to re-open it later. |
Is
a valid sample (typecast operands to bool)?
|
Yeah that'd good. |
The samples from issue #211 are still valid, i.e. 'confusing' or even dangerous. |
if (!(mseq <= this.messages.length)) { throw new Error('invalid mseq: ' + mseq); } This one will nicely throw an Error when mseq is not a number as well if its greater than this.messages.length |
Came here with a use case similar to @axkibe. The inverted case won't catch both range and invalid data errors at once. This doesn't strike me as bad practice considering the resulting code is readable and direct in its intention. |
jshint's killer feature is the opt-out, as opposed to crockford-ed-in. |
For clarity, in our unit test, I have assert.greater et al defined as such: greater: function(lhs, rhs) {
if (!(lhs > rhs)) {
fail(new AssertionError(repr(lhs) + ' not greater than ' + repr(rhs)));
}
},
less: function(lhs, rhs) {
if (!(lhs < rhs)) {
fail(new AssertionError(repr(lhs) + ' not less than ' + repr(rhs)));
}
},
greaterOrEqual: function(lhs, rhs) {
if (!(lhs >= rhs)) {
fail(new AssertionError(repr(lhs) + ' not greater than or equal to ' + repr(rhs)));
}
},
lessOrEqual: function(lhs, rhs) {
if (!(lhs <= rhs)) {
fail(new AssertionError(repr(lhs) + ' not less than or equal to ' + repr(rhs)));
}
}, I wrote it that way for clarity and would appreciate an opt-out for this warning. |
+1 from my side for an opt-out (we're caught at |
BTW, this seems to be a duplicate: #578 |
This is an error, not a warning. By the doc-comments of the closing commit, warnings can be ignored, but not errors. |
So what should I do instead of if (!(event.which == ESCAPE_KEY)) { according to your best practices ? |
Why would you ever write that? What's wrong with being clear about the program's intention? if (event.which !== ESCAPE_KEY) {...} |
Not my code, dunno the intentions. I found it in some lib and jshint was not validating it because of that. |
Why are you linting 3rd party library code? Isn't that the author's job? |
Whether or not !(a == b) is ugly or not, there should be an opt-out, right? I thought the philosophy of jshint was to provide an opt-out for every warning. Personally, in the case of test framework assertions, I found it clearer to write the conditional to match the assertion: function assertEqual(x, y) {
if (!(x == y)) { ... }
function assertNotEqual(x, y) {
if (!(x != y)) { ... }
function assertGreater(x, y) {
if (!(x > y)) { ... } |
How about this example?
I could create a variable to hold this value, but why? To me, this doesn't seem like a confusing use of the NOT operator so it should not throw an error. |
Why do you need to be "clever" here? What's wrong with just writing what you actually mean? |
If someone wants to write I patch, I will gladly review it for inclusion, but until then I don't want to read another comment on this thread. Less talking, more doing. We can discuss whatever patch comes along as much as the author or contributors and interested parties would like. |
This commit adds special syntax to the /*jshint directive making it possible to ignore any warnings (but not errors). For example: /*jshint -W121 */ var a = 12.; The code above would raise a trailing dot warning but '-W121' suppresses it. For advanced use only. Closes jshintGH-578. Closes jshintGH-455. Closes jshintGH-624. Closes jshintGH-683. Closes jshintGH-699. Closes jshintGH-755.
I have a use case. I'm retrieving a value from a database that should be a boolean, but it comes back as either |
+1 for optout, |
+1 for |
actually could it be marked as valid construct in jshint (with no optout needed), @valueof? |
+1 for opt out |
It's great when the first Google result for a problem is a closed issue with no solution. Per #780, this option can be disabled/enabled with the not-at-all-cryptic +1 for a more cryptic opt-out. |
@cjc343 thanks. there should be a way to opt out from whenever someone asks for a way to opt out of something, people always come back with "but why would you ever want to write that, that's bad bad code". this is never a valid response to someone asking for an opt-out. if they're asking for an opt-out, they disagree with you that it's 100% always bad, and that's that. jshint is supposed to help us, not get in the way. everything should be opt-out-able. absolutely everything. |
What is the For example, the following is giving me the warning: function even(n) {
return !(n % 2)
} |
+1 for opt out |
console.assert(!!prototype === !!object, 'invalid state', prototype, object); |
+1. Here is one of my common use cases:
Note that |
Isn't that confusing for the poor person reading the code? Anyway, this has been merged into #780 where it is noted you can use |
if (data === undefined || (!(data.length > 0))) is considered confusing. so is if (data === undefined || !(data.length > 0)) Wat do? |
Have you tried -if (data === undefined || !(data.length > 0))
+if (data === undefined || data.length <= 0) ...although a property name like |
It's not equivalent, because for a non-array object with no length property your examples give false. |
Yes. I'm not sure why this is being discussed. |
!! is the easiest way to convert "anything" into "bool". +1 for opt-out |
The case I keep running into is wanting to test if something is not a positive number: |
This is exactly why I came here wondering why jshint was keeping me from writing code the way I want. |
var n = "1";
console.log(!(n > 0)); // false
console.log(typeof n != 'number' || isNaN(n) || n <= 0) // true Anyway, you can just use -W018 to disable the warning |
This check was added for issue #211 but there is no way to opt-out of this detection. As the original poster of that issue suggested, it should be enabled by default, but with an override.
The text was updated successfully, but these errors were encountered: