Menu

Post image 1
Post image 2
1 / 2
0

5 Fat Structs We Split — And the Go Patterns That Replaced Them

DEV Community·Bala Paranj·about 1 month ago
#VFe6g0c8
#before#after#go#programming#types#struct
Reading 0:00
15s threshold

A 12-field god object, a Summary mixing counts with metadata, 5 types.go junk drawers, a 507-line command handler, and duplicated fields across sibling structs — how splitting and embedding produced focused, maintainable types. A struct with 12 fields is doing too many things. You cannot change one concern without risking another. Our security engine had a Runner with 12 fields. Three of them ( StaveVersion , InputHashes , identitiesByTime ) were per-evaluation state on a struct that was supposed to be reusable configuration. When two evaluations ran sequentially, the second evaluation's identitiesByTime was polluted by the first. The struct was too fat to reason about. Here are five fat structs we split, with the exact before/after code and the principle behind each decomposition. 1. The God Object — Runner with 12 Fields Before: Configuration and session state in one struct type Runner struct { // Configuration (long-lived) Controls [] policy . ControlDefinition Hasher ports .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More