Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It has a problem in that you have to remember both to accumulate the points and perform the purchase (and generate the receipt, which is less of a problem as I assume that action has no side effects). In my mind, point accumulation should come automatically when performing a purchase. It's also rather natural that you get a receipt when performing a purchase, so in a way, your initial method was fine, except that it doesn't read very well.</p> <p>If you want a fluent interface I'd introduce an extra class which gently guides the client code into doing the right thing (assuming that all purchases happen with a card and accumulate points the same way):</p> <pre><code>class TravelClub { OngoingPurchase buyAmount(long amount) { return new OngoingPurchase(amount); } private Receipt buyAndAddPointsAndGetReceipt(long amount, long cardNumber){ // make stuff happen } public class OngoingPurchase { private final long amount; private OngoingPurchase(long amount){ this.amount = amount; } public Receipt withCard(long cardNumber){ return buyAndAddPointsAndGetReceipt(long amount, cardNumber); } } } // Usage: Receipt receipt = travelClub.buyAmount(543).withCard(1234567890L); </code></pre> <p>This way, if you forgot to call <code>withCard</code>, nothing happens. It's easier to spot a missing transaction than an incorrect transaction, and you can't get a receipt without performing a complete transaction.</p> <p><strong>Edit:</strong> As an aside, it's funny to think that we do all this work to make methods with many parameters readable, when for example named parameters would make the problem go away completely:</p> <pre><code>Receipt r = travelClub.makePurchase(forAmount: 123, withCardNumber: 1234567890L); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload