Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Conversation

@blasvicco
Copy link
Contributor

Using inline if

Using inline if
@jonchurch
Copy link
Contributor

Thank you for your effort.

I dont think that there are any peformance gains here, and personally find this to be less readable code.

@blasvicco
Copy link
Contributor Author

No problem dude. It is not a performance improvement. It is a personal suggestion to avoid unnecessary lines.

Cheers ;)

Copy link
Contributor

@anonrig anonrig left a 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?

data.token = config.token;
}

data.token = data.token ? data.token : config.token;
Copy link
Contributor

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.

Copy link
Contributor Author

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
}

Copy link
Contributor

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
< 3

Copy link
Contributor Author

@blasvicco blasvicco May 1, 2017

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!


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;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

if (!currentGroup[nextGroupName]) {
currentGroup[nextGroupName] = {};
}
currentGroup[nextGroupName] = currentGroup[nextGroupName] ? currentGroup[nextGroupName] : {};
Copy link
Contributor

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] || {}

Copy link
Contributor Author

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);
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@anonrig anonrig merged commit 6118c66 into howdyai:master May 1, 2017
@jonchurch
Copy link
Contributor

jonchurch commented May 3, 2017

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants