HarmonyOS Development: ArkTs Language Variables and Constants

AbnerMingAbnerMing
3 min read

Foreword

this article code case based on api13.

It is precisely because of variables and constants that there is data storage and management between programs. Whether it is variables or constants, when naming them, one must be well-known and be able to express the declared intention well. This is very important. Never define them in pinyin or simple letters. For nothing else, I am afraid that others will laugh at you.

Variable (Variable)

A variable is a container used to store data, and the value of the stored data can be changed during the execution of the program. A variable usually has a name (identifier) used to refer to it in the program.

Characteristics of variables

1. Naming: Variables need to have a legal identifier (usually a combination of letters, numbers or underscores, but not a number), usually declared in the form of a small hump.

2. Data type: Variables can store specific types of data, such as integers, floating-point numbers, strings, Boolean values, etc.

Variability: The value of a variable can be read and modified while the program is running.

Variable Declaration

by keyword let the declaration at the beginning introduces a variable that can have different values during program execution.

let hi: string = 'hello'
hi = 'hello, world'

let age = 18

Constant (Constant)

A constant is an identifier used to store data, but its value cannot be changed during program execution. Constants are often used to define values that do not change in a program, such as Pi (PI), the number of days in a year, and so on.

Characteristics of Constants

1. Naming: Constants are usually named with all capital letters to distinguish them from variables. For example, PI , MAX_VALUE .

2. Data type: Constants can store specific types of data.

3. Immutability: The value of a constant cannot be modified after it is defined. An error or warning is usually raised if a modification is attempted.

Constant declaration

A declaration beginning with the keyword const introduces a read-only constant, which can only be assigned once.

const HELLO: string = 'hello'
const AGE: number = 18

Re-assigning a constant causes a compile-time error.

Naming Rules

1. Beginning character: Variable names must begin with a letter, underscore, or dollar sign.

2. Prohibit the use of keywords: variable names cannot be the same as keywords in Java language, such as number, string, for, if, etc.

3. Descriptive: A good variable name should be able to clearly express the purpose and meaning of the variable.

4, hump nomenclature: recommend use hump nomenclature to name variables, including small hump nomenclature (lowerCamelCase) and large hump nomenclature (UpperCamelCase).

5. Constant naming: Constant names are usually all uppercase letters, and words are separated by underscores, such as MAX_VALUE.

Statement Method

if it is a local variable, that is, within the method, you must bring the keyword, that is, let or const, if it is a member variable, you can omit it.

A member variable, also known as a member property or field, is a variable defined in a class.

A local variable is a variable defined inside a method, code block, or loop, etc.

Local variable definition

let name = "AbnerMing"
let age = 18

member variable definition

name = "AbnerMing"
age = 18

summary

meaningful: Use meaningful nomenclature to help other developers quickly understand the purpose of variables and improve code readability.

2, consistency: to maintain the consistency of naming style, easy to understand and cooperate with team members. For example, in a team project, you can agree to use the small hump nomenclature or the underscore nomenclature.

Avoid conflicts: Avoid reserved words and conflicting names to prevent syntax errors and naming conflicts.

4. Concise and clear: Try to keep variable names short and clear while remaining descriptive. Avoid using long variable names to reduce the difficulty of reading.

0
Subscribe to my newsletter

Read articles from AbnerMing directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

AbnerMing
AbnerMing