Skip to content

08. Visual Basic coding standards

Diane Dowling edited this page Feb 27, 2023 · 7 revisions

Reference: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/coding-conventions

Exemplar code

    Function BinarySearchRecursive(ByVal items As Integer(), ByVal searchItem As Integer, ByVal first As Integer, ByVal last As Integer) As Integer
        If first > last Then
            Return -1
        Else
            Dim midpoint As Integer = (first + last) \ 2

            If items(midpoint) = searchItem Then
                Return midpoint
            ElseIf searchItem > items(midpoint) Then
                first = midpoint + 1
                Return BinarySearchRecursive(items, searchItem, first, last)
            Else
                last = midpoint - 1
                Return BinarySearchRecursive(items, searchItem, first, last)
            End If
        End If
    End FunctionfoundIndex
End Function

The exemplar code for Visual Basic in the GitHub version will also include a Main subroutine that sets up some test data and calls BinarySearchRecursive.

Naming conventions

  Guidance Example
Variables CamelCase totalPay
Constants Constants should be declared const and use capitals HOURLYRATE
Functions PascalCase CalculatePay
Parameters PascalCase HoursWorked
Class PascalCase (singular noun) GamePlayer
Methods PascalCase CalculatePay
Strings Must use double quotes "Hello world"
Characters Must use single quotes 'H'  

Spaces

  • No space between the function identifier and the parameter list
  • Single space either side of operators (unless used as a parameter)
  • Single space after commas in lists
  • Two lines between functions (or methods)
  • Use white space (single line) within a function if it helps readability

Comments

  • Comments start with a single quote (').
  • Insert one space between the comment delimiter (') and the comment text.
  • Start the comment text with an uppercase letter, and end comment text with a full stop.
  • Multiline comments: Start each line with a single quote
  • Use a single line comment above each function with important information about the function

Main method

  • VisualBasic applications always use the Main method as their entry point.
  • Main can be used for the whole program if the application is small and there is no need for additional subroutines.
Sub Main()
   Dim n as Integer = 6
   Dim result as Integer = SumToN(n)
   Console.WriteLine($"The sum of 1 to {n} is {result}")
End Dub

Declaring variables

Always use the Dim keyword as this is best practice.

Declaring an array

Use the syntax Dim LetterList As String() = {"a", "b", "c"}

String formatting

Example: Console.WriteLine($"The sum of 1 to {n} is {result}")

Comment for top of GitHub file

The very first lines of code should be the default comment below which explains how to run the Visual Basic program. This comment should appear before the first programming statement or Imports statement.

' Raspberry Pi Foundation
' Developed as part of Ada Computer Science 
' Usage licensed under CC BY-NC-SA 4.0
' Note: This file is designed to be copied out and compiled on your machine.
' To run this file you need to:
' 1. Copy the contents
' 2. Paste them into the Visual Basic IDE of your choice (Visual Studio, for example)
' 3. Compile the program
' 4. Run the program

Clone this wiki locally