EPUBincludes the word “and” (e.g., “It adds the prices of all cart items and applies a discount and applies sales tax”), it’s usually not going to be very maintainable. Listing 2.2 "After": Clear and Focused (shoppingCart.js) const DISCOUNT_RATE = 0.10; const TAX_RATE = 0.08; function calculateSubtotal(items) { return items.reduce((sum, item) => sum + item.price, 0); } function applyDiscount(amount, rate) { return amount * (1 - rate); } function addTax(amount, rate) { return amount * (1 + rate); } function processOrder(cartItems) { const subtotal = calculateSubtotal(cartItems); const totalAfterDiscount = applyDiscount(subtotal, DISCOUNT_RATE); const finalTotal = addTax(totalAfterDiscount, TAX_RATE); return 'Order processed!' + 'Your final total is $${finalTotal.toFixed(2)}'; } This "after" version is far more maintainable. If the tax rate changes, you only need to update the TAX_RATE constant, not hunt through a complex function. While this is a considerable improvement, it isn't perfect. Since the rate is still hard-coded, any change still requires a new deployment. A more advanced architectural solution would store this value in a database, but for now, we've solved the immediate problem of messy, unreadable code. Each piece of logic is now separate, easy to understand, and easy to test. Don’t worry if you don’t fully understand this yet. We’ll discuss this and more in Chapter 3 and explain why it’s so important. 2.4.3 Testability in Action: Breaking Dependencies Testability is
Rate this eBook
| File Name: | 744dedcd8aba7d529275cbf8c92e8b16.epub |
| Original Title: | Grokking Software Architecture |
| Creator / Author: | Matt Erman |
| Language: | English (en) |
| Identifier: | ASIN:410673 |
| Publisher: | Matt Erman Publishing |
| Category: | Non-fiction |
| Format: | 📗 EPUB eBook |
| File Size: | 3.5 MB |




