S4581 — "new Guid()" should not be used

Language
VB.NET
Type
Code smell
Severity
Major

Why is this an issue?

When the syntax new Guid() (i.e. parameterless instantiation) is used, it must be that one of three things is wanted:

  1. An empty GUID, in which case Guid.Empty is clearer.
  2. A randomly-generated GUID, in which case Guid.NewGuid() should be used.
  3. A new GUID with a specific initialization, in which case the initialization parameter is missing.

This rule raises an issue when a parameterless instantiation of the Guid struct is found.

Noncompliant code example


Public Sub Foo()
    Dim G1 As New Guid        ' Noncompliant - what's the intent?
    Dim G2 As Guid = Nothing  ' Noncompliant
End Sub

Compliant solution


public void Foo(byte[] bytes)
Public Sub Foo(Bytes As Byte())
    Dim G1 As Guid = Guid.Empty
    Dim G2 As Guid = Guid.NewGuid()
    Dim G3 As Guid = New Guid(Bytes)
End Sub

↑ Back to top