Skip to content

Instantly share code, notes, and snippets.

@tzachz
Last active May 25, 2018 17:30
Show Gist options
  • Save tzachz/4bedbcd9add851290c4c0e6450baeb7e to your computer and use it in GitHub Desktop.
Save tzachz/4bedbcd9add851290c4c0e6450baeb7e to your computer and use it in GitHub Desktop.
OneLevelOfAbstractionPerFunction
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;
}
}
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