Skip to main content
43 votes

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

The answer to the problem "a third-party module has unsuitable semantics..." is always "...so I code an adapter that warps those semantics into something suitable." In other words, ...
Kilian Foth's user avatar
36 votes

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

There are several ways to solve this problem. One is to use an adapter or facade around the nvs, as described in the answer by @KilianFoth. Another option is to use dependency injection. Then the ...
Bart van Ingen Schenau's user avatar
11 votes
Accepted

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

There are two ways to deal with object state: Have extremely sophisticated debugging skills so that you can find obscure bugs, or Manage your state in a better way so that obscure bugs do not happen ...
Robert Harvey's user avatar
9 votes
Accepted

C# - Initialize DTOs in constructor or via properties?

My thoughts are that if your object serves as a means to transfer data (and therefore purely output/input in a sense), the better thing is to not render objects mutable if they need not be and that ...
Neil's user avatar
  • 22.8k
8 votes

How do you create immutable objects with many parameters?

You pass them in via the constructor. If that means your constructor is too large, then maybe your giant class shouldn't be so giant. There are very few things in the world that have 5+ independent, ...
Telastyn's user avatar
  • 109k
8 votes

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

This shows that the author is using modern C++ (e.g. >= C++11) and applies good practice: Braced initialization is the most widely usable initialization syntax, it prevents narrowing conversions ...
Christophe's user avatar
  • 77.4k
7 votes
Accepted

Unique pointer initialisation

The two are equivalent. The second would (officially) require an implicit copy if (and only if) the type of the initializer differed from the type of the object being initialized. In reality, even in ...
Jerry Coffin's user avatar
  • 44.5k
7 votes

How to indicate that an application has been started before?

Either deploy the initial instance of your database with all the samples, so the data is already there before the application starts the first time, or test if any of the data is already there in ...
Doc Brown's user avatar
  • 208k
6 votes
Accepted

Assignment with brackets

Yes, there is one. Those brackets disable implicit narrowing conversions, so if the type of the expression inside does not fit an int it's an error instead of a silent loss of data. Of course, 99 is ...
Deduplicator's user avatar
  • 9,031
6 votes

What is the meaning of injecting a component in programming?

That is correct. Instead of making ExoPlayer implementation instantiate every single Renderer there is, you instantiate only those that you actually need for that particular implementation, and then ...
Vladimir Stokic's user avatar
5 votes

A few questions about initializing variables

In languages like C, newly-declared variables essentially point to a (more or less) random memory location. If you declare the variable without also initializing it, it will contain whatever random ...
Robert Harvey's user avatar
5 votes

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

I'm sure there are situations where this would make sense to do, particularly if you're writing a small application, but in general I think it would be a bad idea. You could apply any argument that ...
Brett's user avatar
  • 247
5 votes

What is the meaning of injecting a component in programming?

Injecting any value comes from the concept of dependency injection. At it's core, dependency injection defers to run time the actual implementation of an interface it is going to use for your ...
Berin Loritsch's user avatar
4 votes

Creating an Instance of a Complicated Class

Use the Builder pattern The Builder Pattern is intended for exactly this scenario. Named parameters In this example I combine the builder pattern with named parameters to make it crystal clear what ...
John Wu's user avatar
  • 26.5k
4 votes
Accepted

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

Are they both initializing the same child(?) variable? When the object is created, all the non-private properties of all parent classes belong to this newly created object. He doesn't know which ...
Vadim Samokhin's user avatar
4 votes

Calling a static method from constructor's member initializer list

You said in your text that CreateSkyBox () was private, but its not declared private. There is absolutely nothing wrong with having static 'helper' functions in a class, which are used to help with ...
Lewis Pringle's user avatar
4 votes
Accepted

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

"Best" is of course subjective, but in this case I think it boils down to usability. If you are going to have humans executing this program and typing in 500 computer names, that's not ...
mmathis's user avatar
  • 5,488
3 votes

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

One potential solution is to make sure that your object's field is always set in the constructor. Often, that will mean that the constructor takes the value of this field as an argument; it's much ...
Tanner Swett's user avatar
  • 1,359
3 votes

C# - Initialize DTOs in constructor or via properties?

C# 9.0 Records and init-only setters make this very elegant. It can be done as simply as this: public record Dto { public string Name { get; init; } public string Number { get; init; } } or ...
Lewis's user avatar
  • 31
3 votes

Are commands for variable initialization ever used as expressions?

In the typical imperative language, a variable declaration is a statement, not an expression, and therefore does not return a value. But this is not the case for all languages. For example in Python, ...
JacquesB's user avatar
  • 59.6k
3 votes

Conditionally initializing a string list

A lot of times, you can really clean up a problem like this by expressing your data in a form with less syntax than a programming language, such as a CSV string. I don't know C#, but an example in ...
Karl Bielefeldt's user avatar
3 votes

How to handle a one-time startup SQL script

Only option three Carefully wrapping each non-idempotent statement in checks. has any appeal for me. This option is the only one that doesn't require the script to depend on anything else (...
Robert Harvey's user avatar
3 votes

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

This is not a good idea because you can introduce a race condition. In one thread you change the value to one you want but before you manage to create an instance of the class another thread changes ...
Bent's user avatar
  • 2,576
3 votes

Modelling seats of a table in a social game

A Seat should know whether or not it is occupied. Simply because someone joins or leaves the game doesn't mean you need a new Seat(). The player has just vacated that spot at the table. That being ...
Greg Burghardt's user avatar
2 votes

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

Definition An event handler is just a piece of code (function in the case of Javascript) that does something when a specific event occurs. An Example One example would be the mouseover event that ...
Adam Zuckerman's user avatar
2 votes

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

What you're asking about is a form of data flow analysis. The code could be translated into some form of basic blocks (or other intermediate data structure). There will be flow of control (branches) ...
Erik Eidt's user avatar
  • 33.8k
2 votes
Accepted

How do you create immutable objects with many parameters?

Assumed your class is not "too large" (which you should check first), instead of having a huge constructor argument list in FooRepo, you can pass the values in by utilizing a mutable DTO object as a ...
Doc Brown's user avatar
  • 208k
2 votes

Are commands for variable initialization ever used as expressions?

There are so many languages that there are a couple of languages where declarations are expressions. For example Perl. Lexically scoped variables are declared with my, e.g. my $var. This evaluates to ...
amon's user avatar
  • 134k
2 votes
Accepted

Verifying Parameters in Constructor or Service

1) No OOP principle is broken when you throw an exception in a constructor. Bjarne Stroustrup recalled in his book The design and evolution of C++ how exceptions were designed into the language ...
Christophe's user avatar
  • 77.4k
2 votes
Accepted

Python - Type Hinting specific sized Vectors

Python does have optional typing, which you can use like this: from typing import TypeVar, Tuple from numpy import array, float64, isnan import sys T = TypeVar('T', int, float) class Shape: def ...
Turksarama's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible