Replace Temp with Query might add performance overhead

Hoang Tran 4 months ago updated by Daryl Stark 3 months ago 1

Image 1691

I believe the first solution on the left is more efficient than the second as it caches the result of basePrice for reference in conditional expressions. Can you please explain why we should favour the second one ?

Yeah, I agree. The solution needs to calculate the base price three times. I also miss the `quantity` and `itemPrice` initialization.

The cost of the operation for this one is small, though. The performance impact won't be big, because the calulcation is very trivial. It can be fixed by creating a class that caches the value. That's what I would do in this case. The object would get the `quantity` and `itemPrice` as associations and have a method to calculate the price. That will increase the lines of code, but gives more reusability and no performance loss.