46 votes
Accepted

Is it good to define a variable inside a loop?

It's not a problem to define a variable within a loop. In fact, it's good practice, since identifiers should be confined to the smallest possible scope. What's bad is to assign a variable within a ...
26 votes
Accepted

Should I initialize C structs via parameter, or by return value?

In the 2nd approach you will never have a half-initialised Foo. Putting all the construction in one place seems a more sensible, and obvious place. But... the 1st way isn't so bad, and is often used ...
  • 48.1k
24 votes

Should I initialize C structs via parameter, or by return value?

Both approaches bundle the initialization code into a single function call. So far, so good. However, there are two issues with the second approach: The second one does not actually construct the ...
21 votes
Accepted

Why does Swift not require semicolons?

What allows the some languages to do this and others to not? Why not keep a uniform system of adding the semicolon to the end? Robert gave a good answer regarding Swift, I'll try to add bit more ...
  • 464
20 votes

Is it good to define a variable inside a loop?

Complex types have non-trivial constructors and destructors. Those will get called at the start and end of the loop body (as it's initialized and goes out of scope). If the initialization is ...
14 votes

Why does Swift not require semicolons?

Line breaks are used instead of semicolons to delimit the end of a statement. In Language guide: The Basics it says: Unlike many other languages, Swift does not require you to write a semicolon (;)...
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 ...
9 votes
Accepted

Is a large static initializer a code smell?

Having static helper functions to create the constructor argument is a perfectly sane solution, but these functions are limited in what operations they can perform since they must produce exactly one ...
  • 126k
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 ...
  • 22.4k
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, ...
  • 108k
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 ...
  • 69.8k
7 votes

Is it good to define a variable inside a loop?

Well, his advice is slightly too simple (that's an understatement). Following it ranges all the way from a good idea over who cares and bad idea to impossible. You should follow it whenever re-using ...
  • 8,298
7 votes
Accepted

Initialize in Field, Constructor, or Argument

Initializing as a parameter breaks encapsulation in that the caller can then do with the passed in list what it wants (clear at odd times). ArrayList<String> list = new ArrayList<String>(...
7 votes
Accepted

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

You should choose between init in declaration or constructor, not both. In a language that has a default constructor where you don't have to explicitly provide a constructor if the default is all you ...
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 ...
7 votes
Accepted

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

C++ and Go are designed to compile to native machine code. Both have a phase during building where separate modules are linked together to produce a single combined module. Once this has been done, a ...
  • 17.3k
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 ...
  • 188k
6 votes
Accepted

When to use Constructor and when to use Collection Initializer?

Since it is an internal class, there is no benefit to using read/write automatic properties; you are creating lots of get/set methods in your resultant IL. Assuming you are using C# 6, you have two ...
  • 38.4k
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 ...
  • 8,298
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 ...
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 ...
  • 247
5 votes

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

In most plugin systems in C and C++, the loading of plugins works like this: The main application specifies a path where the plugins should be located (or allows such a path to be configured). On ...
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 ...
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 ...
4 votes
Accepted

Where should an object in CQRS+ES be fully initialized: in the constructor, or when applying the first event?

When doing CQRS+ES I prefer not having public constructors at all. Creating my aggregate roots should be done via a factory (for simple enough constructions such as this) or a builder (for more ...
  • 3,435
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 ...
  • 24.9k
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 ...
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 ...
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 ...
  • 4,515
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 ...

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