r/programminghorror • u/Nomergraw • Jun 09 '26
What a simple constructor
Our former IT director (35+ years of experience) wrote this and didn't see what was wrong here.
268
Upvotes
r/programminghorror • u/Nomergraw • Jun 09 '26
Our former IT director (35+ years of experience) wrote this and didn't see what was wrong here.
162
u/No-Collar-Player Jun 09 '26
Just pull that all out in 1 method and call that in the constructor. Problem fixed.
83
u/Lunix420 Jun 09 '26 edited Jun 09 '26
I'm more bothered by the nesting that's going on here. I hate when people write an if and then put the entire rest of the function into an else instead of just returning early. Unnecessary indentation/nesting is one of my personal pet peeves.
I actually think a long/complex function can be totally fine and can be very readable, but as soon as there is a lot of nesting it's just not readable anymore in my opinion.
28
u/RipProfessional3375 Jun 09 '26
> EntityManagerInterface
Born to Java, forced to PHP.
3
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo âYou liveâ Jun 09 '26
Not entirely sure, but shouldn't constructors just make sure an object is ready for use?
2
u/MUSTDOS Jun 09 '26
Why does this format reek of AI trash? This is horrid even by PHP standards (if it has one)
2
u/Spicy_Jim Jun 10 '26
At least he added some comments to make things clearer.
2
u/phpMartian Jun 12 '26
Dude was in the zone and just made it happen. đ
1
u/silentkode26 Jun 09 '26
What gets me the most is how per instance variables and shared services live inside constructorâŚ
1
u/Successful-Pain-7494 Jun 13 '26
I don't see it either. The colours seem to be in order.
1
u/124k3 Jun 09 '26
ah yes if else and if else and anothe rif else and there is regex as well. (i always forget how to do regex)
if (straisemoty(this.str)) ... if (this is there) do this and similarly broke it into helper functions.
firtgermore i don't think comstuctor should do all this checks it should be a seprate fn doing it as handleConstructorInsertion.
(not good at coding so if anyone got more intresting ideas i am all ears)
1
u/RipProfessional3375 Jun 09 '26
My best attempted refactor, a bit lengthy due to the use of classes ,but I like the clarity.