views:

51

answers:

3

I don't know if this has been asked before, but we're having a discussion about it today at my job. Should private variables (that are shared/static) be instantiated when they are dimensioned/defined, or is it a better practice to do this inside of a constructor?

For example, this seems perfectly fine to me...

Public Class IpCam

    Private Const HOST As String = "http://test.com/url/example"
    Private Shared _Example As New OurClass(HOST)

    Public Shared ReadOnly Property Example() As OurClass
        Get
            Return _Example
        End Get
    End Property
End Class

But others are telling me that it should be done like this...

Public Class IpCam

    Private Const HOST As String = "http://test.com/url/example"
    Private Shared _Example As OurClass

    Public Sub New()
        _Example = New OurClass(HOST)
    End Sub

    Public Shared ReadOnly Property Example() As OurClass
        Get
            Return _Example
        End Get
    End Property
End Class

What is the difference? Is there a common consensus as to which one to use?

+3  A: 

It's really a matter of preference. I think what's more important is consistency: if you instantiate a few variables inline, and others in a constructor, it can get harder to maintain, as it's unclear what is instantiated where.

A good idea is to keep variable declarations just above your constructor (so you don't have to jump around to find all the variable instantiations), and instantiate everything inline. For those few objects which require more complex initialization code, you can use a constructor.

Phong
I like this answer. I prefer my first example because it seems more readable to me. Additionally, the whole thing can be done in one line of code as opposed to two.
Josh Stodola
+1  A: 

I wonder if your second example is a hangover from the old VB6 days when good practise meant generally avoiding As New declarations because it wasn't optimal (auto-instantiation meant a run-time check each time) and you could never reliably test the instance for Is Nothing etc.

onedaywhen
A: 

One benefit to initializing the variables inline is that you do not have to remember to put the initialization in each constructor or make sure each other constructor calls the one with the initialization. Take this code for example:

Public Class Person

    Public Sub New()
        _name = "asdlfkj"
    End Sub
    Public Sub New(ByVal age As Integer)
        _age = age
    End Sub

    Private _name As String
    Public ReadOnly Property Name As String
        Get
            Return _name
        End Get
    End Property

    Private _age As Integer = 17
    Public ReadOnly Property Age As Integer
        Get
            Return _age
        End Get
    End Property
End Class

Calling the first constructor will put in a default name, but calling the second will not.

Conversely, if you ever need to initialize the variable different ways for different constructors, I would definitely say to initialize in the constructor.

Gideon Engelberth