Field or variable explicitly initialized to the default value of its type
ID |
vbnet.performance.do_not_initialize_unnecessarily |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Performance |
Language |
VB.NET |
Description
Reports a field, auto-implemented property or local variable declared with an initializer
that merely repeats the default value of its declared type - 0 for a numeric type, False
for Boolean, or Nothing for any type.
Rationale
The runtime already zero-initializes every field and local before any code can observe it, so an initializer that assigns the default value has no effect. For a field it is not free: a field initializer is compiled into every instance constructor, so the object is written twice - once by the allocator and once by the constructor - on every construction. It also adds noise that hides the initializers that do matter, and it misleads the reader into thinking the value was chosen deliberately rather than defaulted.
The following code illustrates the pattern detected by this rule:
Public Class InvoiceTotals
' FLAGGED: Field or variable explicitly initialized to the default value of its type
Private retryCount As Integer = 0
' FLAGGED: Field or variable explicitly initialized to the default value of its type
Private ReadOnly isFinalised As Boolean = False
' FLAGGED: Field or variable explicitly initialized to the default value of its type
Private customerName As String = Nothing
Remediation
Drop the initializer and declare the field or variable with its type only. Keep the
initializer when the value is not the default, when the declaration is a Const (where an
initializer is mandatory), or when writing it out documents a deliberate choice the team wants
stated - in that case say so in a comment rather than relying on the assignment.
' Before: redundant, and for a field it is re-stored in every constructor
Private retryCount As Integer = 0
Dim total As Decimal = 0D
Dim summary As String = Nothing
' After
Private retryCount As Integer
Dim total As Decimal
Dim summary As String
A second exception applies to a local declared inside a loop body. Visual Basic scopes such a variable to the block but gives it a single storage location for the whole procedure, so it keeps its value from one iteration to the next; the initializer is what resets it each time round. Deleting it there changes behaviour rather than tidying it - the second iteration starts with whatever the first left behind.
' Keep the initializer here: without it, `total` carries over between iterations.
For Each order In orders
Dim total As Decimal = 0D
For Each line In order.Lines
total += line.Amount
Next
Report(order.Id, total)
Next
One exception is worth knowing before deleting a = Nothing on a local. Where the local is
read on a path the compiler cannot prove it was assigned on - typically a resource declared
before a Try and then used from the Catch or the Finally - dropping the initializer
raises BC42104, "Variable is used before it has been assigned a value". Assigning Nothing
is the accepted way to state that the unassigned case is intended and to silence that warning,
so in that shape keep it. Fields are not affected: this warning applies to locals only.
' Keep the initializer here: savePoint is read in the Catch and in the Finally, on paths
' where the compiler cannot see the assignment.
Dim savePoint As SqlTransaction = Nothing
Try
savePoint = conn.BeginTransaction()
' ...
savePoint.Commit()
Catch ex As Exception
If savePoint IsNot Nothing Then savePoint.Rollback()
Finally
If savePoint IsNot Nothing Then savePoint.Dispose()
End Try