Questions tagged [constructors]
Member functions of a class which initializes objects of the given class.
153 questions
4 votes
4 answers
467 views
Primary constructors for depedency injection
Microsoft identifies dependency injection as one of the uses for primary constructors. public class ExampleController(IService service) : ControllerBase { [HttpGet] public ActionResult<...
0 votes
1 answer
193 views
Assigning to fields in function or by function?
While writing the constructor for class A in Python, I calculate some of the class's fields using function fun(). This function is never used outside the constructor of instances of this class. In ...
3 votes
3 answers
561 views
How to efficiently build objects without default constructors
I am trying to explain and understand a way to create object which cannot have "default" instances. Typically I want classes which represent, as closely as possible, the real-life concept I ...
-2 votes
3 answers
235 views
How to signal a dealbreaker error from a c-tor?
Context: 128kB RAM, freeRTOS. Considered solutions: Exceptions. Discouraged by both the memory size and the code style guide. Late bool init(...);. Has worked for a decade but has it's problems - can ...
2 votes
5 answers
657 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 votes
1 answer
214 views
Conversion methods: from_xxx() or to_xxx(), is there a reason I shouldn't stick with to_xxx()?
I have a C++ library that I'm converting to Python. In the C++ library I have multiple constructors and many different types of conversion functions (think radians to degrees, and different types of ...
1 vote
2 answers
516 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 ...
-1 votes
3 answers
661 views
When and how to check input parameters
My SmsRecipientDetails class constructor accepts String value as recipient phone number. I would like to accept number with spaces: assertDoesNotThrow(() -> new SmsRecipientDetails("123456789 &...