A Love Letter to Patterns

Ryan James Spencer

Patterns are how we tend to carve up the world around us. Like feedback loops, they are everywhere once we start noticing them. Roger Antonsen feels that mathematics is all about discovering patterns. When the pattern is discovered, we come up with a language to describe that pattern and give us a handle on playing with it. When we have both of those we can start tinkering and playing with assumptions.

Patterns are usually globbed together with abstractions. Abstractions are usually misunderstood. What people tend to call abstractions are actually indirections. Abstractions are actually the rough structure or form of an idea or concept. The abstraction of a program is a specification for the program itself. I don't think patterns and abstractions are the same.

So how are they useful in your day to day? You may or may not realise you do what I'm about to describe but I'm fairly certain most of us do this regardless of us being conscious of it. Becoming more conscious of this process makes it more powerful, so let's shed some light.

When you are working, there is generally a discovery of patterns. You build a trust framework around these patterns; if a pattern is useful, you keep using it. Perhaps a pattern betrays you. Maybe you sit and dabble with it to make it work again at the same capacity it used to serve you. If you're ruthless enough, you ditch the pattern entirely and move onto new ones.

Consider a scenario where you are rigging up some code that needs to walk across a directory and perform an action on every file. You see a third party library is more ergonomic than the standard library so you use it, instead. If you're conscious about this as a pattern, you'll trust it and reuse it tirelessly until it stabs you in the back. It doesn't pay for you to go fiddle around with the standard library functionality or some other third party library because you'll be wasting time. This is also why trying to 'save' a forsaken pattern can come with a high cost. It does pay to generally treat patterns as throwaway in the spirit of speed. There's tons of these every day, in every language, and even beyond coding, so you will not run out if you decide a pattern isn't delivering.

As patterns age and you spend some time thinking about them you'll find you can increasingly discover ways to generalise them further. Patterns are amazing because they let you turn all the boilerplate into habits which means you'll have the mental room to solve other problems with deliberate attention.