Formatting

In this course, you will learn about the various ways to format your HaasScript code for improved readability and organization. By the end of this course, you should be able to:

  1. Use proper indentation to structure your code
  2. Use whitespace to improve readability
  3. Use comments to document your code
  4. Follow naming conventions for variables and functions
  5. Use best practices for formatting control structures

Proper Indentation

Indentation is the use of whitespace at the beginning of a line to visually structure code. In HaasScript, it is recommended to use 4 spaces or tabs per indentation level. This helps to clearly define the hierarchy and structure of your code, making it easier to read and understand.

Whitespace

In addition to indentation, you can use whitespace within your code to improve readability. This includes adding blank lines between functions and blocks of code, and using spaces around operators and after commas.

The recommended line length for LUA code is 80 characters. This is a commonly accepted standard in the programming community, as it allows code to be easily read and shared without requiring horizontal scrolling. It is important to bear in mind that this is a guideline, and there may be cases where it is appropriate to exceed the line length limit. However, in general, it is a good practice to aim for code that is easily readable and fits within the recommended line length.

Comments

Comments are lines of code that are not executed, but are used to add explanations or notes to your code. They are an important tool for documenting your code and making it easier for others (or future you) to understand. In HaasScript, you can create a single-line comment by using “–” and a multi-line comment by using “–[[ ]]”.

Naming Conventions

It is important to follow consistent naming conventions for variables and functions in your HaasScript code. This helps to make your code more predictable and easier to read. Some common naming conventions for HaasScript include:

  • Use lowercase letters and underscores for variables and functions
  • Use camelCase for names that consist of multiple words
  • Use descriptive names for variables and functions

Control Structures

Control structures are used to control the flow of your code, such as looping through a list or conditional statements. In HaasScript, it is recommended to follow the following formatting conventions for control structures:

  • Place the opening brace on its own line
  • Indent the code inside the control structure to the same level as the control structure
  • Place the closing brace on its own line, aligned with the opening brace

For example:

if condition then
  -- code
elseif condition then
  -- code
else
  -- code
end
Back to: HaasScript Fundamentals > Getting Started