Questions tagged [initialization]
53 questions
132
votes
10 answers
What is the opposite of initialize (or init)?
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 used any more.
Possible names are: release, remove,…

Gabriel Diaconescu
- 1,477
- 2
- 10
- 6
37
votes
5 answers
Should I initialize C structs via parameter, or by return value?
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
InitializeFoo(Foo* const foo){
foo->a = x; //derived…

Trevor Hickey
- 993
- 1
- 9
- 16
24
votes
4 answers
C++ - Constructor or Initialize Method to Startup
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. I want to figure out when to use each.
Choice…

Bob Fincheimer
- 428
- 1
- 3
- 10
23
votes
2 answers
Why does Swift not require semicolons?
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 end a line in Swift but if you do-at least from…

Memj
- 335
- 3
- 8
20
votes
3 answers
Is it good to define a variable inside a loop?
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
- 507
- 2
- 4
- 9
16
votes
6 answers
How to write constructors which might fail to properly instantiate an object
Sometimes you need to write a constructor which can fail. For instance, say I want to instantiate an object with a file path, something like
obj = new Object("/home/user/foo_file")
As long as the path points to an appropriate file everything's…

pseudosudo
- 262
- 1
- 2
- 7
15
votes
1 answer
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 the case that the default constructor is useless? …

user2738698
- 957
- 1
- 8
- 20
12
votes
4 answers
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 completely independent, yet they need access to the…

glades
- 315
- 2
- 6
12
votes
4 answers
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 via a REST-API.
Here are two variants I can think…

AyCe
- 378
- 1
- 3
- 13
11
votes
4 answers
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 speaking, the atomic process that brings a newly…

stakx
- 2,138
- 18
- 29
11
votes
5 answers
Is web application startup time really that important?
Had a conversation with someone about adding some initialization code on application startup and he complained about that causing an increasing in the startup time. He couldn't really state a reason (gut feeling or something, don't know). This is…

JohnDoDo
- 2,309
- 2
- 18
- 32
10
votes
9 answers
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
- 135
- 1
- 1
- 6
10
votes
2 answers
best practice for initializing class members in php
I have lots of code like this in my constructors:-
function __construct($params) {
$this->property = isset($params['property']) ? $params['property'] : default_val;
}
Is it better to do this rather than specify the default value in the…

rgvcorley
- 203
- 1
- 2
- 7
8
votes
1 answer
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 no setters or builder. In my class, most of…

TBridges42
- 343
- 1
- 7
7
votes
4 answers
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 creates a temporal coupling
between the Initialize…

Pavel Voronin
- 1,640
- 1
- 18
- 25