Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created March 22, 2025 11:59
Show Gist options
  • Save trikitrok/277401189f67c69f3e5bcde016cda46a to your computer and use it in GitHub Desktop.
Save trikitrok/277401189f67c69f3e5bcde016cda46a to your computer and use it in GitHub Desktop.
Wrap Method V1 example
//////////////////////////////////////////////////
// Step 1: Identify the change point
export class Employee {
public timeCards: TimeCard[];
public payPeriod: Date[];
public date: Date;
public payRate: number;
public payDispatcher: PayDispatcher;
public pay(): void {
const amount = new Money();
for (const card of this.timeCards) {
if (this.payPeriod.includes(this.date)) {
amount.add(card.hours * this.payRate);
}
}
this.payDispatcher.pay(this, this.date, amount);
// <-- this is the change point
}
}
//////////////////////////////////////////////////
// Step 2: Extract a method with the body of the current method
export class Employee {
public timeCards: TimeCard[];
public payPeriod: Date[];
public date: Date;
public payRate: number;
public payDispatcher: PayDispatcher;
public pay(): void {
this.dispatchPayment();
// <-- this is the change point
}
private dispatchPayment(): void {
const amount = new Money();
for (const card of this.timeCards) {
if (this.payPeriod.includes(this.date)) {
amount.add(card.hours * this.payRate);
}
}
this.payDispatcher.pay(this, this.date, amount);
}
}
//////////////////////////////////////////////////
// Step 3: Develop a method for the new behaviour with tests
export class Employee {
// rest of the code omitted for brevity...
public logPayment(logger: Logger): void {
// code for log payment
}
}
////////////////////////////////////////////////////
// Step 4: Call it from the current method
export class Employee {
// rest of the code omitted for brevity...
public pay(): void {
this.dispatchPayment();
this.logPayment(new AcmeLogger()); // <-- call to the new method
}
private dispatchPayment(): void {
// rest of the code omitted for brevity...
}
public logPayment(logger: Logger): void {
// code for log payment
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment