-
Notifications
You must be signed in to change notification settings - Fork 8
08. Visual Basic coding standards
Alex Parry edited this page Mar 4, 2025
·
6 revisions
The following code is from our Ada Computer Science content page on the binary search algorithm (recursive), which includes a detailed explanation of the algorithm.
The exemplar code in the GitHub version also includes the Main()
subroutine with test data and a call to BinarySearchRecursive()
.
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 Function
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' |
- 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 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
- 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
Always use the Dim keyword as this is best practice.
Use the syntax Dim LetterList As String() = {"a", "b", "c"}
Example: Console.WriteLine($"The sum of 1 to {n} is {result}")
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