Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [constructors]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-2
votes
2answers
103 views

C# Static & Constructor Logic [closed]

I learn C# and try to understand the logic between static and Constructor right now. One thing I need ask you about an example which I will give at below. (please ignore the quality of code or how ...
7
votes
3answers
416 views

Avoiding side effects in immutable class constructor

I rewrote a very long method in which some data is queried from a database, based on info about a particular account, which is queried first. I split out the account info into an immutable inner ...
-5
votes
1answer
2k views

pass a variable as parameter to constructor c# [closed]

pass a variable as parameter to constructor as i want to change the variable value with changeable value i'm new to c# so that i tries to implement one thing in many way i did what i want to do with ...
-3
votes
1answer
159 views

Why constructor inheritance is not supported in Python? [closed]

Consider the case where I have the following classes, class A: def __init__(self): print("Class A") class B(A): def __init__(self): print("Class B") b = B() The expected ...
2
votes
2answers
20k views

How do constructor parameters of a MVC Controller get set?

How does construction work with dependency injection? With the following code: public class AdvancedSearchController : Controller { private EmployeeController _employeeController; public ...
0
votes
0answers
37 views

Neural Networks object creation from OOP perspective [duplicate]

Let's say that I have a class representing the Neural Network. The neural network is composed of three bigger units: a subpart_1, subpart_2 and subpart_3, being called in such a way, that the output ...
4
votes
2answers
3k views

Constructor vs casting operator

I'm programming a library (so I have complete access to all the mentioned classes). Two classes (A and B) are essentially the same and differ only by their implementation, so they can easily be ...
1
vote
1answer
65 views

ES6 - Where is it better to initialize a member - parent class or derived class?

I am trying to figure out if there is a better practice for initializing class members of derived classes in ES6 - in the child or the parent, and why? For example: Option 1: class AbstractAnimal {...
-1
votes
2answers
184 views

Java Constructor Syntax and Design [closed]

With regards to object-oriented Java constructor syntax and design, specifically parameterized or specific constructors, it is the accepted process, as describe in books, to build a parameterized ...
33
votes
5answers
20k views

Should I initialize C structs via parameter, or by return value? [closed]

The company I work at is initializing all of their data structures through an initialize function like so: //the structure typedef struct{ int a,b,c; } Foo; //the initialize function ...
0
votes
1answer
96 views

Unit testing for a method in a class which uses constructor DI (prism)

I have a class that uses constructor DI for IEventAggregator public SomeViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; ...
5
votes
3answers
5k views

Is it okay to start a thread from within a constructor of a class

This is purely a design philosophy question in the context of C++. Is it a okay design philosophy to start a thread from within a constructor? I have a library whose sole responsibility is to ...
-4
votes
1answer
166 views

Inheritance of Constructors from parent into child class [closed]

Is the Default Constructor of Super or Parent class Can be Inherited into Child class ?
2
votes
3answers
382 views

What is the best practice for initializing variables: outside or inside of the constructor?

I try to use the best coding standards/practices, however in all of my googling and classes I have never learned which is proper form for declaring/defining variables like the examples below. I have ...
1
vote
2answers
106 views

Performing serial communication in constructor to initialize an object

I have a class UnitInfo which represents a collection of unit information with methods to get the unit information in a structured way, such as a specific encoding, etc. This unit info consists of ...
4
votes
2answers
206 views

Verifying Parameters in Constructor or Service

I was reading on this SO page about when to check parameters when constructing an object. The accepted answer suggests throwing the exception from the constructor so that an invalid object cannot be ...
2
votes
3answers
781 views

Should a class constructor ever create objects of other classes? [duplicate]

Is it ever good or bad to have a constructor create new instances of classes that it needs versus passing in a reference that you want the new class to have ? So basically it's the difference between:...
1
vote
1answer
245 views

Is using the copy constructor in an object constructor bad? [duplicate]

I was reading this page about using the new keyword in the constructor, and I was wondering if it applies to copy constructors for collections. Suppose I have a Book class and a collection to store a ...
8
votes
4answers
560 views

Checking the result of a constructor in C#

I'm working on a code base with a coworker who has a habit of checking the results of a constructor for a null in a fashion similar to this Person p = new Person(); if (p != null) { p.Name = "...
23
votes
4answers
4k views

Legitimate “real work” in a constructor?

I am working on a design, but keep hitting a roadblock. I have a particular class (ModelDef) that is essentially the owner of a complex node tree built by parsing an XML schema (think DOM). I want to ...
1
vote
3answers
388 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....
4
votes
5answers
5k views

Why can't I call a constructor in itself?

I am currently porting the class NumberRange from Java to C#. I am writing this constructor and I wonder whether I can call a constructor in itself. Something like this: public NumberRange(Double ...
5
votes
1answer
422 views

Approach for Constructing View Models in Complex MVVM Application

I'm struggling with the design in a WPF MVVM application. In a few courses I've taken, they say that having a lot of parameters in a constructor is a code smell, but they never address how to deal ...
2
votes
4answers
561 views

What do OOP languages gain from having constructors that always return an object?

In what seems like a deliberate design decision, C++ does not have a null value for objects and references. This makes using objects and references very elegant since we don't have to perform null ...
37
votes
7answers
12k views

Is using “new” in the constructor always bad?

I have read that using "new" in a constructor (for any other objects than simple value ones) is bad practice as it makes unit testing impossible (as then those collaborators needs to be created too ...
1
vote
1answer
165 views

Am I waiting for the new C# 8 'record' feature?

I'd like an immutable class, or rather a class whose properties are initialised (whether they have public/private setters or not), in the constructor: public class MyClass { public string ...
7
votes
4answers
4k views

Is it bad practice to read large file in constructor?

So, I am trying to create an English language trie data structure implementation in C++. I have created a Trie and TrieNode class. The TrieNode class takes in its constructor a vector<string> ...
0
votes
1answer
142 views

Can I use Dependency Injection to test my class when a parameterless constructor is needed?

I want to use PowerArgs for a console application I am writing. It offers some nice features I'd like to try. My root looks like this: public static void Main(string[] args) { try ...
3
votes
1answer
180 views

Does adding values to a dictionary in the constructor violate that “a constructor shouldn't do work”?

Some what of a followup to this question. Suppose I have a Player class with a dictionary call attributes that holds the Health and Mana of the player. In this example, I'll use Python, but my ...
1
vote
0answers
204 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 ...
3
votes
4answers
2k views

Public static method calls private constructor

I'm working in a C# codebase that was largely written by a former developer and this pattern is used extensively... public class AuditInserter { public static void Insert( DataContext ...
1
vote
1answer
244 views

Does creating a list in the constructor violate the guideline that a constructor shouldn't do work?

I was reading these pages (1,2,3), but I'm still unsure if this violates the guideline. I have the following data being read from a website: Date: July 13, 2018 Type: Partial Solar Eclipse Location:...
28
votes
3answers
10k views

Optional parameters or overloaded constructors

I am implementing a DelegateCommand, and when I was about to implement the constructor(s), I came up with the following two design choices: 1: Having multiple overloaded constructors public ...
-4
votes
1answer
126 views

Can a constructor look like this in Java?

I wonder if a constructor in Java can look like this: public double[] values; I'm not sure if the constructor can look like this, or should it be public values (double[] values){ values = ...
2
votes
3answers
566 views

Giving a class many constructors and assigning via them as many properties as possible

I have written a class which represents a SQLite Trigger. public SQLiteTrigger(string Name, string On, TriggerStartType StartType, ...
4
votes
2answers
500 views

How do we freeze an object while constructing an object using JavaBeans pattern?

Going through Effective Java, Joshua Bloch states that the drawback of using JavaBeans pattern is that an object may be in an inconsistent state partway through the construction and with JavaBeans ...
4
votes
2answers
116 views

How to separate construction and runtime logic in iOS?

I've been reading Robert Martin's Clean Code book where it suggests we should separate a programs startup/construction process from its run time logic. In Java (the language the book uses) this ...
4
votes
2answers
380 views

Where should I put the common code of the constructors?

I have a situation where in a class I have 2 constructors and they have very similar code. The only difference is the the call to the constructor of the super class. Where should I put this common ...
1
vote
1answer
184 views

Passing 0's (literals) to a constructor

I have a function that creates a new object by passing to it's constructor integer ids primarily with values of 0. This function is called when saving a newly created record (not an edit). public ...
11
votes
2answers
4k views

Unit Test to test the creation of a Domain Object

I have a Unit Test, which looks like this: [Test] public void Should_create_person() { Assert.DoesNotThrow(() => new Person(Guid.NewGuid(), new DateTime(1972, 01, 01)); } I am asserting that ...
2
votes
4answers
11k views

When to call the constructor and when to call the method in Java?

I am struggling to fully understand the usage of constructors in Java. What I have learned so far about constructors is the following: same name as class abbreviation ctor overloading no return type ...
0
votes
6answers
647 views

why can't class directly take arguments instead of using a constructor?

I referred this page for understanding about constructors in Java: http://www.javatpoint.com/constructor So, I pretty much got it as in my own words: constructor has no return value and is used to ...
18
votes
3answers
11k views

how complex a constructor should be

I am having a discussion with my co-worker on how much work a constructor can do. I have a class, B that internally requires another object A. Object A is one of a few members that class B needs to do ...
-1
votes
3answers
387 views

Constructor from interface with minimal repetition

Preface In an application that is separated by layers or distributed by services it is common to have classes that are very closely related data-wise, but which we wish to have loosely coupled. My ...
0
votes
1answer
3k views

Object constructors with dynamic parameter lists

I had a quick question and I was hoping someone could help me figure this out. I'm new to Java and I'm trying to learn about classes and objects and I see you can call parameters in the constructor of ...
0
votes
1answer
158 views

Constructors or setters for entity classes? [duplicate]

I know this has been asked a lot of times but we are talking about entity classes here. Plain dumb objects containing nothing but primitives for properties. The purpose here is to store data. Our ...
3
votes
1answer
1k views

List initialization by empty braced-init-list (value-initialization or by initializer_list) vs explicit default initialization, for “list” types

Background Based on Item 4 in Scott Mayers Effective C++, when appropriate, it can be good practice to initialize all member properties of custom types via the member initialization list (in same ...
0
votes
1answer
71 views

Identifying the use case for instantiation only through existing instances

I was in my CS class when my instructor presented me the following classes: public class UninstantiableTest { private static Uninstantiable uninstantiable; public static void ...
5
votes
5answers
2k views

Should constructors ever be used only for side-effects?

Summary: Why is it wrong to design a constructor only for its side effects, and then to use the constructor without ever assigning its return value to a variable? I'm working on a project that ...
1
vote
1answer
123 views

Instance of other class within class, instead of constructor instance variables?

I was watching an OOP lesson with such code: class UsersConstroller { protected $userService protected $logger public function __construct { UserService $userService, ...