-
Notifications
You must be signed in to change notification settings - Fork 26.5k
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
callback && callback(); #52
Comments
+1 |
This leads to an error in JSLint and a warning in JSHint, "Expected an assignment or function call and instead saw an expression”. As JSLint explains its
The warning is useful and worth enabling as it does catch typos like JSLint explains. The line you wrote, var shouldCallCallback = (Math.random() > 0.5);
shouldCallCallback && callback; JSLint will give the same warning for the code, and this time it caught a typo: var shouldCallCallback = (Math.random() > 0.5);
if (shouldCallCallback) {
callback;
} The second example would likely be more quickly noticed by the developer, but again JSLint/JSHint is able to point it out. I find using an explicit There is nothing "wrong" with writing |
I think it is more readable to make an |
I make such if's, a one liners: if (callback) callback(); Still lint will scream, but I take such approach as much more justified (and readable) than hacky |
I added a link to this discussion so folks can come back and read through this and add to the discussion. |
I always prefer to use if statements while developing. When the project is built, uglifyjs will short-circuit the if statements to use the construct like yours. |
I don't think either one adequately covers safely attempting to run your callback
is what I use to be safe. I don't think a simple null check is a good idea especially when the arguments are variable or when you intend to use arguments[arguments.length - 1] convention:
Otherwise:
|
What about |
@yisibl thats equivalent to |
Was reading through the style guide, but couldn't find anything about this. Instead of
I've started using
Which I haven't found any issues with. Would this style guide frown upon this?
The text was updated successfully, but these errors were encountered: