class Program
{
static void Main(string[] args)
{
var priceList = new List<int>() { 10, 20, 30, 40 };
var totalAmount = InvoiceCalculator.CalculateTotalPriceWithTax(priceList, 5);
Console.WriteLine(totalAmount);
}
}
static class InvoiceCalculator
{
private static int CalculatePrice(List<int> priceList)
{
return priceList.Sum();
}
private static int CalculateTax(int price, int taxRate)
{
return (price * taxRate / 100); // Ignore int datatype for simplicity. input values are immutable
}
public static int CalculateTotalPriceWithTax(List<int> priceList, int taxRate)
{
int totalPrice = CalculatePrice(priceList); // input values are immutable
int tax = CalculateTax(totalPrice, taxRate); // input values are immutable
return totalPrice + tax;
}
}
Features of the program are
- Inputs are immutable.
- Functions are not altering the state
- Use of pure functions
- CalculateTotalPriceWithTax is calling CalculatePrice and CalculateTax but not altering the state
• For simplicity I have used int datatype instead float, double for tax calculation.
Is it violating functional programming principle because CalculateTotalPriceWithTax() function is calling CalculateTax and CalculatePrice function?
Note: None of these calls are altering the state. For simplicity exception handling and aspects are ignored. Just interested in knowing the functional aspects of the program
int
for simplicity, but for accuracy you would want to useDecimal
for any monetary based calculations. \$\endgroup\$