Questions tagged [constructor]

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

4
votes
1answer
34 views

Super fancy list constructing in Python

Out of curiosity I was discovering the potential of the Ellipsis object in Python (...), and I discovered it doesn't do a whole ...
2
votes
1answer
44 views

Constructor for a packagetarget struct

As many of you know goto is usually signs of code smell. However I thought this could be an appropriate case, and would like confirmation or criticism. Unnecessary ...
4
votes
2answers
124 views

Implementing a c++ Kafka producer based on librdkafka

To practice my c++ I decided to implement a simple Kafka producer to wrap the producer in c in librdkafka library. I just want to get your opinion on the way I have implemented teh default, copy and ...
3
votes
2answers
76 views

Constructing a cumulative distribution function from a histogram

During some running process, a histogram of values shall be build up. When done, the CDF shall be derived from it and used to get the quantiles for some values. My current implementation looks as ...
0
votes
3answers
65 views

Represent a Class which holds translation Strings [closed]

I have a class which represents some translations of a keyword. ...
5
votes
2answers
143 views

C++ wrapper class to mimic a C array's brace initialization

I have an inheritance class that mimics the behavior of C style array's brace initialization by using a class template that has a variadic constructor but the template itself is not a variadic ...
2
votes
1answer
69 views

C++ - Astar search algorithm using user defined class

