I realized, just today, that I have a gap in my programming knowledge. I discovered this while working on my top secret application that you are not supposed to know about (a gtk - matrix application which I’m writing in vala) and I hit a wall, which I’ve hit on most of my personal programming projects and even some shared ones (and a few at work), which is: I don’t know how to organize code, and the little I know about it I’ve learned from copying people or from trial and error.

I’ve heard co-workers, friends, and even some family, talk about the design patterns they’ve learned from books or school. I had heard about them, but I didn’t know what they really were or what they were for. And so I asked my wife (who is much more brilliant and beautiful than I am) what a design pattern was and where her old text book on the subject was.

I’d like to share what I’ve learned so far after having just read the introduction of the textbook “Head First - Design Patterns” and after doing some light duckduckgo-ing on the topic.

What is a Design Pattern?

From what I gather, a design pattern is a way to organize your code. That’s really it. There are other details we can say though, such as design patterns are based on design principles, which are building blocks behind design patterns.

The example given in the introduction of the book is the strategy pattern. The idea of this pattern is that you abstract the logic for certain behaviors of your object into interfaces and then have a collection of behaviors which you compose* into your object depending exactly on what behavior you need.

_*note: there is a really good video on the YouTube channel fun-fun-function that explains composition really well._

Since I’m a noob and don’t have any authority on this subject yet, let me also give you a quote from Wikipedia.

In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. Rather, it is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.

Why Should I Learn Design Patterns?

Well, for me, I noticed that when I write code outside of a code base that I’m not already intimately familiar with, or when I’m creating a new project, I don’t organize code very well. And when the code is organized well it’s after several iterations and a lot of trial and error. I’m also slow at coming up with solutions in these circumstances. I think that if I learn about general design patterns I will also learn design principles along the way and my code will turn out better.

My wife’s old textbook (Head First - Design Patterns) had some good points too. The authors mentioned that if your team all understand design patterns, it becomes easier to communicate about a problem. There is also the benefit of better and cleaner code re-use while also having a code base that is more resilient to change (normal people would have used the word “flexible” instead).

Any Down Sides to This?

From what I’ve seen so far while researching this, there are a number of people who say that design patterns are not needed if you are using a properly designed language. Most of them really mean you don’t need a design pattern if you are using a functional programming language, such as lisp. This debate is pretty clearly seen in the answers to the software engineering stack exchange post “Where are all the functional programming design patterns?”.

The chosen answer says:

…One could even say, that DP [Design Patterns] are not needed in functional programming – there is no itch which DP is cure for…

(Maglob - July 4th, 2011)

One of the top comments on his answer says:

To claim that design pattern doesn’t exist in functional programming is misinformation. Easiest counter-example is the monad. You do not need to use monad in functional programming, but it is a very common pattern people follow to facilitate application of pure function programming. That in essence is the definition of design pattern.

(voidvector July 7, 2014)


So far I’m convinced that learning design patterns and design principles will help me a lot with writing better code, faster.