Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Forked from franreyes/Budgets.cs
Last active June 26, 2024 18:28
Show Gist options
  • Save trikitrok/b2e9c5bef57c64a55f73fe6bf4efbed4 to your computer and use it in GitHub Desktop.
Save trikitrok/b2e9c5bef57c64a55f73fe6bf4efbed4 to your computer and use it in GitHub Desktop.
Budget {
public double CalculateTotal() { ... }
}
/* ---- */
OneClient {
...
budget.CalculateTotal();
...
}
/* ---- */
OtherClient {
...
budget.CalculateTotal();
...
}
/* new changes => Take into account spanish VAT (IVA) */
// Refactor step 1
Budget {
public double CalculateTotal() { // used by clients in production code
// ...
var taxes = 0.0;
return total + taxes;
}
}
// Refactor step 2
Budget {
public double CalculateTotal() { // used by clients in production code
// ...
return AddTaxes(total);
}
private double AddTaxes(double total) {
var taxesPercentage = 0.0;
var taxes = total * taxesPercentage;
return total + taxes;
}
}
// Refactor step 3, enables testing new functionality
Budget {
public Budget(double taxesPercentage = 0.0) { // used by clients in production code
this.taxesPercentage = taxesPercentage;
}
public static Budget WithSpanishTaxes() { // for testing purposes
return new Budget(0.21);
}
public double CalculateTotal() { // used by clients in production code
// ...
return AddTaxes(total);
}
private double AddTaxes(double total) {
return total * (1.00 + this.taxesPercentage);
}
}
// Final state: new feture working
Budget {
public Budget() { // used by clients in production code
this.taxesPercentage = 0.21;
}
public static Budget WithSpanishTaxes() { // used by tests
return new Budget();
}
public double CalculateTotal() { // used by clients in production code
// ...
return total * (1.00 + this.taxesPercentage); // AddTaxes was inlined
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment