Constant name is not PascalCase

ID

vbnet.maintainability.naming_constant

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Naming

Language

VB.NET

Description

Reports a Const declaration - a member constant or a local one - whose name does not match PascalCase, that is ^[A-Z][A-Za-z0-9]*$. The .NET convention for constants is PascalCase, the same as for any other member, so upper snake case such as MAX_RETRIES is reported even though it is idiomatic in C and Java, and so are camelCase names such as backoffFactor and names containing separators such as default_timeout. Both the explicitly typed form and the inferred-type form (Const RetryHeader = "X-Retry") are examined. ReadOnly fields are not constants and are not reported here. This is a convention rule and is reported at informational severity.

Rationale

NET deliberately does not distinguish constants from other members by capitalization: the framework itself ships Int32.MaxValue, String.Empty, Math.PI and DateTime.MinValue, all PascalCase, so a codebase that writes MAX_RETRIES is announcing a distinction the platform does not make and that no consumer of the API expects. The practical cost is that a public constant is part of the published surface, and an upper-snake name there is immediately visible as foreign to callers - especially from C#, where the same guideline applies - while a camelCase constant is worse still, because it reads as a mutable local at every use and hides that the value is baked into the caller’s assembly at compile time. Mixed conventions inside one project are the real expense: once some constants are MAX_RETRIES and others MaxRetries, a name can no longer be recalled or guessed, and every reference has to be looked up.

The following code illustrates the pattern detected by this rule:

Public Class RetryPolicy

    ' BAD: upper snake case is a C convention, not a .NET one.
    ' FLAGGED: Constant name is not PascalCase
    Public Const MAX_RETRIES As Integer = 5

    ' BAD: separators are not part of PascalCase.
    ' FLAGGED: Constant name is not PascalCase
    Private Const default_timeout As Integer = 30

    ' BAD: camelCase reads as a variable.
    ' FLAGGED: Constant name is not PascalCase
    Friend Const backoffFactor As Double = 1.5

    ' BAD: the inferred-type form follows the same convention.
    ' FLAGGED: Constant name is not PascalCase
    Const retry_header = "X-Retry"

    ' GOOD: PascalCase.

Remediation

Rename the constant to PascalCase - an initial capital letter, each subsequent word capitalized, no underscores: MAX_RETRIES becomes MaxRetries, default_timeout becomes DefaultTimeout, backoffFactor becomes BackoffFactor and retry_header becomes RetryHeader. Do not carry the upper snake case habit over from C or Java; in .NET it marks a constant as foreign. If the value is not genuinely compile-time constant - anything a caller should be able to change without recompiling, or any reference type built at startup - declare it Shared ReadOnly instead of Const and keep it PascalCase, or read it from configuration. Use the rename refactoring in the IDE, and remember that a public Const is inlined into every assembly that reads it, so those assemblies must be rebuilt as well.

Configuration

This detector does not need any configuration.