Latest news and articles about DDD, CQRS, Event Sourcing and Software Architecture in general, aggregated daily and delivered weekly to your feed (RSS) reader. Subscribe here!

Week 31, year 2014

  • DRY is about Knowledge - Have a look at these two classes: <?php// example 1finalclassBasket{private$products;publicfunctionaddProduct($product){if(3==count($this->products)){thrownewException("Max 3 products allowed");}$this->products[]=$product;}}finalclassShipment{private$products;publicfunctionaddProduct($product){if(3==count($this->products)){thrownewException("Max 3 products allowed");}$this->products[]=$product;}} Would you say this is duplicate code? Does it violate the DRY principle (aka “Don’t Repeat Yourself”)? If it is, then the solution to get rid of it, could be something like this: <?php// example 2abstractclassProductContainer{protected$products;publicfunctionaddProduct($product){if(3==count($this->products)){thrownewException("Max 3 products allowed");}$this->products[]=$product;}}finalclassBasketextendsProductContainer{}finalclassShipmentextendsProductContainer{} The code is identical, but why? As good Domain-Driven Design practitioners, we’d have to check with the business. The product could be a flammable chemical, and for safety concerns, a shipment is not allowed to have more than three of them. As a consequence, we don’t allow customers to order more than three at a time. In another scenario, the similarity of the code might also be simple coincidence. Maybe supply of the product is limited, and we want to give our customers equal opportunity to buy it. Reasons to Change Whatever the case, it doesn’t matter. [Mathias Verraes]
Permalink | From 28 July 2014 to 03 August 2014 | Last updated on: Mon, 7 Jun 2021 09:18:52 GMT