A special type of subroutine called at the creation of an object.

learn more… | top users | synonyms

1
vote
2answers
59 views

Write a class with some properties and indexed

Is there a better way to write it? I need the best way to write it. It is this principle? Please show me a better way it breaks SRP? public class PlacementLocation { public int Row { get; set; ...
1
vote
1answer
33 views

__constructor mysqli variable options

I like the fact that I can create a class and have the connection details in one file if I need to update them and then pass them to the method I create in the class that defines my specific use of ...
4
votes
2answers
116 views

Constructor Chaining in Java

When creating multiple constructors, I usually do the following; public ConstructorName(int count) { this(count, 0); } public ConstructorName(String count) { this(Integer.parseInt(count), ...
1
vote
2answers
52 views

JavaFX design writes to static field from constructor

In JavaFX, the lifecycle of an Application is quite different than Swing. Your constructor must take no arguments so that the Application class can create an instance of your object. Once the instance ...
2
votes
2answers
88 views

Is this a correct way to write a convenience constructor?

I was reading more about the instancetype on Stack Overflow and now my question is: For every convenience constructor that I write, do I need to have the corresponding one using init? For example: ...
1
vote
1answer
64 views

Dynamic object creation in Python

I want to abstract away differences between zipfile and rarfile modules. In my code i want to call ZipFile or RarFile constructors depending on file format. Currently i do it like that. def ...
1
vote
0answers
65 views

Changing states of a game character and constructor signature

I have an Actor class who represents the player and the NPCs of an game. The Actor have an StateMachine object who manages the push and the pop of the states of the Actor object. The Actor have only ...
1
vote
2answers
50 views

Changing the state of another entity into the constructor method

I have a Clock class who inherites from pyglet.clock.Clock and behaves like a Singleton. Into the constructor method I have the following line: pyglet.clock.set_default(self) Who changes the state ...
1
vote
1answer
122 views

Java constructor should use setter?

I have a simple Java model Class called Person which have a constructor that receives a JSONObject. In that constructor I get the values from the JSONObject and put in my instance variables. This is ...
2
votes
3answers
118 views

Python: How much logic is enough logic, and how much is too much logic in an __init__ method?

I am writing an email parser and cannot decide how much logic should be included in the __init__ function. I know, a constructor should make the object ready for use, but are there best practices to ...
0
votes
2answers
252 views

Constructor with too many arguments [closed]

We have a Transaction class that's very loaded; so loaded that I originally ended up passing almost 20 argument to the ctor. After extracting a few value objects, there are still 12 arguments left, ...
5
votes
3answers
196 views

Calling functions (or invoking methods) in constructor

I had written something like the following, public class ClassName { //private instance variables public ClassName() { //initialize instance variables this.someFunction(); } public void ...
2
votes
3answers
372 views

Self populating view models

So I have a view model (asp.net mcv 3 app) that when instantiated with a particular parameter, populates it's properties via a couple of calls to a web service in it's constructor. public class ...
1
vote
2answers
117 views

Get resources inside or outside constructor?

I need your opinion on writing a constructor that's clean code and unit test friendly. Option 1 : var res1 = bigObject.GetResource1(); var res2 = bigObject.GetResource2(); var res3 = ...
5
votes
1answer
575 views

Constructors and inheritance in Groovy

I am new to Groovy and I am having a little problem with constructors of subclasses. Basically, I have a base abstract class like class BaseClass { def BaseClass(Map options) { // Does ...
0
votes
2answers
78 views

May be a memory leak somewhere or allocated memory not freed? [closed]

Here is the entire program, please help me, I've tried everything to find out what exactly is going with the memory. The problem is everything runs perfectly, but there are some extra characters ...
2
votes
1answer
105 views

Assign local-variables in constructor

How to make this code in constructor more DRY? Is there some more concise way to assign variables passed through object? class Ball constructor: (args) -> @name = args.name @x = ...
3
votes
1answer
174 views

How could this constructor code be improved?

How could this constructor code be improved? I feel like I'm not doing something optimally here... @interface JGProduct : NSObject { NSString *identifier; } +(JGProduct ...
0
votes
0answers
111 views

“metaconstructors” for inheritance in js?

Edit - decl smokes dojo.declare - http://jsperf.com/dojo-declare-vs-decl The code has been updated a bit. decl now accepts object literals as well, although I still like "declaration functions" ...
3
votes
2answers
2k views

MySQL database connection in the constructor

I'm an absolute beginner on PHP OOP in search of the "Holy Graal" of connecting to MySQL database once and reuse this connection for the whole site So far, I've created these files: classes/db.php ...
11
votes
2answers
3k views

Styles: “object initializer” vs “constructor” object creation

Question: What are peoples preferences between the two methods of creating an object (in C# in particular) and setting other fields (that may not be given values during construction)? Are either of ...
5
votes
6answers
1k views

Constructors - lots of paramters or none and use of set methods

In your opinion what is the best way to construct an object given the two examples below: Option 1: Lots of parameters private string firstname; private string surname; private Address homeAddress; ...
1
vote
2answers
309 views

How to improve this basic bit of PHP code

How to improve: class gotClass { protected $field1; protected $Field2; protected $field3; (...) function __construct($arg1, $arg2, $arg3, $arg4) { $this->field1 = ...
7
votes
2answers
1k views

Workaround for overloaded constructor in PHP

I have a class whose purpose is to display a comment. I'd like to be able to instantiate it by passing a Comment object if I happen to have it available, or just the values if that's what I have ...
9
votes
6answers
364 views

Arguments in constructors matching fields

What is your opinion in arguments in constructors matching members as in the following example public Join(final int parent, final TIntHashSet children) { this.parent = parent; this.children = ...
25
votes
5answers
887 views

Instantiating objects with many attributes

I have a class with quite a few attributes, most of which are known when I create an instance of the object. So I pass all the values in the constructor: $op = new OpenIdProvider($imgPath . $name . ...
17
votes
5answers
3k views

Database connection in constructor and destructor

I am playing with different ways to do database interaction in PHP, and one of the ideas I have been playing with is connecting to the DB in the constructor and disconnecting in the destructor. This ...