In big business applications, we write code to take care of issues. In this way, we ought not make new issues with the code. Note that when we express “frameworks code” or libraries where we go for superior or the issue we comprehend is too unpredictable in fact, it is permitted to forfeit clarity, however and still, after all that, we ought to do it cautiously to abstain from composing dark code that conceals the rationale.
Code tells a story
All that we write recounts a story. The code isn’t an exemption. The code ought not conceal the business rationale or the calculation that is utilized to take care of an issue. Rather, it should bring up out unmistakably. The names that are utilized, the length of the techniques, even the arranging of the code should resemble the issue has been managed care and polished methodology.
Can we imagine what is the role of the below class? It is doing some wired calculating? That’s all we can say about it for now…
public class Class1
{
public decimal Calculate(decimal amount, int type, int years)
{
decimal result = 0;
decimal disc = (years > 5) ? (decimal)5/100 : (decimal)years/100;
if (type == 1)
{
result = amount;
}
else if (type == 2)
{
result = (amount – (0.1m * amount)) – disc * (amount – (0.1m * amount));
}
else if (type == 3)
{
result = (0.7m * amount) – disc * (0.7m * amount);
}
else if (type == 4)
{
result = (amount – (0.5m * amount)) – disc * (amount – (0.5m * amount));
}
return result;
}
}
Presently imagine that it is a DiscountManager class which is in charge of ascertaining a discount for the client while he is purchasing some item in online shop.
- Come on? Truly?
- Unfortunately Yes!
It is totally disjointed, unmaintainable, unextendable and it is utilizing numerous awful practices and enemies of examples.
After certain modification we make DiscountManager class look as below;
public class DiscountManager
{
public decimal ApplyDiscount(decimal price, AccountStatus accountStatus, int timeOfHavingAccountInYears)
{
decimal priceAfterDiscount = 0;
decimal discountForLoyaltyInPercentage = (timeOfHavingAccountInYears > Constants.MAXIMUM_DISCOUNT_FOR_LOYALTY) ? (decimal)Constants.MAXIMUM_DISCOUNT_FOR_LOYALTY/100 : (decimal)timeOfHavingAccountInYears/100;
switch (accountStatus)
{
case AccountStatus.NotRegistered:
priceAfterDiscount = price;
break;
case AccountStatus.SimpleCustomer:
priceAfterDiscount = (price – (Constants.DISCOUNT_FOR_SIMPLE_CUSTOMERS * price));
priceAfterDiscount = priceAfterDiscount – (discountForLoyaltyInPercentage * priceAfterDiscount);
break;
case AccountStatus.ValuableCustomer:
priceAfterDiscount = (price – (Constants.DISCOUNT_FOR_VALUABLE_CUSTOMERS * price));
priceAfterDiscount = priceAfterDiscount – (discountForLoyaltyInPercentage * priceAfterDiscount);
break;
case AccountStatus.MostValuableCustomer:
priceAfterDiscount = (price – (Constants.DISCOUNT_FOR_MOST_VALUABLE_CUSTOMERS * price));
priceAfterDiscount = priceAfterDiscount – (discountForLoyaltyInPercentage * priceAfterDiscount);
break;
default:
throw new NotImplementedException();
}
return priceAfterDiscount;
}
}
I hope you agree that modified code speaks for iteself.