Feature envy methods
When we try to break down our code base into components that make sense based on our use case and domain, we’re basically dividing the code into zones. One of the key things we need to be careful about is maximizing interactions within the zones and, conversely, minimizing interactions between different zones. Feature envy is like a warning sign in the code, describing when an object accesses another object’s fields to perform an operation instead of simply instructing the object on what to do.
As a simple example, let’s consider a method that calculates a price based on a payment request:
public Double calculatePrice(PaymentRequest paymentRequest);
Everything seems fine until we realize that we need the order and some user information to calculate the price. That’s when we start writing its implementation and, for instance, we must retrieve the order related to the payment request; in the first lines of the method, we’...