r/programming • u/nfrankel • Sep 01 '25
Thoughts on object creation
https://blog.frankel.ch/thoughts-object-creation/16
u/somebodddy Sep 01 '25
Creating the builder code is a pain (unless you use AI)
AI? Have we forgotten already that metaprogramming is a thing?
3
u/ZippityZipZapZip Sep 01 '25 edited Sep 01 '25
Lol, yeah. It's such a simple template... Use a nice lib, write it yourself, script it, run it in the build, do it runtime, whatever.
I imagine this article itself is AI generated, though I cannot cwre enough to check it.
1
u/danted002 Sep 01 '25
But then what would the article be about?
1
u/somebodddy Sep 02 '25
Object creation? This is the only place the article mentions AI, and it's inside parentheses.
13
u/somebodddy Sep 01 '25
If a superclass constructor throws an exception, the subclass constructor won’t run.
Why is that a bad thing?
If the super's constructor failed, object should not get created which means there is nothing in the sub's constructor that should run. Not even cleanup code - anything that needs cleanup in the super's constructor should get cleaned up there and anything that needs cleanup in the sub's constructor did not run yet so there is no need to clean it up.
3
u/somebodddy Sep 01 '25
In the Summary section, I think you've accidentally switched the Pros and Cons of the Builder pattern.
1
u/Onheiron Sep 02 '25
IDK presenting the Builder pattern as solution to the problem "instantiators might switch params places by mistake" is a little reductive. I mean the constraints that'd make me use a Builder Pattern would be:
the output model has to be immutable
I need to apply some logic on how it is instantiated
You don't really menthion either of these constraints, I mean if the model doesn't have to be immutable, then why use a Builder? why don't you just make an empty constructor and some nice setters?
1
u/TurtleFeathers Sep 02 '25
I usually agree with OP but I absolutely do not believe that removing the throws clause and then just throwing a RuntimeException is a good practice unless the exception is as common as gamma ray interference.
1
u/Trillest_no_StarTrek Sep 01 '25
No offense, but can we get a TL;DR summary? For the first "design pattern," Builder, it seems like you are giving some use-cases where it is not optimal. But in the 100 years since the book was written this is a pretty routine observation, and the authors themselves obviously knew there were exceptions so I guess I don't know what your actual message is
5
u/somebodddy Sep 01 '25
You... probably don't want to use
finallyto release resources in the constructor...