Skip to content

Instantly share code, notes, and snippets.

@trikitrok
trikitrok / QuarterlyReportGenerator.ts
Created March 22, 2025 11:50
Sprout Class example
//////////////////////////////////////////////////
// Step 1: Identify the change point and determine the sprouted class interface
export class QuarterlyReportGenerator {
public readonly beginDate: Date;
public readonly endDate: Date;
public readonly database: Database;
constructor() {
// constructor with multiple side effects
@trikitrok
trikitrok / BlogPostPublisher.ts
Created March 22, 2025 11:46
Sprout Method example
//////////////////////////////////////////////////
// Step 1: Identify the change point and determine the sprouted method signature (input/output)
export class BlogPostPublisher {
private blogActivityRecord: BlogActivityRecord;
private entriesRepository: EntriesRepository;
constructor(blogActivityRecord: BlogActivityRecord) {
this.blogActivityRecord = blogActivityRecord;
this.entriesRepository = new MySqlEntriesRepository();
// After changing the code, Invoice should look like:
export class Invoice {
private readonly shippingPricer: ShippingPricer;
// ...
constructor(
billingDate: OurDate,
openingDate: OurDate,
originator: Originator,
export class Invoice {
//...
public getValue(): Money {
const total = this.itemsSum();
if (this.billingDate.after(OurDate.yearEnd(this.openingDate))) {
if (
this.originator.getState() === "FL" ||
this.originator.getState() === "NY"
) {
@trikitrok
trikitrok / Element.ts
Created March 22, 2025 11:17
Used to illustrate finding test points
export class Element {
private readonly name: string;
private text: string;
constructor(name: string) {
this.name = name;
this.text = "";
}
addText(newText: string): void {
@trikitrok
trikitrok / InMemoryDirectory.ts
Created March 22, 2025 11:11
Used to illustrate finding test points
export class InMemoryDirectory {
private readonly elements: Element[];
constructor() {
this.elements = [];
}
public addElement(newElement: Element): void {
this.elements.push(newElement);
}
@trikitrok
trikitrok / CppClass.ts
Created March 22, 2025 11:05
Used to illustrate Effect Sketches
export class CppClass {
private readonly name: string;
private readonly declarations: Declaration[];
constructor(name: string, declarations: Declaration[]) {
this.name = name;
this.declarations = declarations;
}
public getDeclarationCount(): number {
@trikitrok
trikitrok / Reservation.ts
Created March 22, 2025 11:03
Used to illustrate Feature sketches and Effect Sketches
export class Reservation {
private duration: number;
private dailyRate: number;
private readonly date: Date;
private readonly customer: Customer;
private readonly fees: FeeRider[];
constructor(customer: Customer, duration: number, dailyRate: number, date: Date) {
this.fees = [];
this.customer = customer;
@trikitrok
trikitrok / BannerAdChooserRefactored.ts
Last active March 22, 2025 10:44
Port of example from Re-Engineering Legacy Software book
// an abstract Rule class that each of our business rules will extend.
export abstract class Rule {
private readonly nextRule: Rule | null;
protected constructor(nextRule: Rule | null = null) {
this.nextRule = nextRule;
}
// Does this rule apply to the given player and page?
@trikitrok
trikitrok / BannerAdChooser.ts
Last active March 22, 2025 10:12
Port of example from Re-Engineering Legacy Software book
export class BannerAdChooser {
private readonly bannerDao = new BannerDao();
private readonly bannerCache = new BannerCache();
public getAd(player: Player, page: Page): Banner {
let banner: Banner;
let showBanner = true;
// First try the cache
banner = this.bannerCache.get(player, page);