Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: How to Learn Design Patterns?
5 points by null_bomb on Oct 7, 2023 | hide | past | favorite | 18 comments
1. How do I learn different design patterns?

2. How do I know when to use one?

3. How to evaluate and use them when developing a feature?



Don’t.

Memorizing patterns is so cringe. Instead just learn to program, which means writing original software that solves a given problem as efficiently as possible.

There is more to programming literacy than just syntax. Writing software is a form of writing. Nobody memorizes English patterns when tasked to write a paper, because it demonstrates just how bad and cliche they are as an author. The same is true for programming.


Agree. And I feel like some patterns emerged from the limitations of programming laguauages Java & C++ which does not apply to languages like python/js. I mean when you can use lambdas/high order functions a lot of those patterns does not even make senses. Not to mention those pure functional programming languages.


Respectfully... I disagree. There absolutely are anti-patterns in code and patterns can be useful in efforts to avoid them.

I do, however, agree that "programming to pattern" can be a cringe experience if you're trying to force code into a pattern it isn't intended to hold. And you're right that the original Gang of Four book really was focused on C++ like languages.

Lisp and O-Caml DEFINITELY have patterns. Python ABSOLUTELY has patterns. JavaScript CERTAINLY has patterns. But yes, they're not exactly the same as patterns you find in C++, Java or C#.

Patterns are also useful to communicate concepts to peers. Saying something as simple as "I used an abstract factory pattern on this interface" is much simpler than explaining in detail how your constructor worked. I mean, you might want to write down some details about the constructor, but referencing a standard pattern is a quick shorthand.

I think the concept of patterns is extremely useful. The Gang of Four Design Patterns book is less useful if you're using Python than if you're using Java. But that doesn't mean its useless. And if your code doesn't fit inside a well-defined pattern, that's completely okay. There are more developers who have gone to the trouble to remember a few of the more popular design patterns than in the old days, so it might be easier to communicate your intent if you use a "standard" pattern. But again... design patterns are supposed to help you out, not tie you down. And if you're coding in Python or JavaScript, consider searching for "Design Patterns in Python" or "Design Patterns in JavaScript."

(and as an aside... when they added the "class" keyword to JavaScript, a small part of me died inside. JavaScript was designed with prototypal inheritance (like Self), not class-based inheritance (like Smalltalk.) Insisting that a "class" keyword be added to the language was like arguing for reducing the expressiveness of the language. But I digress. I mention it only to point out "I feel your pain" in issues of other people needing to impose their mental model on an unsuspecting development environment.)

But I believe patterns are useful not so much to help you code, but to help you communicate your intent to others.


How we can summarize the situation is this: Lisp has patterns, but you don't need a book to give you copy-paste boiler plate.

E.g. Visitor Pattern.

Lisp programmer be like: I'm a gonna recursively walk this tree of objects, and call a generic function with each object from the tree as one argument, and one specific object as the other argument. Done!

C++/Java programmer be like: I'm gonna study the Visitor Pattern again, and then prepare a bunch of skeleton code ...


Turns out Lisp has different patterns than C++. Who knew?


It's the same pattern. It just doesn't need an elaborate coding recipe from an instruction book. The essence of the pattern is there, but since the boilerplate is all gone it's almost unrecognizable.


I feel that actually we are on the same page.

patterns as a concept for communication are helpful.

But I mean for people coding in in python & js creating singletons are overkill right?


Oh yeah. I'd agree with that. Maybe the message that should be added to the beginning of the Design Patterns book is... "Some languages support different patterns than other languages. Not all patterns will be useful in all applications or programming environments."

Someone once told me Python supported OOP, so I tried to use Smalltalk patterns with Python. It was not pretty.


I've tried to do this for years now and I think it's not possible. I've heard that the people that use design patterns correctly end up discovering the pattern after they've intuitively implemented it already.


I wouldn't be surprised. The objective of design patterns isn't just to have a list of structures in your head you can rattle off in coding interviews. It's to provide you (the coder) with a way to think about program structure and function. Think back to when you learned functional programming. You can say "oh. functional programming is about programming with functions," but that's different than the moment you understood at an intuitive level how to construct programs out of functions. And different from the time you were able to reason about the operation of programs that are constructed out of functions.


IMHO the best book on design patterns is: https://gameprogrammingpatterns.com/ Examples are in C++ but easy enough to understand even if you're not familiar with C++.


1. I'm not the biggest fan of the Gang of Four book, but it's a reasonable place to start, especially if you're doing C++, Java or C#. But IMHO, the best way to learn design patterns is to examine code which uses one of the patterns. In the mid-90s I was working on a big Java project and one of my co-workers had boarded the design patterns train. He went out of his way to code in a way that was very obviously pattern-oriented. I did have to do some reverse work to try to figure out how particular patterns were better than other patterns or better than no pattern at all, but it absolutely made it easier for us to talk about the code.

Sadly, I think there's no substitute for "examine other people's code." Software documentation has fallen out of style as the young kids now believe every line of code they generate is a "golden pearl of wisdom, exquisitely obvious to all but the most dense. Oh? You don't understand it? You must be dense."

2. How do you know when to use a loop? Or an if...then construction? It might be hard to remember, but there was a time when you didn't know what a for() loop was. Then maybe you watched some videos or read some books and it made sense and you wrote a bunch of code with loops and then it was second nature, subsumed into the grey matter behind your eyeballs. I think the same thing happens with design patterns, but they're "bigger" than simple control structures, so you need a bit more training.

Also, don't be afraid to use only a few of them. They're supposed to help you, not make you feel like you're an idiot 'cause you haven't encountered a situation where you can use them all. They're not Pokemon. You don't have to get them all.

3. Sadly, I think it's just experience. Again. Read other people's code. Read the comments and issues to see if using the pattern made it easier or harder for other people.

Also... I was just looking at this and I think Dick Gabriel, as usual, has cogent comments to offer: http://wiki.c2.com/?PatternsAreNotTheLesserOfTwoEvils


In the wagon of 'read other peoples code', do check out https://refactoring.guru/design-patterns

I like how they translate to many languages, and how they build the notion of a pattern with simple example and also have a slightly more in depth example as well.

But do try to stay away from those.


From an educative perspective, you can read https://refactoring.guru/design-patterns/


Learn core design patterns only:

- Observer patterns => This is your AHA moments !

- Iterator patterns => So that you can use (for item of your_collection) { }

And the mother of all clean code: Monad.

That's it, you hardly need more than those.


Please don’t! Even if some smart looking egg head convince you… just don’t!

We have enough shitty over engineered software in the world already.


This is going to sound bad, and it's not intended as a personal attack, but... maybe software engineering isn't your jam?


Maybe Im just on different level than you or mean something different than you see between my letters.

Or you are some kind of blessed enlightened engineer who never faced a horrible “designed” million lines codebase to untangle?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: