Skip to content

Conversation

@ljhaywar
Copy link
Contributor

No description provided.

@ljhaywar
Copy link
Contributor Author

Let's just pretend I named the branch correctly. This is what happens when you commit on Friday afternoon.

read.js Outdated

// Find up to 5 listings with at least 4 bedrooms and at least 2 bathrooms
// If you recently ran create.js, a listing named Beautiful Beach House should be included in the results
await findListingsWithMinimumBedroomsBathroomsAndMostRecentReviews(client, 4, 2, 5);

Choose a reason for hiding this comment

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

This works fine for the example, but it is approaching a readability issue. This could go either way, as there isn't a preset rule to this, but I would probably rewrite this to use destructuring:

async function findListingsWithMinimumBedroomsBathroomsAndMostRecentReviews(client, { minimumNumberOfBedrooms=0, minimumNumberOfBathrooms=0, maximumNumberOfResults=Number.MAX_SAFE_INTEGER }={}) {

Then, when the function is invoked, it would be called by doing:

await findListingsWithMinimumBedroomsBathroomsAndMostRecentReviews(client, { minimumNumberOfBedrooms: 4, minimumNumberOfBathrooms: 2, maximumNumberOfResults: 5 });

Pros:

  • Makes it clear what parameters you are passing in
  • Allows for flexibility in passing parameters (you don't need to worry about order)
  • Easier to make parameters optional / have default values

Cons:

  • More Verbose
  • If you don't want to have any parameters be optional, it makes it a little harder to enforce that

See this stackoverflow question for more discussion on the subject.

Choose a reason for hiding this comment

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

I would also move to passing the collection rather than the client.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tips on destructuring @daprahamian! I've updated the code. Is it ok/normal to have some functions with desctructuring and some without?

@codepope In the original version of the code, I passed the collection. I had to switch to passing the client when it was decided that the first blog post had to list the databases instead of running a query. To prevent having to re-explain the main() function in every post, I've been passing the client.

read.js Outdated
// Print the results
if (results.length > 0) {
console.log(`Found listing(s) with at least ${minimumNumberOfBedrooms} bedrooms and ${minimumNumberOfBathrooms} bathrooms:`);
results.forEach(function (result, i) {

Choose a reason for hiding this comment

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

nit: use an arrow function here:

results.forEach((result, i) => {
read.js Outdated
*/
async function findListingByName(client, nameOfListing) {
// See http://bit.ly/Node_findOne for the findOne() docs
result = await client.db("sample_airbnb").collection("listingsAndReviews").findOne({ name: nameOfListing });

Choose a reason for hiding this comment

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

needs a const:

const result =
read.js Outdated
// Get the results and send them to an array

// See http://bit.ly/Node_find for the find() docs
results = await client.db("sample_airbnb").collection("listingsAndReviews")

Choose a reason for hiding this comment

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

needs a const:

const results =
@ljhaywar ljhaywar force-pushed the create branch 4 times, most recently from b85200d to b309471 Compare October 28, 2019 12:25
Copy link

@daprahamian daprahamian left a comment

Choose a reason for hiding this comment

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

LGTM w/ one take-or-leave-it comment.


// Find up to 5 listings with at least 4 bedrooms and at least 2 bathrooms
// If you recently ran create.js, a listing named Beautiful Beach House should be included in the results
await findListingsWithMinimumBedroomsBathroomsAndMostRecentReviews(client, {

Choose a reason for hiding this comment

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

take or leave it: since you don't need to be explicit with the positional parameters, might want to rename this to something shorter like findListingsAndFilter?

@ljhaywar ljhaywar merged commit 5cab381 into master Oct 29, 2019
@ljhaywar ljhaywar deleted the create branch October 29, 2019 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants