Questions tagged [constructor]

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

Filter by
Sorted by
Tagged with
3
votes
1answer
5k views

Reading and storing values from JSON

I have a class ConfigFile that reads some values from a JSON using Boost and stores them in variables. Everything is done in the constructor: ...
8
votes
3answers
4k views

Count PDF pages in constructor

I have an object that represents a PDF file. In the constructor, I pull out various information about the file name and make it available via properties: ...
6
votes
2answers
3k views

Storing a message from a queue

I am doing a code review and I have encountered a class in an application that is throwing an exception in the constructor: ...
8
votes
2answers
1k views

Should I set a license in the constructor or a static constructor?

We are using the Aspose PDF library to work with PDF files. Aspose requires the license to be set once per process. Accordingly, I have created the following common static method that sets the license....
4
votes
2answers
842 views

Exercising in copy constructors and assignment operators

I've just created an implementation of array to train myself in copy constructors, assignment operators, and exception safety. Please look at this code and tell me what is wrong here. ...
4
votes
1answer
71 views

Zip-like construction of objects from parameter collections

This little function can make object creation much easier. Sometimes you need to switch between representing your data as objects vs. having the members in separate containers. Example: ...
23
votes
5answers
2k views

When a constructor calls a helper function, should the helper mutate the object or return part of the object?

I have a private method that is called from the constructor to instantiate a bunch of objects. I can do it two ways. ...
7
votes
2answers
3k views

What is the advantage/disadvantage of using default initializer for private fields?

Let's compare the following code: 1) ...
10
votes
2answers
3k views

Long constructors, inheritance, class method constructors

My class takes functions as a parameter. There are 6 of them, along with numerous other parameters. Almost all of the fields have default values. It also has a class method that creates an instance ...
3
votes
1answer
77 views

Framework constructor

I'm working on my framework constructor. First I tried to look into jQuery and understand it, but it's so out of my league. Instead I googled and put this code together. Though there aren't many posts ...
3
votes
2answers
100 views

Login field validator

I have this basic JavaScript that, using a constructor, validates the form where the login fields can't be empty. All this WITHOUT using any JS libraries. ...
2
votes
2answers
157 views

Improve testability of constructor

I have the following constructor ...
2
votes
3answers
2k views

Review of three constructors for a String class

I have the following declaration in my String.h file: private: char* nstring; int nlength; }; The following are three constructors I've implemented in ...
4
votes
4answers
187 views

Reference type and constructors [closed]

Code in Java but should be readable also for c#... Let's assume I have a class with some reference types. Example: ...
1
vote
1answer
164 views

Cleaning up PHP object constructors

I have this annoying problem in PHP with faking overloading constructors. I understand the concept, but the code I produce feels kind of ugly. It isn't a lot of code, just bad code. Any ideas on how ...
1
vote
2answers
85 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? ...
1
vote
1answer
577 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
259 views

Constructor Chaining in Java

When creating multiple constructors, I usually do the following; ...
1
vote
2answers
3k 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 ...
3
votes
2answers
4k 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 ...
1
vote
1answer
308 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 ...
-1
votes
2answers
170 views

Checking whether a comment contains text

Is this code good for checking if the variable texto is empty or not? How would it be done with try and catch? ...
3
votes
2answers
103 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: ...
1
vote
1answer
251 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 ...
3
votes
3answers
962 views

Logic for an init function in email parser [closed]

I am writing an email parser and cannot decide how much logic should be included in the __init__ function. I know that a constructor should make the object ready ...
6
votes
3answers
5k 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. ...
1
vote
2answers
162 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 : ...
12
votes
1answer
15k 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 ...
3
votes
1answer
231 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... ...
1
vote
1answer
167 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. ...
5
votes
2answers
21k views

MySQL database connection in the constructor

I'm an absolute beginner in PHP OOP in search of the "Holy Grail" of connecting to MySQL database once and reusing this connection for the whole site. classes/db.php ...
14
votes
3answers
16k 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 ...
7
votes
6answers
6k 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 ...
2
votes
2answers
2k views

Subclassing UIButton

I need to subclass UIButton, however the design of UIButton makes this really painful. This is because to create a button you ...
7
votes
1answer
4k 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 ...
6
votes
6answers
457 views

Arguments in constructors matching fields

What is your opinion in arguments in constructors matching members as in the following example ...
45
votes
7answers
2k 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: ...
40
votes
5answers
10k 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 ...

1 2
3