Diseño Procedimental
Diseño estructurado
Escrito por Darjus Kazakevic · revisado el
Steps over plain data, read from top to bottom.
Procedural design writes the solution as a sequence of steps over plain data. There is no class hierarchy and no indirection: you read it from top to bottom and you understand it. For scripts, glue code and small tools, this is not a compromise. It is the right answer.
It stops scaling when rules start to repeat. When the same check appears in a second procedure, and the copies start to differ, it is time to group the data with its rules. Wait for that signal. A class hierarchy added before it buys indirection and nothing else.
En esencia — Write plain procedures over plain data, and add a model only when the same rule starts to appear in more than one place.
Cuándo recurrir a él
Recurre a él cuando
- Scripts, glue code and small tools that read from top to bottom
- A one-off job, like an invoice run or a data fix, with no shared rules
- Batch jobs where each step runs once, in order
Reconsidéralo cuando
- The same check now lives in several procedures, and the copies disagree
- Long-lived state with rules that many callers could break
- Behavior that varies by kind, with new kinds arriving often
Conceptos clave
- Procedure
- A named sequence of steps that takes data in and returns data out. It is the unit you read, test and reuse.
- Plain data structures
- Records, arrays and maps with no behavior attached. Any procedure can read them. That is simple, and it is also the risk: nothing stops a caller from breaking a rule.
- Top-down decomposition
- Split a big task into smaller steps, then split those again, until each step is a short procedure. The top procedure then reads like a table of contents.
Reglas de diseño
- Wait for the repeated rule before modeling
- Stay procedural until the same check appears in a second procedure and the copies start to differ. That, not line count, is the signal to group data with its rules.
- Keep each procedure to one level of detail
- A top-level procedure calls named steps, and each step does the detail. When loading, pricing and writing are mixed in one function, split them into named steps.
- Pass data in, return data out
- Avoid global variables and hidden shared state. A procedure that takes its inputs as arguments and returns its result is easy to test and safe to run twice.
Errores habituales
- A 600-line main() that loads, prices, emails and logs. Nobody can test one step alone.
- Global variables shared between procedures. A change in one place breaks a run somewhere else.
- A class hierarchy for a 50-line script, built “for the future”. The future rarely needs it.
- Ignoring a copied rule. The copies drift apart, and invoices go out with different totals.