The tag has no usage guidance.

learn more… | top users | synonyms

1
vote
1answer
40 views

Pass data into a constructor or instead create virtual methods?

In an object oriented module describing a database, should I pass DB description data structures to the constructor in the constructors of derived classes, or should I instead create ("virtual" that ...
-6
votes
1answer
75 views

Why ? Constructors cannot be declared as final? [on hold]

Why final keyword is not working with constructors in java? I'm a bit confused with this, but it works fine when declared with methods. What's the reason for that? class NewClass{ int speed = 40; ...
0
votes
1answer
76 views

Should I put the parameters in constructor or in method? (Python 3)

I have the following code: def __init__(self, vocable_file_path, xsd_file_path, word_list_file_path): self.vocable_file_path = vocable_file_path self.xsd_file_path = xsd_file_path ...
27
votes
5answers
2k 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 ...
6
votes
4answers
295 views

Can I make my constructors less repetitive?

I'm extending a class with 10 different constructors. The new subclass, SpecialImage, is used like this: SpecialImage specialImage = new SpecialImage(..); // Leverage the Rotate() method of ...
2
votes
2answers
234 views

interpretation of the statement in java

I am confused over the interpretation of the following statement Fruit x= new Fruit(); My interpretation is that the new operator along with Fruit(); creates a new object somewhere in the memory. ...
1
vote
2answers
129 views

Should constructors for API classes ever be public, or can a factory always be used? [closed]

I'm surprised this doesn't seem to have been asked before, or at least I can't find it anywhere. I know this is more of a philosophical question, but is there any particular reason not to use a ...
0
votes
2answers
104 views

Usage of objects in multiple containers in C++

In cases where there is a desire for objects to be "shared" between multiple containers, I would like to know what is the best practice in C++. For example, when implementing some path algorithms ...
-2
votes
1answer
106 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 ...
0
votes
1answer
189 views

C# Constructor and private LINQ to SQL members

I am extending an application I have developed so that it is more broadly useful for multiple jobs, rather than the single job I created it for. There are a number of tables that I get from SQL ...
0
votes
1answer
115 views

Multiple method calls in the constructor and dependency injection

I was asked to refactor some almost ureadable spaghetti code into object-oriented architecture. I have some doubts regarding a class that I designed. Here is the class' skeleton: require_once ...
0
votes
1answer
114 views

How can I condense a case class with ~20 constructor arguments into 5 or 6 arguments in Scala?

Let's say I have a case class with ~20 constructor arguments? This is obviously very clunky to type. What would be the best way to condense these arguments into maybe 5 or 6 arguments? There are some ...
14
votes
4answers
1k views

How does one keep argument counts low and still keep third party dependencies separate?

I use a third party library. They pass me a POJO that, for our intents and purposes, is probably implemented like this: public class OurData { private String foo; private String bar; private ...
-3
votes
1answer
94 views

How should I change in get method without calling getList() of Deque? [closed]

import java.util.List; import java.util.ArrayList; public class Test { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { ...
2
votes
2answers
120 views

Initialize physical resources in constructor

Is it an acceptable practice to initialize physical/external resources from a constructor when the resource is needed for the object to do it's work? For instance, let's say I wanted to create an ...
1
vote
1answer
234 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 be easily ...
0
votes
1answer
57 views

Query on usage of 'Window()' default constructor from java.awt

I had been through this query before asking this question. In class Window we have constructor with default direct access level package private but not private as shown below: Window() throws ...
1
vote
1answer
151 views

Patterns for subclass constructors that vary the parent class constructor slightly

So, my problem is in the context of an MVC-style approach. The code here is PHP, but I'm hoping this is a design issue independent of it. abstract class Controller { private $domain; private ...
38
votes
7answers
8k views

Why do people nowadays use factory classes so often? [closed]

I've seen the history of several С#/Java class library projects on GitHub and CodePlex, and it seems like a trend. Why do people nowadays use factory classes so often for almost everything? I.e. We ...
5
votes
1answer
393 views

Decoupling when constructors have non-contractual parameters

By non-contractual parameters, I mean parameters that are not interfaces or service dependencies, something like class Person(string name). I am writing a webpage scraping application, and so far ...
2
votes
2answers
167 views

Wrapping constructor arguments

Given a base class with around 25 or so subclasses, I have found that adding an argument to the base constructor is painful. protected AbstractController(Service1 s1, Service2 s2, Service3 s3){ ... } ...
0
votes
1answer
102 views

Constructors, Ignore Arguments

Is it possible to have a constructor in a class, that you can ignore certain arguments/parameters? For example, I have this constructor Car(color, make, model, wheels, type, doors) Could I call ...
14
votes
1answer
892 views

Is it fine to make a default constructor unusable?

Specifically asking about the default constructor Given that the constructor initializes all the data for an object, if I create a class that can't be used without proper initialization, is it not ...
16
votes
5answers
2k views

How can you decompose a constructor?

Lets say I have a Enemy class, and the constructor would look something like: public Enemy(String name, float width, float height, Vector2 position, float speed, int maxHp, int ...
4
votes
3answers
270 views

Where should an object in CQRS+ES be fully initialized: in the constructor, or when applying the first event?

There appears to be widespread agreement in the OOP community that the class constructor should not leave an object partly or even fully uninitialized. What do I mean by "initialization"? Roughly ...
6
votes
2answers
2k 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
vote
3answers
504 views

Explicitly writing default empty constructor

Does it make sense to write default constructor when it has no arguments, empty body and no other constructors exist? The only benefit I can think of is reducing the risk of forgetting to add the ...
5
votes
2answers
330 views

Constructor Injection, Single Responsibility and List initialisation

Given the class: public class Foo : IFoo { private IBarRepository repository public Foo(IBarRepository repository) { this.repository = repository } public IList<IBar> Bars { ...
3
votes
1answer
1k views

Initializing properties that aren't parameters in a constructor: alternatives

A class loads properties from a file and uses those as it performs its duties. For testing purposes I want to be able to modify said properties directly without having to use a test properties file. ...
3
votes
3answers
698 views

Is it better to have constructors with or without parameters?

Is it better to have constructors with or without parameters and why? public NewClass( String a, String b, int c) throws IOException { //something } OR public NewClass() { //something } ...
1
vote
1answer
131 views

Writing arguments in constructors

When creating constructors with multiple arguments, is ok to use the generic String[] args or is it better to list the arguments? Does it really make any significant difference as the coding becomes ...
4
votes
5answers
532 views

Requring static class setter to be called before constructor, bad design?

I have a class, say Foo, and every instance of Foo will need and contain the same List object, myList. Since every class instance will share the same List Object, I thought it would be good to make ...
-4
votes
4answers
494 views

Why must a constructor's call to the superconstructor be the first call?

It is an error if you do anything in a constructor before calling the superconstructor. I remember that I had problems because of that. Yet, I do not see how this saves us from errors. It could save ...
3
votes
3answers
4k views

How to initialize all your references?

I have recently taken a project with another developer, and he has a certain way of initializing his references. class Player { private: Console &console; Armor &armor1, ...
29
votes
3answers
2k views

Is a “start”, “run” or “execute” method a good practice?

I'm currently working on a code base that has many classes that implement a Start method. This seems like two-phase construction to me, which I had always considered a bad practice. I can't tell the ...
4
votes
6answers
789 views

Should all classes have a default constructor as part of good coding convention

From the texts I have read so far, the conventions talk about organizing constructors, starting with the default, if any. I am wondering, should all classes have a default constructor anyway. This ...
5
votes
2answers
3k views

Why multiple calls to same classes' Constructor from within a Constructor wouldn't work?

Please have a look at following snippet: public class Foo { int digit; String name; Foo (int d, String n) { this(d); // cannot do following. //compile-time error: Constructor call must ...
22
votes
10answers
12k views

Constructor parameter validation in C# - Best practices

What is the best practice for constructor parameter validation? Suppose a simple bit of C#: public class MyClass { public MyClass(string text) { if (String.IsNullOrEmpty(text)) ...