Skip to content
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

Add app.createRouter function #3623

Closed
wants to merge 1 commit into from

Conversation

czaarek99
Copy link
Contributor

Implement PR #3622 but against 4.x master branch instead

@czaarek99 czaarek99 force-pushed the app-create-router-4.x branch from bda0c07 to d9ea79d Compare April 16, 2018 14:50
* @public
*/
app.createRouter = function (options) {
options = options || {}
Copy link
Member

Choose a reason for hiding this comment

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

Since this modifies the options below, can you make it not modify the incoming options object?

var opts = options || {};

// ...
opts.strict = this.enabled('strict routing')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor

Choose a reason for hiding this comment

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

It still looks the same. Did you commit the fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dougwilson Yeah, it just doesn't seem like it. Look at the whole file and you'll see its fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

I did and it still says options = options || {} can you write here what you see this line as?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dougwilson Um yes... But it's not modifying the original object anymore because the code changed below.

Copy link
Contributor Author

@czaarek99 czaarek99 Apr 16, 2018

Choose a reason for hiding this comment

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

Well I mean, technically it is modifying it but only if it returns a falsy value in which case the user didn't actually pass anything into the function and in which case it doesn't matter.

Copy link
Contributor

Choose a reason for hiding this comment

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

The argument is still being redefined. Just use the code Wes suggested. Do not reassign arguments.

Copy link
Contributor

Choose a reason for hiding this comment

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

It does matter due to how v8 optimized and compiles functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dougwilson Oh I didn't realize. I fixed it now.

@czaarek99 czaarek99 force-pushed the app-create-router-4.x branch from d9ea79d to 1a48254 Compare April 16, 2018 17:05
@czaarek99
Copy link
Contributor Author

Seems like I broke the tests with my latest commit. I'll look into it.

@czaarek99 czaarek99 force-pushed the app-create-router-4.x branch from 1a48254 to 0f645b6 Compare April 16, 2018 17:38
@czaarek99
Copy link
Contributor Author

Fixed.

Copy link
Contributor

@dougwilson dougwilson left a comment

Choose a reason for hiding this comment

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

Awesome, looking good 👍 I'll fix the coverage reporting on the PR as it should be failing because one of your lines is not covered by a test. You can run npm run test-cov to generate a coverage report locally until I get the CI upload fixed.

caseSensitive: this.enabled('case sensitive routing')
}

if (options.hasOwnProperty('caseSensitive')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't use hasOwnProperty to access the key. Just follow the existing patterns so we don't break people who configure with plain objects

opts.caseSensitive = options.caseSensitive
}

if (options.hasOwnProperty('strict')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same hop concern here.

@czaarek99 czaarek99 force-pushed the app-create-router-4.x branch from 4a4a681 to 0b475bb Compare April 16, 2018 18:51
@czaarek99
Copy link
Contributor Author

@dougwilson The CI test should be 100% now and I stopped using hasOwnProperty

})

assert.equal(router.strict, false, 'options param strict did not override app.settings')
assert.equal(router.caseSensitive, false, 'options param caseSensitive did not override app.settings')
Copy link
Contributor

Choose a reason for hiding this comment

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

How do you know if this false is coming from the app settings or from the override?

Copy link
Contributor Author

@czaarek99 czaarek99 Apr 16, 2018

Choose a reason for hiding this comment

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

It's true by accident, I'll get it fixed.

* and allows them to be overriden by passing in an options
* object
*
* @param {Object} options
Copy link
Contributor

Choose a reason for hiding this comment

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

We had a PR about this right now, so we should make sure we land it right this time :) The options argument needs to be optional in the JSDoc here since it's optional in the code 👍

@czaarek99 czaarek99 force-pushed the app-create-router-4.x branch from f9296aa to b207a68 Compare April 16, 2018 19:05
*/
app.createRouter = function (options) {
var opts = {
strict: this.enabled('case sensitive routing'),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the case sensitive routing is a typo on this line. Let's fix that and add a test that will actually catch that mistake in the future 👍

Copy link
Contributor Author

@czaarek99 czaarek99 Apr 16, 2018

Choose a reason for hiding this comment

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

After staring at a screen for this many hours eventually you go blind 😆

Copy link
Contributor

Choose a reason for hiding this comment

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

That's why we review code 😄 happens to everyone

@czaarek99 czaarek99 force-pushed the app-create-router-4.x branch from b207a68 to 2d62933 Compare April 16, 2018 19:07
@czaarek99
Copy link
Contributor Author

czaarek99 commented Apr 16, 2018

Fixed the optional argument, tests and me being blind (hopefully).


return Router(opts)
}

Copy link
Member

Choose a reason for hiding this comment

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

This is mostly just a style thing, so if there is strong opposition feel free to ignore me, but I find that reducing the code required to achieve the same result often reduces maintenance and understanding costs in the future. For example, this could be written much more succinctly as follows:

app.createRouter = function (options) {
  var opts = options || {}
  opts.strict = options.strict !== undefined ? options.strict : this.enabled('strict routing')
  opts.caseSensitive = options.caseSensitive !== undefined ? options.caseSensitive : this.enabled('case sensitive routing')
  return Router(opts)
}

Sometimes more lines of code is a good thing for clarity, but in this case, there are a bunch of lines which can be achieved with one usage of a ternary. This also has the upside of not unnecessarily calling this.enabled if you pass the option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that we don't have to call this.enabled if the options are passed in, that's good. But to me this looks like a jumbled mess. It takes multiple seconds to decode what it actually means while undefined checks are really easy to make sense of imo.

Copy link
Member

Choose a reason for hiding this comment

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

Like I said, mostly a style thing :), but maybe you can find a middle ground without the unnecessary calls?

Also, technically it is just the same undefined checks you had but in a single line. Maybe other people like the more verbose syntax with the same result, I don't know and it probably shouldn't be the focus of any more energy.

app.createRouter returns a router which inherits settings from
app.settings unless they're overriden in the options param
@czaarek99 czaarek99 force-pushed the app-create-router-4.x branch from 2d62933 to 447e756 Compare April 17, 2018 17:49
@czaarek99
Copy link
Contributor Author

czaarek99 commented Apr 17, 2018

@wesleytodd The new code skips uneccessary this.enabled calls but uses the more verbose if syntax. Seems like the best solution to me 😸

@wesleytodd
Copy link
Member

Awesome, this is much more clean and clear. Thanks for the good work!

@czaarek99

This comment has been minimized.

@wesleytodd

This comment has been minimized.

@dougwilson dougwilson added this to the 4.17 milestone Sep 24, 2018
@dougwilson dougwilson mentioned this pull request Oct 27, 2018
23 tasks
@dougwilson dougwilson removed this from the 4.17 milestone May 10, 2019
@czaarek99 czaarek99 closed this Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants