Populate fields or return object
https://softwareengineering.stackexchange.com/questions/145182
-
04-11-2019 - |
题
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.
没有正确的解决方案