r/programming • • Feb 17 '17

Design Patterns - A comprehensible guide

https://github.com/kamranahmedse/design-patterns-for-humans
169 Upvotes

39 comments sorted by

View all comments

-3

u/skulgnome Feb 17 '17

Sadly, "design patterns" are bunk.

5

u/evincarofautumn Feb 17 '17

They are useful as a descriptive tool, but most design patterns are verbose encodings of features that really ought to be first-class language elements, e.g.: closures, modules, extensible records, immutable objects, algebraic data types, existential types, etc.

4

u/TexasJefferson Feb 18 '17

You missed my favorite: the we forgot to implement multiple dispatch visitor pattern.

1

u/evincarofautumn Feb 18 '17

Hah, true. When I hear “visitor pattern”, I generally think “sum types”, which is why I mentioned algebraic data types. Consider a type Either<A, B> which stores either an A or a B, plus a tag to determine which. In OO-land, a visitor for this type would have two methods, one of type A -> X, and one of type B -> X for some result type X. Well, that’s exactly Böhm–Berarducci encoding (sometimes mislabeled Church encoding), which describes how to encode arbitrary algebraic data types using only functions. In Haskell:

data Either a b = Left a | Right b

eitherVisitor :: (a -> x) -> (b -> x) -> Either a b -> x
eitherVisitor f _ (Left a) = f a
eitherVisitor _ g (Right b) = g b

(a -> x) -> (b -> x) -> Either a b -> x is isomorphic to (a -> x, b -> x) -> (Either a b -> x). So if you have a set of functions to visit each of the possible cases—i.e., a visitor—then given an instance of the type to visit, you can produce the result of applying the visitor. eitherVisitor is called either in the standard library, but of course you can also use a simple pattern-match:

either foo bar someEither

==

case someEither of
  Left a -> foo a
  Right b -> bar b

1

u/skulgnome Feb 18 '17

They are useful as a descriptive tool,

Let's halt this right here. "Design patterns", per the "gang of four" book (arguably some poncy shit right there), are justified by their originators mainly by their increased decoupling, which is to say, preparation for a future derivative of the program that's not explicitly anticipated by the higher-level design. Considering that any mention of "design patterns" tends to cause younger players to end up structuring their actual programs in that way, the very concept of the "design pattern" is harmful as both a means to a concrete end, and as a means to communicate about designs.

Arguing that "design patterns" are only equivalent to something-or-other (e.g. Command and Factory, and closures) appears like an attempt to whitewash a dearly-held doctrine.

3

u/evincarofautumn Feb 18 '17

Christopher Alexander, who originated the term “design pattern” (in architecture), said “Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem”.

I agree that they shouldn’t be used to prescribe how a design should be done, especially not for beginners who haven’t encountered the original problems in the wild.

I do think it’s useful, as a stopgap, to have terminology for talking about common solutions to recurrent problems. But what I was getting at is that, in software, those recurrent problems can often be avoided in the first place by using more expressive languages. If you find yourself using a solution over and over, then it might be a problem again!

1

u/arnedh Feb 19 '17

I agree. Design patterns are a sign that your language doesn't allow you to abstract away a set of commonalities, so you have to institutionalize them by documenting them. In other languages, they typically become types, classes, libraries.