CodeSOD: Stocking Up
Sometimes, you find some code that almost works, that almost makes sense. In a way, that's worse than just plain bad code. Rene was recently going through some legacy JavaScript code for their warehouse management system.
Like any such warehousing system, there's a problem you have to solve: sometimes, the number of units you need to pick to complete the order is larger than the stock you have available. At that point, you need to make a decision: do you hold the order until stock comes in, do you partially fill it and then follow up with a second shipment, or do you perhaps just cancel the order?
Rene found a line like this:
pick.qty_to_pick -= pick.qty_to_pick - stock.available
So, if I want to pick 100 units, but only have 25 in stock, I'll decrement the qty_to_pick by 75. Which vaguely makes sense, but also is a weird and awkward way of saying "make the qty_to_pick equal to the stock.available".
I assume there are guards around this line which make sure it's executed only if the qty_to_pick is greater than the stock.available. At least, I hope so, because if not, some customers are going to be surprised by the quantity when their order arrives.
In the end, this code isn't strictly wrong, it's just the weirdest most awkward way of copying one value to another variable.
[Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.