News for hackers. Stuff that matters.
Re:On Design Patterns in C++ by AlexanderDhoore on Friday December 18, 2020 @03:56PM (#25471874)
Design Patterns are a massive code smell. I always prefer simple code over extensible over-engineered frameworks.
Re:On Design Patterns in C++ by TillE on Friday December 18, 2020 @04:11PM (#25472079)
For the nth time, design patterns are descriptive, not prescriptive. They're a guide for when you're facing a type of problem which has been encountered thousands of time before, not a blunt tool to be jammed mindlessly into every possible scenario.
Re:On Design Patterns in C++ by cle on Friday December 18, 2020 @08:05PM (#25473998)
The problem is that you can preach this over and over but some people will still use them mindlessly in every possible scenario, because it's easy and feels good.

I think we've done a poor job as an industry of beating new developers over the head with "know the tradeoffs!" Design patterns are taught without teaching their tradeoffs, and so new developers think that it's best practice to apply every possible design pattern as eagerly as possible.

I agree that naming patterns is useful, but IME they've been a net negative on the code bases I've worked on, since the way they're taught encourages using them mindlessly without considering their costs.

Re:On Design Patterns in C++ by ahartmetz on Friday December 18, 2020 @04:31PM (#25472321)
...and the elaborate ones aren't needed very often, in my experience. So if you have many of them, it's probably overuse.
Re:On Design Patterns in C++ by smitty1e on Saturday December 19, 2020 @10:19AM (#25477893)
I think the languages with multi-page exception dumps are a great example of design patterns applied to the point of absurdity.
Re:On Design Patterns in C++ by comeonseriously on Friday December 18, 2020 @06:02PM (#25473166)
Funny, I prefer Design Patters over extensible over-engineered frameworks.

Design patterns can reduce a lot of unnecessary cognitive overhead. For example, a builder pattern that takes a config a returns an evaluation pipeline is really simple and eliminates a lot of runtime if..else conditions.

Re:On Design Patterns in C++ by rualca on Friday December 18, 2020 @06:26PM (#25473378)
> Design Patterns are a massive code smell.

Are you really trying to argue that a collection of terminology aimed at describing high level concepts, or standard solutions to commonly repeating problems and requirements, is a code smell?

What other good engineering practices are also code smells?

Re:On Design Patterns in C++ by rimher on Friday December 18, 2020 @06:56PM (#25473579)
It's very close-minded to believe that having a common phrasebook to solve common problems can be described as a "code smell".

Design Patterns are tools in a box, aiming to create scalable solutions, but the way a solution is implemented is up to the implementer. Design patterns just try to establish the language.

Re:On Design Patterns in C++ by happytoexplain on Friday December 18, 2020 @04:01PM (#25471951)
Yes, but this approach doesn't scale.
Re:On Design Patterns in C++ by dvfjsdhgfv on Friday December 18, 2020 @04:31PM (#25472319)
Torvalds and a few other people would disagree with you.
Re:On Design Patterns in C++ by BeetleB on Friday December 18, 2020 @04:42PM (#25472414)
The reason it's just a few other people is an indicator that it doesn't scale.
Re:On Design Patterns in C++ by AnimalMuppet on Friday December 18, 2020 @04:58PM (#25472580)
"Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp." - Greenspun's Tenth Rule

In the same way, any sufficiently complicated C++ program contains an ad hoc, informally specified, bug ridden implementation of half of the Gang of Four design patterns. They would be better off just using the patterns, instead of trying to roll their own half-baked solutions to the same problems.

If you've got one of those problems, use the standard, tested solution. If you don't have one of those problems, don't shoehorn in a solution that doesn't fit.

It's not that hard to grasp (except for junior devs who have recently read a pattern book...)

Re:On Design Patterns in C++ by mdiesel on Friday December 18, 2020 @05:50PM (#25473057)
"Oh, it's just a singleton" - me after half an hour of reading yet another implementation calling it something else.
Re:On Design Patterns in C++ by AnimalMuppet on Friday December 18, 2020 @05:57PM (#25473108)
OK, let me extend my statement. If you've got one of those problems, use the standard solution, and call it by the standard name so that everybody understands what you're doing and how you're doing it.
Re:On Design Patterns in C++ by kenniskrag on Friday December 18, 2020 @04:16PM (#25472153)
patterns are not frameworks. Patterns are not libraries, too.
Re:On Design Patterns in C++ by amelius on Friday December 18, 2020 @04:26PM (#25472260)
But they could be ...
Re:On Design Patterns in C++ by kenniskrag on Friday December 18, 2020 @05:06PM (#25472644)
a library maybe but not a framework. The framework controls the program flow.
Re:On Design Patterns in C++ by ptr on Friday December 18, 2020 @04:03PM (#25471979)
So you would rather have a “sortListByName(list)” where the sort algorithm is hard-coded to what field we should compare on rather than a factored approach using the Strategy pattern? “sortList(list, [](const item& i) {return i.name();})”? I would say the factored solution using “Design Patterns” is the simple one here.
Re:On Design Patterns in C++ by akavi on Friday December 18, 2020 @04:16PM (#25472150)
I'm not sure I agree with the author of this book that that counts as an example of "The Strategy Pattern" (vs. simply "passing a function as a parameter").

Is having parameters at all "The Parameter Pattern"? Should we call passing a boolean flag "The Flag Pattern"?

To me, "Design Pattern" implies at least some higher order structure than that (and which is not natively abstractable over in the language in question).

Re:On Design Patterns in C++ by ptr on Friday December 18, 2020 @04:43PM (#25472425)
This is the crux of the matter. Design patterns are just common structures that have been found useful at some point in some context. Some programming languages have adopted these patterns, adding "native" support for them. If your programming language didn't have parameters, then yes, I guess someone would add the "Parameter Pattern". It's all rather arbitrary what ends up in a book on "design patterns".

Regarding the strategy pattern, when does it start becoming the pattern and when is it just "passing a function as a parameter"?

   sortList(list, [](const item& i) {return i.name();});

   struct MySorter
   {
      bool operator ()(const item&) {...}
   };

   sortList(list, MySorter{});

   class MySorter : public ISorter
   {
   public:
      virtual bool execute(const item&) override {...}
   };

   sortList(list, MySorter{});
Re:On Design Patterns in C++ by thom on Friday December 18, 2020 @05:40PM (#25472965)
Design patterns aren't common _structures_. I suppose this is why everyone gets so caught up in the language specifics and think they're trivial. Design patterns are common _design decisions_. The strategy pattern isn't saying "you can have callbacks", it's saying "sometimes you should allow a policy to be supplied by the user". Every day, in every language, even the ones with first class functions and people posting on the internet that design patterns aren't needed, people still use libraries with massive 'options' arguments to functions, instead of internalising the actual lesson of the strategy pattern which is to allow executable specifications of behaviour.

So yes, there are trivial things which you can argue the toss on, but in the last month I've used HTTP libraries that don't allow you to supply a strategy for retry logic and machine learning libraries that don't allow a strategy for early stopping.

Re:On Design Patterns in C++ by jcelerier on Friday December 18, 2020 @05:07PM (#25472654)
>Is having parameters at all "The Parameter Pattern"?

Yes

> Should we call passing a boolean flag "The Flag Pattern"?

The fact that we call it a flag, in itself a metaphor, shows that this is exactly what is happening already. We just omit the "pattern" as it is not necessary for understanding.

Pattern just means "something that occurs repeatingly". Some things do so obviously, others a bit less obviously.

My experience teaching comp. sci. is that when faced with problems that call for design patterns, ~40% of 20/22 y.o. students are able to come up with the "classical" design pattern implementations on their own in a couple hours without prior exposure to them. For the rest, well, we have the books.

Re:On Design Patterns in C++ by ColFrancis on Friday December 18, 2020 @05:41PM (#25472977)
> students are able to come up with the "classical" design pattern implementations on their own in a couple hours without prior exposure to them.

I think this is even mentioned in the GoF book. The point of naming these patterns is not that they're amazingly innovative. The point of "Patterns" is that when we give them a name we can (a) discuss them easier with a common language, and (b), we can recognise when we use them again so to remember the pitfalls of last time.

Re:On Design Patterns in C++ by Privacy846 on Friday December 18, 2020 @05:18PM (#25472764)
Yeah, pretty much. All languages have design patterns, but somehow everything in Java becomes a Pattern. So whether something is called a pattern or not is definitely very culture-dependent.
Re:On Design Patterns in C++ by AnimalMuppet on Friday December 18, 2020 @05:57PM (#25473112)
I think this about monads in Haskell. They're a design pattern (well, several different patterns). They just aren't called that.
Re:On Design Patterns in C++ by Privacy846 on Saturday December 19, 2020 @01:11PM (#25479585)
Design patterns are considered to be things that you can’t express directly in the language (that’s the Design Patterns book definition). Monads can definitely be expressed directly in Haskell. The only thing you can’t do is prove the Monad laws in Haskell (I think?).

Other languages, like e.g. Rust (because of lack of HKT), cannot express monads like Haskell can.

But yeah, you can definitely call it a design pattern if that’s what the culture around the language is like. But it would be like Java folks calling interfaces with default implementations for something goofy like “Interface with stateless implementations pattern”, and even they don’t go that far.

Re:On Design Patterns in C++ by AnimalMuppet on Wednesday December 23, 2020 @12:28AM (#25514671)
Late reply; I didn't see this earlier.

But in Haskell, a lot of the time you don't use a monad because you want a monad. You use a monad to do something else - to log in a pure way, for example. That use of a monad to solve a particular problem is a pattern.

I'd even argue that it's using the pattern to compensate for a weakness of the language. (Or, if you prefer, to help with things that are difficult within the language design and philosophy.)

Re:On Design Patterns in C++ by astrobe_ on Friday December 18, 2020 @05:49PM (#25473047)
That's sometimes called language idiosyncrasies.
Re:On Design Patterns in C++ by rualca on Friday December 18, 2020 @06:29PM (#25473406)
>>Should we call passing a boolean flag "The Flag Pattern"?

You mean like a bit field?

Did you realized that with only two words you can convey amultitude of information regarding it's use and implementation and properties?

Two words.

Perhaps there is more to design patterns than mindlessly complaining about stuff you don't fully understand.

Re:On Design Patterns in C++ by aparsons on Friday December 18, 2020 @04:30PM (#25472309)
The Flag Antipattern
Re:On Design Patterns in C++ by nesarkvechnep on Friday December 18, 2020 @04:20PM (#25472188)
I have a colleague which calls a higher-order function Reader pattern which is obviously absurd.
Re:On Design Patterns in C++ by msla on Friday December 18, 2020 @06:32PM (#25473427)
Yes, things like thread-local storage and singletons are so over-complicated.

/s

https://en.wikipedia.org/wiki/Software_design_pattern

Re:On Design Patterns in C++ by s1k3s on Friday December 18, 2020 @06:14PM (#25473277)
There's a balance to it, and finding that is what (I think) you should strive for in software development.
Re:On Design Patterns in C++ by skor on Friday December 18, 2020 @04:41PM (#25472407)
unless you're writing a framework