Birmingham | 25-ITP-Sept | Joy Opachavalit | Sprint 1 | Feature/destructuring#335
Birmingham | 25-ITP-Sept | Joy Opachavalit | Sprint 1 | Feature/destructuring#335enjoy15 wants to merge 8 commits intoCodeYourFuture:mainfrom
Conversation
illicitonion
left a comment
There was a problem hiding this comment.
This is looking really good - I left a couple of comments to consider for how to make your code even more clean, but this is all looking great, well done!
| ]; | ||
|
|
||
| // Print the receipt header | ||
| console.log("QTY ITEM TOTAL"); |
There was a problem hiding this comment.
This works, but let's imagine we wanted to change the spacing between the columns. Right now I would need to change two bits of code - this line, and also line 23 where we say how much padding to add.
Can you think how to restructure this code so that I would only need to change one place if I wanted to change the spacing?
There was a problem hiding this comment.
@illicitonion I define a variable for the column spacing and use it consistently in both places. This way, if I want to change the spacing, I nly need to update the variable.
| ); | ||
| }); | ||
|
|
||
| // Log the total cost |
There was a problem hiding this comment.
These comments don't seem to be adding a lot of value to this code... Have a read of https://www.jackfranklin.co.uk/blog/code-comments-why-not-how/ and think if either these comments could be changed, or could be removed, or if there are any changes you could make to your code to make them unnecessary.
There was a problem hiding this comment.
I'm still not sure the comments are providing much value here.
console.log(\nTotal: ${totalCost.toFixed(2)}); is the last line of code. And from reading the code, it's quite clear what it's doing: printing (console.log) the total cost (totalCost), at the end (it's the last line).
| const columnSpacing = 8; | ||
|
|
||
| // Print the receipt header with aligned columns | ||
| console.log("QTY".padEnd(columnSpacing) + "ITEM".padEnd(20) + "TOTAL"); |
There was a problem hiding this comment.
You're still repeating the 20 across these lines :)
Also, rather than having two different lines of code where you need to refer to the same variable, can you think how a function could help you not even need that variable?
| ); | ||
| }); | ||
|
|
||
| // Log the total cost |
There was a problem hiding this comment.
I'm still not sure the comments are providing much value here.
console.log(\nTotal: ${totalCost.toFixed(2)}); is the last line of code. And from reading the code, it's quite clear what it's doing: printing (console.log) the total cost (totalCost), at the end (it's the last line).
| const COLUMN_SPACING = 8; | ||
| const ITEM_COLUMN_WIDTH = 20; | ||
|
|
||
| function formatColumn(content, width) { |
There was a problem hiding this comment.
This can be a helpful function on its own, but it doesn't solve the problem of repeating the constants at each call site.
But each line has exactly three columns with the same spacings - quantity, item, and total. So it feels like you could make a function formatColumns(quantity, item, total) which is responsible for the column widths, and then call that function twice?
There was a problem hiding this comment.
Refactor column formatting function for receipt alignment
illicitonion
left a comment
There was a problem hiding this comment.
Looks good! But unfortunately your comment problem has come back, e.g. function formatColumns is clear enough about what it does - the comment "// Function to format multiple columns" doesn't add any extra information/value here. Similarly, the "Print the total cost at the end" comment came back.
|
Redundant comments removed. |
| ); | ||
| } | ||
|
|
||
| // Print the receipt header with aligned columns |
There was a problem hiding this comment.
I think there are still some redundant comments you could remove here :)
Learners, PR Template
Self checklist
Changelist