The objective of the post is to improve design and improve command on C++. Given a map, start point and end point, the shortest path to end point from the start point has to be found out using Astar (...
3
votes
1answer
114 views

Load and update XML inside the constructor

My requirement is, I need to load the existing xml file from the given path by passing through command line arguments and need to update the xml file from C# code. First time I am working on the C# ...
0
votes
2answers
290 views

Fetching data from your database in a constructor

I have this item (an item is an object linked to a xml view), to build it I need 2 things, get labels from codes, and get text from the strings.xml android file. please ignore the long to int and int ...
-1
votes
2answers
129 views

Constructors with variable number of arguments [closed]

Assume that we have a class with multiple constructors which may look like the following: ...
-1
votes
2answers
63 views

Healthy Characters

Let's say I have an abstract class Character that have fields health and maxHealth. maxHealth...
3
votes
1answer
90 views

Representing destinations in a square

I've written a program following the instructions of an exam paper. There are no solutions available so I would appreciate a feedback. I'm concerned with main in ...
4
votes
1answer
74 views

Designing the constructor interface for a reflection object (any class)

I am working on cleaning up and making open source a C++ reflection library that has served me well over the last few years. One of the most important classes of the library is ...
1
vote
4answers
131 views

Java Date class- switches and ifs in the constructor

I'm a beginner i have an assignment to write a Date class (as part of a bigger project). in my question i focus on the constructor. here's some background: the given guidelines are that the date is ...
1
vote
2answers
105 views

Class for course information

Here's what the question asks: Build a class called Course.java. This class should have 4 properties: CourseId, CourseName, Description, and credit hours. Also add the appropriate set and get ...
1
vote
1answer
2k views

Testing async method call from constructor

I have a project where I want to build a more sophisticated ToDo list - basically a personal project management system. I'm just starting out with the project, and I'd like some feedback on whether my ...
3
votes
1answer
465 views

Character class with name, gender, and inventory list

I am trying to create a class to test my understanding, and it takes an ArrayList as parameter. The decision of choosing an ...
3
votes
4answers
104 views

Simple addition calculator accepting addition expressions

Maybe someone can help me to optimize the calculate method. I think that it can be simplified from that what I have now. The objective is: grab a string via the ...
0
votes
2answers
57 views

Ensuring a non-zero radius when constructing a circle

Is there a little "better" way of writing the line: raise RuntimeError if not radius.is_a? Numeric or radius == 0 I want to only allow numbers bigger than 0. <...
2
votes
2answers
87 views

Elegant way to “construct” kind-of enum values in go

I'm quite new to Go and would like to improve my code. I have a representation for access levels in Go like this: ...
4
votes
2answers
186 views

Request dispatcher for PHP JSON web service

Below you can find the constructor method and related fields of the main class of my REST API (note that the class contains more code than displayed) written in PHP 7.1. It does various things with ...
0
votes
1answer
1k views

Initializing values of a singly linked list with a constructor [closed]

I have to create a constructor that allows a new linked list to be populated with ten consecutive values, starting at 0. Then I need to print the list! So i want to check if the functions I wrote for ...
0
votes
1answer
107 views

Controller with too many parameters with DI [closed]

Is there a way to reduce the number of parameters of this constructor? All the services must be used in this Controller. The majority of them are used more than 2 times(some of them >10), with the ...
3
votes
3answers
2k views

OOP modelling of a car agency

Exercise: A car dealer wants a computer system to manage the data of their vehicles and classify them by type. All cars have the following data Engine serial number Brand Year ...
2
votes
1answer
1k views

Factory using Structuremap to inject concrete implementation of BaseTask<T>

The problem statement that I am trying to solve is this; We will retrieve a list of Tasks from an external repository. These tasks will have a type and for each type, there needs to be a specific ...
5
votes
1answer
717 views

Simple c++ triple class to mimic pair for use with uniform initialization

I'm trying to create a simple triplet class, but the more I look at the implementation of std::pair the more I feel like I'm missing important details. What I have ...
5
votes
1answer
745 views

Counting the number of unique fractions read from a text file

I will begin this question with the disclaimer that it is homework but my code is written and working, I just want to know if there any ways in which I can improve what I already have. I want my ...
2
votes
3answers
112 views

Message class, with constructors for data from user and from database

I'm fairly new to Kotlin, and am looking to slim down a data class. It has two constructors, one for creation based on user input, and one for creation based on database data. ...
0
votes
2answers
58 views

Stateful User tracking

I have a class that takes a string or an integer, which stand for username or user id in the database. I am not comfortable with my constructor taking "a string or an integer" and want to modify it ...
2
votes
1answer
38 views

Validating inputs in the constructor [closed]

Let's take the following example. Even if the number of parameters is acceptable, I find that the code block is too long. Are these validations at the right place? Is there a better approach in such ...
11
votes
3answers
876 views

Immutable Object class in VBA – Creatable only through constructor and not via “New” keyword

Goals for the class Create Immutable objects – i.e. only Getters – no Setters Object creation only possible through a constructor, not via New keyword, to ensure ...
1
vote
1answer
44 views

SVG class with boilerplate prologue and epilogue from files

I noticed this old class of mine: ...
1
vote
1answer
27 views

Class for Stripe transactions

I just wonder whether it is a common approach to hit the third-party API and fetch values within the initialize method? Ex. ...
2
votes
0answers
561 views

OpenGL Shape class constructor

I'm coding a wrapper for OpenGL 3.3, and I have some questions about the design of my Shape class. To be short, It's a class that englobes a VBO, a VAO, and an EBO ...
22
votes
10answers
4k views

Verifying a person's age

When coding I noticed I started to get into a habit of not using if-else statements when the else block only has one line of code. For example, if I have code that can be solved like this: ...
1
vote
1answer
2k views

Runs simple menu driven ATM program with various option in Java

RunMyAtm ...
3
votes
3answers
123 views

Constructor for a Time object

I am trying to write a constructor method that takes dd, hh, mm and ...
4
votes
1answer
193 views

JSON loader with event bus, cache, and session

I have an Android application which needs to retrieve JSON files from a server and then display the information in my app. To accomplish this I have the following architecture: The main activity adds ...
1
vote
1answer
70 views

Multiway Ruby conditional

I'm a little unhappy with the look of this "flexible" way of dealing with errors from an API in Ruby: ...
7
votes
1answer
2k views

Primitive Type Wrapper in C++

In C++, primitive types are treated differently than user-defined types. If you do not initialize them, their values are undefined in some cases. This is an easy mistake to make. I wanted to make a ...
4
votes
1answer
2k views

Class decorator in Python to set variables for the constructor

I personally don't like the boilerplate of most __init__ methods: self.a = a self.b = b ... So I thought this would be a nice ...
4
votes
0answers
146 views

2d game enemy entities factories, probably in need of refactoring

So I'm making a 2d shoot'em up game as a way of learning canvas, and larger-scale programming than what I'm accustomed to. I'm a purely front-end web dev, I can't say I'm very experienced in this. ...
2
votes
1answer
100 views

Inheritance and small number of parameters

Uncle Bob in his Clean Code says: So all the same rules apply. Functions that take variable arguments can be monads, dyads, or even triads. But it would be a mistake to give them more arguments ...
2
votes
1answer
1k views

Java member method initialization pattern

To avoid the overridable method call in constructor dilemma this approache delegate the variables initialization and calls to overridable methods to a member method that return reference to the ...
3
votes
2answers
92 views

Throw exception in Spring when loading a file

I have this following piece of code: ...
5
votes
1answer
560 views

Python 3 class constructor

I have implemented a metaclass in Python 3 that, apart from the usual instance constructor (i.e. __init__), enables you to define a class constructor (as a class ...
0
votes
1answer
1k views

C++ lazy constructor

I'm implementing a lazy constructor in C++. The goal is that for a type T, lazy<T>(args...) returns a callable object ...
3
votes
1answer
225 views

C# data class with public auto-implemented properties with constructor that specifies each

I have a dll and an executable which both deal in configuring certain things for a much broader software solution, and the code below in particular is directly meant for database configuration. (Here ...
3
votes
2answers
405 views

A PHP MVC controller to display a form and add an item to the database

I am creating a controller that will display a form and once the form has been submitted to store the item in the database. After the item has been stored the user will be redirected back to the main ...
4
votes
1answer
5k views

Emulating constructor overloading in Typescript

I'm writing a Matrix class, and I want to be able to construct a Matrix in two ways: Supply a 2D array that the Matrix can use internally Supply the dimensions of the Matrix, and the value to fill ...