Last active
May 25, 2018 17:30
-
-
Save tzachz/4bedbcd9add851290c4c0e6450baeb7e to your computer and use it in GitHub Desktop.
OneLevelOfAbstractionPerFunction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CampaignServiceImpl { | |
private static final double budgetMarginPercent = 0.03; | |
private final CampaignCostService campaignCostService; | |
/**** | |
* Read this and try to answer: when does a campaign have available funds? | |
* | |
* Hard to answer because method deals with different abstraction levels - | |
* mixing the important business logic with the insignificant low-level details | |
*/ | |
public boolean hasAvailableFunds(Campaign campaign) { | |
double margin = campaign.getBudget().getValue() * budgetMarginPercent; | |
double dailyAvailableFunds = campaign.getBudget().getValue() - campaignCostService.getCampaignDailyCost(campaign.getChannelId(), campaign.getTimeZoneId()); | |
double actualAvailableFunds = dailyAvailableFunds - margin - campaign.getMaxCpm(); | |
return actualAvailableFunds > 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CampaignServiceImpl { | |
private static final double budgetMarginPercent = 0.03; | |
private final CampaignCostService campaignCostService; | |
/**** | |
* Read this and try to answer: when does a campaign have available funds? | |
* When (budget - spend - margin) is positive! | |
* | |
* Method deals with one "level": concepts like Budget, Spend and Margin; | |
* Does not deal with how each of these is computed (lower level of abstraction, e.g. applying percentage) | |
*/ | |
public boolean hasAvailableFunds(Campaign campaign) { | |
final double remainingFunds = getBudget(campaign) - getTodaySpend(campaign) - safetyMargin(campaign); | |
return remainingFunds > 0; | |
} | |
private double getBudget(Campaign campaign) { | |
return campaign.getBudget().getValue(); | |
} | |
private double getTodaySpend(Campaign campaign) { | |
return campaignCostService.getCampaignDailyCost(campaign.getChannelId(), campaign.getTimeZoneId()); | |
} | |
private double safetyMargin(Campaign campaign) { | |
return (getBudget(campaign) * budgetMarginPercent) + campaign.getMaxCpm(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment