Linked Questions
13 questions linked to/from Legitimate "real work" in a constructor?
1 vote
2 answers
776 views
Real Work in Constructor vs Factory Method [duplicate]
So been doing a lot of reading/research into clean code/design, OOAD, refactoring, TDD, etc. Just trying to improve my designs to be easier to extend and maintain. One thing that is come up quite ...
1 vote
0 answers
516 views
Are constructors with complex initialization logic always bad? [duplicate]
I've recently read this blog post regarding what a constructor should do and I am also reading Eric Evans' book on Domain Driven Design. Both the blog post and the book state that a constructor ...
1 vote
2 answers
517 views
Can and should constructors do more work than merely assigning values to fields? [duplicate]
A typical introductory example to OOP, classes, and constructors is object Car, with properties such as float fuel, bool is_engine_running, etc etc, and a class and constructor definition might be as ...
2 votes
0 answers
37 views
What reasons would someone separate instantiation and data loading? [duplicate]
I was wondering what the purpose of separating the instantiation logic and the data loading logic of a class that loads data into memory if the class is a one time use. Here is an example of what I ...
51 votes
15 answers
9k views
Is there ever a reason to do all an object's work in a constructor?
Let me preface this by saying this is not my code nor my coworkers' code. Years ago when our company was smaller, we had some projects we needed done that we did not have the capacity for, so they ...
33 votes
1 answer
9k views
Is "StringBuilder" an application of the Builder Design Pattern?
Is the "Builder" pattern restricted to addressing the "telescoping constructor" anti-pattern, or can it be said to also address the more general problem of complicated creation of immutable objects? ...
11 votes
4 answers
8k views
Is it a bad idea to do validation in the constructor?
Consider the following approach to validation of an API class (POJO or what have you, I mean a class which just acts as a container for some properties), we make all constructors private to the API ...
5 votes
7 answers
982 views
Fail-fast design vs. limiting constructor logic
In any programming task, my preference is to write fail-fast code. That doesn't seem to be too controversial. However, I've also seen many developers say that constructors should do as little as ...
2 votes
5 answers
659 views
Do db calls in constructors lead to more DRY code?
It recently came to my attention that its best practice to avoid database calls in constructors. I feel like this means you end up repeating unnecessary code, thus the code is less DRY? For example, ...
1 vote
3 answers
831 views
Is calling .sort() in the constructor a violation of the guideline that a constructor shouldn't do work?
Suppose I have the following List to hold a list of fruits. Example: def fruits = ["Apple", "Orange", "Grapes"] def fruitsBowl = ["Apple", "Grapes", "Orange"] // Will print false println(fruits....
0 votes
2 answers
2k views
Do you usually instantiate dependency objects in the constructor?
I know that we usually inject the dependencies by instantiating them in the constructor of the class we are injecting to. However, in my own experience, I just pass the class of the dependency as a ...
0 votes
4 answers
272 views
Is going through network during DI anti pattern?
So I want to inject an Application Client, say a Rest Api client, in my code. In order to create this client I need to go over the network to get it's user and password that is in a HTTP Vault. This ...
-1 votes
1 answer
119 views
Doing work when passing constructor arguments
This is very closely linked to the question Legitimate "real work" in a constructor? but not quite the same. I am interested in having feedback on whether this is acceptable or has any risks....