Coding Guidelines
From Xojo Documentation
Every developer and team should have coding guidelines to help ensure that code is readable by everyone on the team (or even yourself several months from now). There is no such thing as coding guidelines that everyone will agree with, but the guidelines described here are meant to be a starting point and match the coding guidelines generally used for the Xojo example projects and sample code that you see here in the Dev Center.
The most important thing is consistency, regardless of what guidelines you choose to use. Inconsistent code is more difficult to understand.
Contents
Definitions
Term | Definition |
---|---|
camel case | The first "word" in the name is lower cased. Subsequent "words" are started with an upper case letter: customerName |
title case | All "words" in the name are started with an upper case letter: CustomerName |
Naming
Having a consistent way to name the things in your project is an important first step in coding standards. Ideally coding standards will help others write correct code more easily and grasp existing code more readily.
Constants
- Start with lower case "k", followed by title case: kMaxUsers.
Local Variables
- Use camel case: Dim customerName As Text
- Minimize use of abbreviations; spell things out. Use customerName, not custNm.
- Use of single-letter variables names are acceptable for looping variables, such as used by For loops.
Arrays
- Should be plural: Customers() As Text
Properties
- Use title case: CustomerName
- A Private property that is the companion for a Computed Property should start with "m" and then be title case: mCustomerName.
Methods
- Use title case: SaveCustomer
- Method parameters should be camel case: SaveCustomer(customerName As Text)
Controls
- Use title case, with a suffix indicating the type of control: OKButton. Below are some common suffixes:
Control | Suffix | Example |
---|---|---|
PushButton, WebButton, iOSButton, BevelButton | Button | SaveButton |
ListBox, WebListBox | List | CustomerList |
SegmentedButton, WebSegmentedControl, iOSSegmentedControl | Selector | TaskSelector |
CheckBox, WebCheckBox | Check | TaxableCheck |
PopupMenu, WebPopupMenu | Popup | StatePopup |
RadioButton, RadioGroup | Radio | SourceRadio |
TextField, WebTextField, iOSTextField | Field | NameField |
TextArea, WebTextArea, iOSTextArea | Area | DescriptionArea |
Canvas, WebCanvas, iOSCanvas | Canvas | GraphCanvas |
Label, WebLabel, iOSLabel | Label | NameLabel |
PagePanel | Panel | MainPanel |
TabPanel | Tab | MainTab |
ProgressWheel, ProgressBar, WebProgressWheel, WebProgressBar, iOSProgressBar, iOSProgressWheel | Progress | DownloadProgress |
HTMLViewer, WebHTMLViewer, iOSHTMLViewer | Viewer | DocViewer |
ImageWell, WebImageView, iOSImageView | Image | ProfileImage |
GroupBox | Group | BusinessGroup |
Classes (Types)
- Use title case.
- Subclasses of built-in classes should use original class name as suffix: CustomerListBox.
Windows
- Use title case with "Window" as the suffix: CustomerWindow.
Interfaces
- Use title case with "Interface" suffix.
Formatting
- Keywords should be in title case:
- Data types should be in title case:
- Put spaces between all lists of arguments or parameters: SaveCustomer(name, location, value)
- Do not put spaces before or after parenthesis.
- Methods called without parameters should not include empty parenthesis. Use: MyMethodinstead of:MyMethod()
- Methods with parameters should always include parenthesis. Use: MyMethod(42)instead of:MyMethod 42
- Leave blank lines between code lines as appropriate to maintain readability.
SQL
- SQL commands are written in uppercase:
SELECT * FROM Team WHERE City = "Boston"
Coding
- Dim local variables near where they will be used with one declaration per line.
- Methods not used outside the class/module should be Private.
- Prefer dot notation over equivalent global methods when possible: Dim length As Integer = customerName.Len
- Never use Me when you mean Self.
- Limit globals.
- Prefer shared methods/properties on classes over global methods/properties on modules.
- Classes, methods, properties, etc., within a Module or Class should be Private or Protected where possible.