Question

What is better to do in this type of scenario: populate fields or return an object?

The situation is like this: I'm doing calculations to find the prices (inc tax, ex tax, discounted etc) of a product. Is there any advice or are there any guidelines on how to handle a return value?

Populate fields:

public class ProductPrice
{
    public int BasePrice;
    public int IncTax;
    public int ExTax;
    // ...

    public void GetPricesForProduct(int ProductID)
    { /* Populate fields based on results */ }
}

Return object:

public class PriceCalculator
{
    public ProductPrice GetPricesForProduct(int ProductID)
    { /* Populate object fields and return object */ }
}
public class ProductPrice
{
    public int BasePrice;
    public int IncTax;
    public int ExTax;
}

I'm not sure I really like the first method, because the void return type doesn't make it clear what happens when the method executes. However, returning an object results in a class which has no functionality.

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top