Simplifying method calls
Method calls are essential in OOP, as they enable objects to perform specific tasks or actions. There are many techniques intended to simplify the way objects interact with each other; we’re going to see some of the most interesting ones.
Avoiding side effects
We already discussed the side effects and mutability of objects in Chapter 3, explaining why they are not ideal in a software project. A typical way to cause side effects is when you mix a query, which is a part of the code that simply retrieves information, and a modifier, which is code that performs an action on some data or system, thereby changing its state. Here’s an example of this:
public Price getTotalItineraryPrice(User user, Itinerary itinerary){ Price totalPrice = calculateTotalPrice(itinerary); emailService.sendPriceRecap(user); return totalPrice; }
In this example, the method calculates the total...