Created
March 22, 2025 11:46
-
-
Save trikitrok/8dded4f068a908c6c99d0b4c8da3a38d to your computer and use it in GitHub Desktop.
Sprout Method example
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
////////////////////////////////////////////////// | |
// 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(); | |
} | |
public postEntries(): void { | |
const entries = this.entriesRepository.getAll(); | |
// <-- this is the change point | |
for (const entry of entries) { | |
entry.post(); | |
} | |
this.blogActivityRecord.add(entries); | |
} | |
} | |
////////////////////////////////////////////////// | |
// Step 2: Develop the sprouted method with tests | |
export class BlogPostPublisher { | |
// rest of the code omitted for brevity... | |
public postEntries(): void { | |
// rest of the code omitted for brevity... | |
} | |
// the sprouted method | |
public uniqueEntries(entries: Entry[]): Entry[] { | |
const result: Entry[] = []; | |
for (const entry of entries) { | |
if (!this.blogActivityRecord.contains(entry)) { | |
result.push(entry); | |
} | |
} | |
return result; | |
} | |
} | |
//////////////////////////////////////////////////// | |
// Step 3: Call the sprouted method from the source method | |
export class BlogPostPublisher { | |
// rest of the code omitted for brevity... | |
public postEntries(): void { | |
const allEntries = this.entriesRepository.getAll(); | |
const entries = this.uniqueEntries(allEntries); // <-- call to the sprouted method | |
for (const entry of entries) { | |
entry.post(); | |
} | |
this.blogActivityRecord.add(entries); | |
} | |
public uniqueEntries(entries: Entry[]): Entry[] { | |
const result: Entry[] = []; | |
for (const entry of entries) { | |
if (!this.blogActivityRecord.contains(entry)) { | |
result.push(entry); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment