Programming Style
Adopting a consistent programming style and adhering to naming conventions are crucial for writing clean, readable, and maintainable code. Here are some good practices for programming style and naming conventions in OneVision Scripts.
Style good practices
Indentation
Use consistent indentation to improve code readability. Typically 4 spaces per indentation level. OneVision usually adds indentation automatically; however, you should ensure it is correct, especially after copying and pasting code.
function example() {
if (condition) {
// Indented code
}
}
Braces
Place opening braces { on the same line as the statement and closing braces } on a new line aligned with the start of the statement.
if (condition) {
// Code block
} else {
// Code block
}
Spacing
Use spaces around operators and after commas to enhance readability.
var sum = a + b;
function example(param1: number, param2: number) {
// Code block
}
Comments
Use comments to explain the purpose of code blocks, especially complex logic. Use single-line comments (//) for short explanations and multi-line comments (/** ... */) for detailed explanations. Make sure you add a simple explanation for all global variables, functions, and function blocks using multi-line comments.
// Single-line comment
var x = 10; // Initialize x to 10
/** Global variable with description*/
global var y = 100;
/**
* Multi-line comment
* This function calculates the sum of two numbers.
*/
function add(a: number, b: number): number {
return a + b;
}
Naming Conventions
Variables and functions
Use camelCase for variable and function names. Names should be descriptive and concise.
var firstName = "John";
function calculateSum(a: number, b: number): number {
return a + b;
}
Constants
Use UPPER_CASE_WITH_UNDERSCORES for constant values. Add a description for each constant variable.
/** Maximum number of users */
const global var MAX_USERS = 100;
Function blocks
Use PascalCase for function block names.
block UserAccount {
in var var2;
in var var1;
out var result;
result = var1 + var2;
}
Interfaces
Use PascalCase prefixed with I for interfaces.
interface IUser {
name: string;
age: number;
}