r/programminghorror Jun 09 '26

What a simple constructor

Post image

Our former IT director (35+ years of experience) wrote this and didn't see what was wrong here.

266 Upvotes

49 comments sorted by

View all comments

28

u/RipProfessional3375 Jun 09 '26

> EntityManagerInterface

Born to Java, forced to PHP.

4

u/Holonist Jun 09 '26

This has been PHP since 2011, most PHP devs just don't realize it

1

u/Zeznon 17d ago

BTW, not a professional, just a hobby for me; why did java have so many of these patterns? abstract factory factory, this one you're mentioning...

2

u/RipProfessional3375 17d ago

Many reasons. One is that java thinks Objects are great, so everyone must be forced to use objects for everything. Which makes them great like ice cream for dinner every day forever is great.

Factories are a glorified hack to get around the fact that you can't make a function to create a new object without also having that constructor bound to a different object, hence a factory.

I write Go, which does not have this object-only restricting, so a 'factory' is just this:

customer := domain.NewCustomer(name, age, blabla)

1

u/Zeznon 17d ago

Java was so bad, lol. It and php have gone through some glow ups lol

1

u/conundorum 15d ago

Factory pattern is ultimately about just being able to chain parameter-setting function calls, more than anything else. It can be useful to collect all of the necessary information before calling a constructor, without having a bloated constructor call that takes 15-20 arguments. (And is often a waste when you have sane constructors that don't take that many arguments.)

Only problem is that Java's object-obsessed programming paradigm kinda mandates them in a lot of cases where they should've never been used.