Code Pattern: Separating Decisions and Actions

UPDATED: JUL 14, 2023 | PUBLISHED: JUL 13, 2023 | 799 words, 4 minute read β€” TIL

When developing, I keep running into situations where it turns out better to separate the decision making logic vs the actual actions that arise from it.

Benefits of separating πŸ”—οΈŽ

A couple clear benefits have popped out when I separate decisions from actions:

  • Simpler to test
    • Logic to make a decision is often self-contained & stateless, and can be written with few, if any, dependencies.
    • You can pass real-time information, data from storage, user input, or whatever else - use dependency injection to make life easy.
  • Makes assumptions explicit
    • Instead of a morass of conditionals, you now have clearly labeled outcomes. It should be clear in the future if your assumptions about the array of possible outcomes is missing pieces.

Negatives πŸ”—οΈŽ

There have to be downsides, right? I haven’t found any yet, but I’ll update this in the future if it bites me in the… leg.

Example pseudocode πŸ”—οΈŽ

A quick example to illustrate what I mean.

cdddlpeieeieee#nre#dtaaffrlrffuluaslrfoeneassesepipdtsacwcvckssmctettadfddaeimooieauu:uacaa_r:sal=r=s_Dkrrrkttatmteildiaeernneiecee_nedsoocc_e_o_t_sbV(ca_stntidnDDandidsaa)ttf.hiseteec_bobacl:ieree=oic_cctt(n_gkuntonnoidiiiy)_fegegimvsmsnstssoptl_rEm_ika((iiineyatorwedrykd)o>oospgourh.boee:nnn(=e(_noanni_c(M..a=)qdrto=[sdicIooc=u_(ew"esuNuutD=ej'v(dEtcir_ttieuoWe)bNhiorDccocDebHr.VesneToonie((Ag"i)nmm_sc))Tie]lotaeetiintin_n__yosIp_m(ddabpniSufinte.otito:l:onTsl.weu.Ht.,dnEtoIye.a(ncuSor.rtruotueeeemmcUdctc)eoNn_oio:_mKesrmraeNeeded:_Odts,sbW__):Nfofrofre>Tr_ocYrmo5Pde_rEecdda!cobsn'ir,:d)sdiseLeo(ninn)vsv)t[=d=ic"tP]R,ODe"n:v:str)Enum:

Keep up with me!

Subscribe to get emails when I write new essays.

    We won't send you spam. Unsubscribe at any time.

    See Also