Skip to main content

Questions tagged [initialization]

Filter by
Sorted by
Tagged with
6 votes
4 answers
1k views

How to avoid init methods when 2 objects need the reference of each other?

According to https://softwareengineering.stackexchange.com/a/334994/432039, I know init is a code smell and should be avoided, and one of the solutions is to use a builder to hold the state first ...
wcminipgasker2023's user avatar
12 votes
4 answers
3k views

How to initialize the same global resources from multiple modules independently?

I encountered the following situation: I have two modules, TokenService and Wifi which are initialized from main(). The modules themselves don't know of the existence of each other and function ...
glades's user avatar
  • 419
0 votes
1 answer
248 views

Best way to accept a lot of user input from command line

I have a program that accepts computer names and then will perform CIM session tasks. The computer names are passed in from the user input and separated by "," (ex: program.exe -computers ...
James's user avatar
  • 11
3 votes
3 answers
1k views

Is there any performance hit associated by the definition of a static constructor or due to availability of it?

This question is mostly related to the way language implementer do implements static class initalization (pretty specialized question). It is for curiosity and also to potentially improve my code to ...
Eric Ouellet's user avatar
0 votes
0 answers
419 views

Ensure that library has been initialized

I am using a 3rd party library that must be initialized with Lib::init() before any of its other functions may be called and that must be uninitialized with Lib::destroy() before the application ends. ...
pschill's user avatar
  • 1,990
2 votes
1 answer
115 views

Modelling seats of a table in a social game

Assume we want to model a table where players can sit down to get together to play a game (card games, dice games, ...). Assume a few properties associated with each seat class Seat { public int ...
Benj's user avatar
  • 169
-5 votes
2 answers
219 views

What is the proper way to unspecify an integer's value in C++? [closed]

// Default initialization int i; // i has an unspecified value return i; // Probably 0, but Unreliable i = 5; // i has a specified value i = int();// This will give it a specified value, 0 i = ...
Anon's user avatar
  • 3,613
0 votes
1 answer
839 views

Python - Type Hinting specific sized Vectors

Considering a class method that takes a "vector" (Tuple or List of either int or float) of defined values such as the following: import sys from numpy import isnan, array, float64 class Shape: """ ...
lucasgcb's user avatar
  • 365
4 votes
2 answers
1k 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 ...
user avatar
1 vote
2 answers
5k views

Calling a static method from constructor's member initializer list

I'm implementing a simple rendering system for a game engine. In my engine I have renderable entities that have a Model component (I'm using inheritance as opposed to a ECS for my engine for now, but ...
Luca's user avatar
  • 181
1 vote
1 answer
197 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 ...
samus's user avatar
  • 475
1 vote
1 answer
560 views

Can you explain the behavior of PHP in cases when a parent class variable is masked by the child variable of the same name? [closed]

See the comment inside ChildEntity ::__construct(): class ChildEntity extends ParentEntity { /** @var int */ protected $classParameter; function __construct(int $classParameter) { ...
Dennis's user avatar
  • 8,247
2 votes
2 answers
213 views

What is the meaning of injecting a component in programming?

Components common to all ExoPlayer implementations are: A MediaSource that defines the media to be played, loads the media, and from which the loaded media can be read. A MediaSource is injected via ...
Mehdi Haghgoo's user avatar
3 votes
4 answers
279 views

Advice for bugfixing object oriented code: why is data not set?

When debugging object oriented (Java) code, sometimes some field of some object is null while it should not be. I often spend what I feel is way too much time trying to figure out where it should have ...
Mark's user avatar
  • 662
13 votes
4 answers
17k views

C# - Initialize DTOs in constructor or via properties?

Update: C# 9 should fix this problem using records :D I was wondering if there is a recommended approach to initializing the properties of a plain object that is used for data transfer, for example ...
AyCe's user avatar
  • 408
2 votes
3 answers
272 views

How to indicate that an application has been started before?

I'm building an application with a database where I'd like to populate the DB with some data at first start up (so that the user has a few examples of how the application can be used) To achieve this ...
sunyata's user avatar
  • 459
4 votes
1 answer
1k views

Assignment with brackets

On another SE site I saw code similar to this: int x[] = {1,2,3,4}; .... x[0] = {99}; For the assignment, is there an advantage to this over simply: x[0] = 99; http://ideone.com/xwA0Yh
001's user avatar
  • 273
5 votes
2 answers
3k views

What can I infer from someones code when they use direct initialization with Curly Braces as opposed to Parenthese?

Up until now, I have been initializing most of my variables as such: const QString foo("bar"); Which from my newfound understanding, is known as direct initialization. (http://en.cppreference.com/w/...
Anon's user avatar
  • 3,613
2 votes
2 answers
104 views

Creating an Instance of a Complicated Class

How do I create instances of classes which require a huge amount of components and attributes? Take for example a car. A car has hundreds of sub components each with their own specific properties. ...
supertommy's user avatar
2 votes
2 answers
4k views

How to handle test with initialising functions that call mocked functions?

I am working on an embedded C project where there is a function used to initialise a module's (foo) context object handle. Thus when testing using the Ceedling I have used this initialising function ...
Toby's user avatar
  • 595
0 votes
3 answers
94 views

Are commands for variable initialization ever used as expressions?

In C, the assignment command also returns the value assigned, so it is also an expression. Similarly, initialization, which happens when defining a variable, also assigns a value to the newly created ...
Tim's user avatar
  • 5,495
2 votes
3 answers
4k views

How do you create immutable objects with many parameters?

I need to create an immutable object but it requires several parameters to work like: class FooRepo { public string ConnectionStringName { get; } public string SchemaName { get; } public ...
t3chb0t's user avatar
  • 2,582
0 votes
3 answers
1k views

A few questions about initializing variables

I'm in an introductory programming class and we're only in lesson two, so try to keep terminology simple, ha. I'm a bit confused about when you need to and when you don't need to initialize a ...
FBHSIE 's user avatar
3 votes
8 answers
7k views

Conditionally initializing a string list

I am writing a program that needs to be able to process data from a number of different sources. The sources output data in a variety of formats. So, depending on which source is being used, I need to ...
TestEngineer's user avatar
3 votes
2 answers
600 views

How to handle a one-time startup SQL script

I'm trying to figure out the best way to tackle running a one-time SQL script when a service instance starts up. Here's the scenario: We run multiple Amazon instances of our service in parallel. Each ...
Nick Gotch's user avatar
1 vote
2 answers
658 views

Is it a good idea having default static variables for new instances?

OK let's say I have something like this: public class MyObject { public static int DefaultValue = 9 private int _value = DefaultValue; public int Value { get { return _value; } set { ...
user avatar
3 votes
2 answers
3k views

What does "initialize event handler" mean in this context?

In file1.js I read: // NOTE: initialize your app event handlers here, see file2.js for a simple event handler example // TODO: configure following to work with both touch and click events (mouse + ...
red-o-alf's user avatar
  • 131
2 votes
1 answer
84 views

How to compute whether it is guaranteed the variable is set?

Assuming declarations are expressions consider such code: if ((var x = foo()) and (var y = x)) or (var z = bar()) then println(z); end The reference to x is OK, because at this point x has to be ...
greenoldman's user avatar
  • 1,534
4 votes
1 answer
4k views

Unique pointer initialisation

What is the correct initialisation of a smart pointer? std::unique_ptr<Class> ptr(std::make_unique<Class>()); or std::unique_ptr<Class> ptr = std::make_unique<Class>(); Is ...
bbalchev's user avatar
  • 584
3 votes
2 answers
305 views

For what reasons Java and C# initialize static data on demand?

I am reading "The Go Programming Language" right now and I have read package initialization chapter which tells (or I read it wrong) that Go uses eagerly initialization. So in time we saw say C++ ...
greenoldman's user avatar
  • 1,534
6 votes
2 answers
1k views

When to use Constructor and when to use Collection Initializer?

I am having a .Net class which has 10 properties like given below: [the datatypes of individual property is just a placeholder here and it can be anything from a primitive type to an object to a list ...
GawdePrasad's user avatar
8 votes
1 answer
772 views

Is a large static initializer a code smell?

I am extending SimpleExpandableListAdapter in Android. I don't think Android's adapter is implemented very well, in that its constructors have a large number of rather complicated arguments and it has ...
TBridges42's user avatar
20 votes
3 answers
60k views

Is it good to define a variable inside a loop? [closed]

My instructor once told me that I should not define a variable inside a loop, but I honestly still do not understand why. What are the disadvantages of that? Could any body explain that to me?
Mo Haidar's user avatar
  • 537
23 votes
2 answers
9k views

Why does Swift not require semicolons? [closed]

I normally code in either c# or Objective-C and recently took it upon myself to learn Apple's new programming language - Swift. The first thing I noticed is that you don't need to add semicolons to ...
Memj's user avatar
  • 335
39 votes
5 answers
28k 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 ...
Trevor Hickey's user avatar
2 votes
1 answer
9k views

Should I initialize a member variable in declaration when it is initialized by a constructor parameter?

Which is recommended for initialization of class fields in C#: class Foo { public X x = new X(); // or any default value... public Foo(X _x) { x = _x; } } Or class Foo { ...
Ahmad's user avatar
  • 1,836
3 votes
3 answers
2k views

In C++, how to call an 'init' routine as a class is loaded?

I have a program that loads a bunch of "addons" (different meters that measure and display system values). The "addons" have "grown" over time and the code of what addons to "activate", and how many ...
Astara's user avatar
  • 187
2 votes
2 answers
14k views

Initialize in Field, Constructor, or Argument

If I know what a field will be initialized to, should I initialize it in the field, constructor, or receive it as a parameter? I am asking about best practices. All three options effectively provide ...
Evorlor's user avatar
  • 1,556
0 votes
1 answer
2k views

issues about Spring initialization in different environment

I have some questions about spring's initialization in different environment. 1. In web container context, such as tomcat. I knew that spring can be initialized by declaring org.springframework....
NingLee's user avatar
  • 230
1 vote
3 answers
17k views

Make Return Type an Interface - Problem with Initialization

I would like to make the return type of my method an interface rather than a class for similar reasons stated in c# List or IList, however I am having trouble figuring out how to initialize the ...
Strategy Thinker's user avatar
14 votes
1 answer
3k 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 ...
user2738698's user avatar
11 votes
4 answers
2k 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 ...
stakx's user avatar
  • 2,128
10 votes
9 answers
35k views

How important is to initialize a variable

How important is it to initialize variables? Does proper initializing avoid memory leaks or have performance advantages?
Vivek's user avatar
  • 135
7 votes
4 answers
3k views

Separation of construction and initialization

I'm confused by this post by Mark Seeman. And his comment on IInitializable below: The problem with an Initialize method is the same as with Property Injection (A.K.A. Setter Injection): it ...
Pavel Voronin's user avatar
3 votes
4 answers
2k views

Initializing derived classes in the same way

I have a class Base that has several children, say A, B, C. For testing purposes I'd like to mock those derived classes by deriving from them. So MockA derives from A, MockB derives from B and so on. ...
TobiMcNamobi's user avatar
  • 1,161
2 votes
1 answer
133 views

Data decoding initialization/Constructor error handling

I have a set of loadable data decoders for a specific type of data and a stream to read containing data. Now I want the program to select the correct decoder in a reliable way so I want to use a trial-...
user877329's user avatar
3 votes
1 answer
1k views

Why does this static field always get initialized over-eagerly?

I am looking at this excellent article from Jon Skeet. While executing the demo code, Jon Skeet says that we can expect three different kinds of behaviours. To quote that article: The runtime ...
TheSilverBullet's user avatar
24 votes
4 answers
26k views

C++ - Constructor or Initialize Method to Startup [duplicate]

Possible Duplicate: Avoid having an initialization method I want to determine when to do non-trivial initialization of a class. I see two times to do initialization: constructor and other method. ...
Bob Fincheimer's user avatar
135 votes
10 answers
106k views

What is the opposite of initialize (or init)? [closed]

The term will be used as a method name. The method is called when a part of the user interface is hidden (or removed), and it is used to reset values to default and dispose objects that will not be ...
Gabriel Diaconescu's user avatar
4 votes
5 answers
2k views

variable comparison without initialising

I am working with a project in VC++ which involves co-ordinate system having x,y,z axis. I am trying to check for if a variable(Point with x,y,z values) is assigned on the coordinate system or its a ...
Shirish11's user avatar
  • 1,469