-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Code Improvement: #821
Code Improvement: #821
Conversation
Using inline if
|
Thank you for your effort. I dont think that there are any peformance gains here, and personally find this to be less readable code. |
|
No problem dude. It is not a performance improvement. It is a personal suggestion to avoid unnecessary lines. Cheers ;) |
anonrig
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR. I've added some comments to improve readability. Can you fix it?
lib/Slack_web_api.js
Outdated
| data.token = config.token; | ||
| } | ||
|
|
||
| data.token = data.token ? data.token : config.token; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data.token = data.token || config.token will make it more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with the || operator is that if the var is not defined it will throw an error.
Example:
testFunction(i) {
i = i || 10; //PERFECT
let t = t || 10; //ERROR because t is not defined
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If data is defined on the same scope and in the same line, JS interpreter will throw an error. You're right. But the thing is data variable is defined before this line and therefore will not throw an error if data.token is undefined.
Example:
> var data = {}
< undefined
> data.token = data.token || 3;
< 3
> data.token
< 3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!!!
I will change it ;)
Thanks man!
lib/Slack_web_api.js
Outdated
|
|
||
| data.client_id = data.client_id ? data.client_id : bot.config.clientId; | ||
| data.client_secret = data.client_secret ? data.client_secret : bot.config.clientSecret; | ||
| data.redirect_uri = data.redirect_uri ? data.redirect_uri : bot.config.redirectUri; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the OR operator for these operations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
lib/Slack_web_api.js
Outdated
| if (!currentGroup[nextGroupName]) { | ||
| currentGroup[nextGroupName] = {}; | ||
| } | ||
| currentGroup[nextGroupName] = currentGroup[nextGroupName] ? currentGroup[nextGroupName] : {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again currentGroup[nextGroupName] = currentGroup[nextGroupName] || {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
| } else { | ||
| slack_api.callAPI('files.upload', options, cb, false); | ||
| } | ||
| slack_api.callAPI('files.upload', options, cb, !!options.file); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
| return cb(null, json); | ||
| } | ||
| return cb(json.error, json); | ||
| return cb((json.ok ? null : json.error), json); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
Hey so why did this get merged in? There are only performance losses with ternary operators when replacing If/Else blocks in Javascript (although they are completely negligible here), and I don't find this to be more readable code, especially for newer devs (which botkit has lots of). @anonrig can you give some context to your choice here? |
Using inline if