An item operation computes on an item while depending on the item's structural representation. In shopping-cart code, that means knowing the fields or shape of an item, such as its name and price. section 5.11 section 6.1
Common item operations include:
- Setting an item's price. section 6.1
- Getting an item's price or name. section 6.1
- Distinguishing item knowledge from cart operations and business rules. section 5.6
Reading a cart item
In calc_total(cart), the calculation accesses item.price while traversing the cart. That access gives the function item knowledge; the total calculation may also carry business meaning, so categories can overlap. section 5.6
C I function add_item(cart, name, price) { var new_cart = cart.slice(); #1 new_cart.push({ name: name, price: price }); return new_cart; }
C I B function calc_total(cart) { #2 var total = 0; for(var i = 0; i < cart.length; i++) { var item = cart[i]; total += item.price; } return total; }
B function gets_free_shipping(cart) { return calc_total(cart) >= 20; }
B function calc_tax(amount) { return amount * 0.10; }
Inspect the classification marks on add_item, calc_total, gets_free_shipping, and calc_tax to see how item structure relates to cart operations and business rules. section 5.6
