Correct syntax way in object instance

Asked

Viewed 197 times

5

In the instance of an object, we can:

Pessoa pessoa = new Pessoa();

and we can also:

Pessoa pessoa = new Pessoa(){ Nome = "Nome", Id = 0, Situacao = true };

What is the best or most correct way to instantiate the object?

  • 3

    It’s the same thing.

  • Augusto Henrique, on Monday you are already filling values within the instance... but you need to do it right now?

  • In the second version, the parentheses () are not necessary.

  • @Fabioin not necessarily at this time.

  • I wrote wrong... the keys are not necessary, because they pass parameters that are not necessary.

4 answers

5

It depends on your need or ease you wish to have.

The first one will instantiate the class with the default values of the properties (0 for int, 0.0 for double, false for Boolean, etc).

The second it instantiates and already passes directly the value between keys to the property. Note that parentheses are not required in this case if there is no default constructor or does not receive any parameter.

You can change the value later, regardless of the way you did, but the second one is quite useful if you want to instantiate with predefined values different from the standard that C# considers, being another facility at the time of instantiation.

4

There is not necessarily a right way to do this instance, the two ways are correct, the main difference is that the first way you can debug the code and access its members separately.

3


The most important thing about this is that the two do completely different things, so they’re not exactly comparable. This code would be comparable to the second of the question:

Pessoa pessoa = new Pessoa();
pessoa.Nome = "Nome";
pessoa.Id = 0;
pessoa.Situacao = true;

I put in the Github for future reference.

The first question code creates an object Pessoa and without any initialization, that is, the object exists, but it is useless, all its data is invalid, it is a danger to its application. The second creates with some data. I do not know if the correct way, if there are validations in the properties (I am considering that there are properties and not fields), and nothing forces you to do this way.

But this code is all wrong already by class modeling. Almost always a constructor without parameters is wrong. See What good is a builder?. No object should be created in invalid state.

So yes, contrary to what another answer says, there is a correct way to instantiate an object. There are several correct ways, and several wrong ways. The two forms presented are wrong by the above. Although it’s not a very serious conceptual error or one that might not be useful in some situation, but in almost every situation it shouldn’t do so, It is correct to have a constructor that receives arguments and creates a valid state object before it can be manipulated by the code in a possibly inappropriate way. With a suitable constructor does not give the consumer the option to boot wrong.

And even if another answer is technically correct it validates this bad practice of having a builder without parameters for business objects and others. Of course, you need to know many more details about the problem to state what is right or wrong. I think it’s absurd, but the requirements could be something like "no matter if the object is in an invalid state," that would be right, although this would be counterintuitive and probably a requirement error.

1

Like most programming, there is no form correct, there is the shape that best fits according to your need.

But what is the difference between these two forms cited?

First we have to consider that the IL will transform this:

Pessoa pessoa = new Pessoa()
{
   Nome = "Nome", 
   Id = 0, 
   Situacao = true 
};

in something like this:

Pessoa _tempPessoa = new Pessoa();
_tempPessoa.Nome = "Nome";
_tempPessoa.Id = 0.
_tempPessoa.Situacao = true;

Pessoa pessoa = _tempPessoa;

Therefore, the performance is the same! No change in runtime.

Advantages of the initializer

  • Atomicity: the object will never be partially initialized: either it is null, or it is 100% initialized (as mentioned in this answer)
  • Best visibiliadde
  • Less code (little, but still)

Disadvantages of the initializer

  • To thresh is a bag!!! Especially in nested structures

Browser other questions tagged

You are not signed in. Login or sign up in order to post.