Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring variables - best practices

Tags:

c#

.net

I found a while ago (and I want to confirm again) that if you declare a class level variable, you should not call its constructor until the class constructor or load has been called. The reason was performance - but are there other reasons to do or not do this? Are there exceptions to this rule?

ie: this is what I do based on what I think the best practice is:

public class SomeClass
{
   private PersonObject _person;

   public SomeClass()
   {
      _person = new PersonObject("Smitface");
   }
}

opposed to:

public class SomeClass
{
   private PersonObject _person = new PersonObject("Smitface");

   public SomeClass()
   {

   }
}
like image 814
schmoopy Avatar asked Oct 29 '08 18:10

schmoopy


4 Answers

If you set your variable outside of the constructor then there is no error handling (handeling) available. While in your example it makes no difference, but there are many cases that you may want to have some sort of error handling. In that case using your first option would be correct.

Nescio talked about what implication this would have on your applicaiton if there were some constructor failures.

For that reason, I always use Option #1.

like image 167
discorax Avatar answered Sep 30 '22 17:09

discorax


Honestly, if you look at the IL, all that happens in the second case is the compiler moves the initialization to the constructor for you.

Personally, I like to see all initialization done in the constructor. If I'm working on a throwaway prototype project, I don't mind having the initialization and declaration in the same spot, but for my "I want to keep this" projects I do it all in the constructor.

like image 40
OwenP Avatar answered Sep 30 '22 17:09

OwenP


Actually, in spite of what others have said, it can be important whether your initialization is inside or outside the constructor, as there is different behaviour during object construction if the object is in a hierarchy (i.e. the order in which things get run is different).

See this post and this post from Eric Lippert which explains the semantic difference between the two in more detail.

So the answer is that in the majority of cases it doesn't make any difference, and it certainly doesn't make any difference in terms of performance, but in a minority of cases it could make a difference, and you should know why, and make a decision based on that.

like image 42
Greg Beech Avatar answered Sep 30 '22 17:09

Greg Beech


There's a common pattern called Dependency Injection or Inversion of Control (IOC) that offers exactly these two mechanisms for "injecting" a dependant object (like a DAL class) into a class that's furthur up the dependency chain (furthur from the database)

In this pattern, using a ctor, you would

public class SomeClass
{
private PersonObject per;

 public SomeClass(PersonObject person) 

 {      
     per = person;   
 }   

}

private PersonObject Joe = new PersonObject("Smitface");

SomeClass MyObj = new SomeClass(Joe);

Now you could for example, pass in a real DAL class for production call or a test DAL class in a unit test method...

like image 40
charles bretana Avatar answered Sep 30 '22 17:09

charles bretana