Read the original article:ArkTS Coding Style Guide: Writing Clean, Readable, and Efficient Code
Introduction
As application development becomes more complex and cross-platform, writing high-quality, maintainable code is essential. ArkTS, the HarmonyOS programming language, offers powerful features but requires consistent and disciplined coding practices. This article provides structured recommendations to improve readability, performance, and maintainability, highlighting best practices and common mistakes.
Purpose and Scope
The ArkTS Coding Style Guide builds upon industry standards, particularly from the TypeScript and JavaScript ecosystems, while introducing rules specific to ArkTS. The goal is to enhance:
- Code standardization
- Execution performance
- Static analysis capabilities
- Security and clarity in collaborative environments
It is designed for use during both system and application-level development in the ArkTS ecosystem.
Naming Conventions
🟡 Use Clear, Descriptive Identifiers: Avoid single letters, abbreviations, or non-English terms. Always aim for identifiers that clearly express intent.
let userName = 'JohnDoe';
function sendMessage(message: string) { ... }
🟡 Use UpperCamelCase for Classes, Enums, and Namespaces
class UserProfile { ... }
enum AccountType { ADMIN, USER }
namespace AuthUtils { ... }
🟡 Use lowerCamelCase for Variables, Methods, and Parameters
let maxRetry = 5;
function handleLogin(userName: string) { ... }
🟡 Use UPPER_CASE_WITH_UNDERSCORES for Constants
const MAX_USER_COUNT = 10000;
🟡 Avoid Negative Boolean Names: Use positive forms with clear prefixes like is, has, can.
let isEnabled = true
Formatting Rules
🟡 Indentation: Use 2 Spaces: Consistent two-space indentation improves readability across devices and editors.
🟡 Line Length: Max 120 Characters: Keep lines concise. Use line breaks for long conditions or expressions.
🟡 Use Braces for All Conditional and Loop Blocks: Even for one-line blocks.
if (isValid) {
process();
}
🟡 Indent case and default in switch Blocks
switch (status) {
case 0: {
handleIdle();
break;
}
default:
break;
}
🟡 Consistent Line Breaks in Expressions: Operators should appear at the end of lines when breaking long expressions.
🟡 One Variable Declaration per Line
let count = 0;
let isReady = true;
🟡 Space Usage
- Add spaces around operators and after commas.
- No space before commas or semicolons.
- No space before method call parentheses.
🟡 Use Single Quotes for Strings
let message = 'Hello ArkTS';
🟡 Object Literals with >4 Properties: Multi-Line Format
let obj = {
id: 1,
name: 'Tom',
age: 30,
active: true,
role: 'admin'
};
🟡 else and catch Should Follow Closing Braces on Same Line
if (isActive) {
start();
} else {
stop();
}
🟡 Opening Braces Stay on the Same Line
function init() {
// ...
}
Programming Practices
🟡 Use Access Modifiers: Always specify private, protected, or public to clarify intent.
class Counter {
private value: number = 0;
public increment(): void {
this.value++;
}
}
🟡 Write Readable Floating-Point Numbers
let pi = 3.14;
let ratio = 0.75;
🟡 Use Number.isNaN() to Check for NaN
if (Number.isNaN(value)) { ... }
🟡 Prefer Array Methods Over Loops
const doubled = numbers.map(n => n * 2);
🟡 Avoid Assignments in Conditions: Separate the assignment from the conditional logic.
let isReady = checkStatus();
if (isReady) { ... }
🟡 Never Use return, throw, break, or continue in finally Blocks: Doing so can suppress critical errors or override return values.
try {
return fetchData();
} catch (err) {
handleError(err);
} finally {
log('Completed');
}
🟡 Avoid Using ESObject: Use typed interfaces and native types wherever possible to prevent cross-language overhead.
let obj: I = getObject(123);
🟡 Prefer T[] over Array<T>
let names: string[] = ['Alice', 'Bob'];
Conclusion
Adopting the ArkTS Coding Style Guide is essential for developers aiming to write clean, secure, and performant code within the HarmonyOS ecosystem. Following these conventions helps teams reduce bugs, improve collaboration, and maintain code quality over time. By coding clearly and purposefully, you’re not just writing code — you’re shaping the future of cross-platform applications with ArkTS.
Top comments (0)