Appframe Knowledge Base

1 hits

VB.NET Naming Conventions - General

''' <summary>
''' This class demonstrates the naming conventions to be followed when writing Appframe
''' libraries and applications
''' </summary>
''' <remarks>
''' For cases not covered by the example, refer to the official .NET Guidelines for Names
''' found at
''' </remarks>
Public Class Person
    Inherits DomainObject

    ' No prefix on constants
    Private Const FormatNameMask As String = "{1}, {0}"

    ' g prefix on class-level static variables
    Private Shared gDatabaseTable As String = "Persons"

    ' Also g prefix on class-level instance variables
    Private gFirstName, gLastName As String

    ' p prefix on parameters
    Public Sub New(ByVal pFirstName As String, ByVal pLastName As String)

        ' v prefix on local variables
        Dim vIsNameValid = String.IsNullOrEmpty(pFirstName) AndAlso Not String.IsNullOrEmpty(pLastName)

        gFirstName = pFirstName
        gLastName = pLastName

        AddHandler MyBase.AfterDataUpdated, AddressOf HandleSomeEvent

    End Sub

    ' Event handlers are technically functions, thus best practice is to rename
    ' the parameters to follow naming conventions. Leaving them as "sender" and "e"
    ' is permissable if they are not referenced within the method body
    Private Sub HandleSomeEvent(ByVal pSender As Object, ByVal pArgs As EventArgs)
        ' TODO: Add some important logic here
    End Sub

    ' We also capitalize the first letter of private method names
    Private Sub DoSomethingRisky()
            Dim vMagicValue As Integer = Integer.Parse("FortyTwo")
        Catch ex As Exception ' Use ex as exception names
            Console.WriteLine("Something blew up: {0}", ex.Message)
        End Try
    End Sub

    Public Function FirstNameIsInList(ByVal pNames As List(Of String)) As Boolean

        ' One letter variable names is ok for indexers, but should be reconsidered
		' if there are several nested for-loops
        For i As Integer = 0 To pNames.Count - 1
            If pNames(i) = gFirstName Then 
				Return True
			End If
		' One-letter variable names is ok for lambda expression parameters
		Dim vUnused = pNames.Where(Function(n) n.StartsWith("p"))

        Return False

    End Function

    Public Overrides Function ToString() As String
        Return String.Format(FormatNameMask, gFirstName, gLastName)
    End Function

End Class

win · Perma link post comment Posted by: Jarl Erik Schmidt (08-mar-2012)