views:

120

answers:

4

This is a sort of design question, and I'm sure there are people who do it both ways. But in your opinion, is it better to assign a variable in the class or in the constructor? For example (regardless of syntax or language, this is just to explain):

public class Example
{
   private final int TIME_STAMP = Now();
   private int num = 2;
}

OR

public class Example
{
   private readonly int TIME_STAMP;
   private int num;

   public Example()
   {
      TIME_STAMP = Now();
      num = 2;
   }
}

Please disregard the mix of different languages and syntax... Which is preferable and why?

+2  A: 

Inline (the first option):

  • it is more readable
  • you don't have to duplicate it in every constructor
  • if there is such thing in your language, you can use initializer blocks. They look like constructors, but don't need to be defined multiple times. In Java they look like this:

    {
        var1 = 5;
        varTime = now();
    }
    
Bozho
@Bozho - what's the benefit of an initializer block over just putting the code in the class (like the first option in my question)?
froadie
depending on preferences it might be considered more readable.
Bozho
+4  A: 

In tend to :

  • Set default, constant values in the class itself
  • And values that depends on something else (like the current time) in the constructor.


i.e., I tend to use something like this (merging your two examples) :

public class Example
{
   private readonly int TIME_STAMP;
   private int num = 2;

   public Example()
   {
      TIME_STAMP = Now();
   }
}

But my prefered language is PHP, and it doesn't allow me to set non-constant values in the class declaration itself, so my way might be a bit biased.

Pascal MARTIN
yes, `private readonly int TIME_STAMP = NOW();` is quite better, if possible
Bozho
Voting this up because I like the distinction between constant/default values and variable values. A logical, useful distinction, methinks.
doppelfish
isn't the distinction obvious by the `readonly` keyword?
Bozho
But, as Bozho mentioned, then you would have to repeat the TIME_STAMP = Now() code in each constructor... is that unnecessary repetition? Or justified for design reasons?
froadie
(almost) no duplication of code is justified, and I don't see a design reason here :)
Bozho
+1  A: 

The main idea here is that you put in any start up or initialization code for the class in the constructor. As opposed to this and logically, you put in any clean up code in the destructor.

Sarfraz
+1  A: 

From a testability standpoint, both approaches are problematic. Except for constants, you should have a constructor which accepts these as arguments, or a property which allows you to set the backing field.
Typically, I would create a "full" constructor, which gives you complete control of the class, and an empty/default constructor, which calls the other one specifying the default arguments. That way, everything is in one place, and I don't have to go look into the backing fields to figure out what is going on at construction time.

Mathias
This is how I write my constructors. The only time I instantiate something is when I need the instantiation to guarantee global scope.
Joel Etherton