S — Single Responsibility Principle
Definition
Single Responsibility Principle
- In plain English
- The Single Responsibility Principle says a type should have only one reason to change.
- Think of it like
- It is like a kitchen where the pastry chef edits recipes only when desserts change, not when prices change.
Reason to change
- In plain English
- A reason to change is a person, team or rule whose requests would force you to edit the code.
- Think of it like
- It is like the department that would phone you to ask for an edit.
Why it exists
Finance changes refill pricing. Label printing breaks because both jobs live in the same type.
Two teams also edit the same file. Their edits can conflict, leaving you to combine unrelated changes.
How it works
SOLID
- In plain English
- SOLID names five design principles: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion.
- Think of it like
- It is like a five-part checklist for keeping a workshop easy to rearrange.
- List change requests
Name the people or rules that cause edits.
- Group related work
Keep methods together when the same requests change them.
- Split unrelated groups
Give each group a focused type.
- Connect the parts
Use a small coordinating type to call them in order.
RefillService contains three jobs before separation. RefillHandler instead uses Validator, Pricer and LabelPrinter.
One reason to change does not mean one method per type. Several fee methods can belong together when finance owns their rules.
Examples
Follow who requests changes
Pharmacists change validation, finance changes prices, and store operations change label layouts.
package main
import "fmt"
type RefillService struct{}
func (RefillService) Validate(count int) bool { return count > 0 }
func (RefillService) Price(count int) int { return count * 20 }
func (RefillService) PrintLabel(count int) { fmt.Println("tablets", count) }
func main() {
s := RefillService{}
if s.Validate(10) {
fmt.Println(s.Price(10))
s.PrintLabel(10)
}
}
package main
import "fmt"
type Validator struct{}
func (Validator) Validate(n int) bool { return n > 0 }
type Pricer struct{}
func (Pricer) Price(n int) int { return n * 20 }
type LabelPrinter struct{}
func (LabelPrinter) PrintLabel(n int) { fmt.Println("tablets", n) }
type RefillHandler struct {
v Validator
p Pricer
labels LabelPrinter
}
func (h RefillHandler) Handle(n int) {
if h.v.Validate(n) {
fmt.Println(h.p.Price(n))
h.labels.PrintLabel(n)
}
}
func main() { RefillHandler{}.Handle(10) }
The insight
Split by who asks for changes.
Separate calculation from presentation
Designers change the HTML certificate monthly. The finish-time calculation rarely changes.
HTML
- In plain English
- HTML is a text format that describes the structure of a web page.
- Think of it like
- It is like a page outline that marks headings and paragraphs.
package main
import "fmt"
type ResultsService struct{}
func (ResultsService) Elapsed(start, finish int) int { return finish - start }
func (ResultsService) Certificate(seconds int) string {
return fmt.Sprintf("<p>Finished in %d seconds</p>", seconds)
}
func main() {
s := ResultsService{}
fmt.Println(s.Certificate(s.Elapsed(100, 400)))
}
package main
import "fmt"
type TimeCalculator struct{}
func (TimeCalculator) Elapsed(start, finish int) int { return finish - start }
type CertificateRenderer struct{}
func (CertificateRenderer) Render(seconds int) string {
return fmt.Sprintf("<p>Finished in %d seconds</p>", seconds)
}
func main() {
seconds := TimeCalculator{}.Elapsed(100, 400)
fmt.Println(CertificateRenderer{}.Render(seconds))
}
The insight
Things that change for different reasons belong apart.
Separate data from storage and export
Donation stores a donation amount. Its Save and ToCSV methods also mix storage and export decisions into that data type.
CSV
- In plain English
- CSV is a text format that separates values with commas and records with new lines.
- Think of it like
- It is like a table written as one line per row.
package main
import "fmt"
type Donation struct{ Units int }
func (d Donation) Save() { fmt.Println("saved", d.Units) }
func (d Donation) ToCSV() string { return fmt.Sprintf("%d", d.Units) }
func main() {
d := Donation{12}
d.Save()
fmt.Println(d.ToCSV())
}
package main
import "fmt"
type Donation struct{ Units int }
type DonationStore struct{}
func (DonationStore) Save(d Donation) { fmt.Println("saved", d.Units) }
type CSVExporter struct{}
func (CSVExporter) ToCSV(d Donation) string { return fmt.Sprintf("%d", d.Units) }
func main() {
d := Donation{12}
DonationStore{}.Save(d)
fmt.Println(CSVExporter{}.ToCSV(d))
}
The insight
Keep storage and formatting decisions out of the donation data type.
Trade-offs
Pros
- Small, focused types.
- Easier tests.
- Fewer conflicting edits.
Cons
- More types to navigate.
- Too many splits obscure a simple flow.
Use when
- One type changes for unrelated reasons.
- Different teams own different rules.
Avoid when
- Tiny code where everything changes together.
- A proposed split only separates method names.
Quick check
Takeaways
- One type, one reason to change.
- Find reasons by asking who requests changes.
- A small coordinator can wire focused types together.