Skip to content

Thank goodness I asked for help with these bitcoin functions#2

Open
MarlaJane wants to merge 2 commits into
masterfrom
answer
Open

Thank goodness I asked for help with these bitcoin functions#2
MarlaJane wants to merge 2 commits into
masterfrom
answer

Conversation

@MarlaJane

Copy link
Copy Markdown
Owner

No description provided.

@MarlaJane MarlaJane requested a review from jlcarmic April 9, 2020 19:49

@jlcarmic jlcarmic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could be cleaner. Dealing with orders one at a time instead of passing around the entire book would have made this much easier to solve and made your code more readable and maintainable. The only instance you'd even need to look at a second order is if the incoming order was partially fulfilled so why loop past it when you don't have to which is what your filters are doing. In fact you are looping through the entire array twice before even determining what your data looks like and if you need to work with it at all.

Score: 86 + 10 = 96%

Explainer Video: https://drive.google.com/open?id=10t8tsCqw_ESVrCTcEjTAmL9MevQu0bv9

Comment thread orderBook.js
function reconcileOrder(existingBook, incomingOrder) {
const oppOrderTypes = existingBook.filter(order => order.type !== incomingOrder.type)
let updatedBook = existingBook.filter(order => order.type === incomingOrder.type)
updatedBook = oppOrderTypes.length

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you're gonna short circuit on the existingBook.length you should do that before you start filtering.

Comment thread orderBook.js
Comment on lines +1 to +8
function reconcileOrder(existingBook, incomingOrder) {
const oppOrderTypes = existingBook.filter(order => order.type !== incomingOrder.type)
let updatedBook = existingBook.filter(order => order.type === incomingOrder.type)
updatedBook = oppOrderTypes.length
? updatedBook.concat(determineSamePrice(oppOrderTypes, incomingOrder))
: updatedBook.concat(incomingOrder)
return updatedBook
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you dealt with one order at a time you wouldn't need so many loops. You're filtering twice here and then later doing a findIndex. See the solution branches.

Comment thread orderBook.js
else if (existingBook[index].quantity < incomingOrder.quantity) {
incomingOrder.quantity -= existingBook[index].quantity
existingBook.splice(index, 1)
return determineSamePrice(existingBook, incomingOrder)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You've got flow of control split out across multiple functions which is why this code becomes so unreadable. It also makes debugging much harder. reconcileOrder should be responsible for flow of control with the other functions merely operating on and returning data for it.

Comment thread tests.js
Comment on lines +111 to +112
{ type: 'buy', quantity: 15, price: 5900 },
{ type: 'sell', quantity: 15, price: 6000 }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You shouldn't have to change the order of these if written correctly.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants