DEV Community

HarmonyOS
HarmonyOS

Posted on • Edited on

ArkTS Coding Style Guide: Writing Clean, Readable, and Efficient Code

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) { ... }
Enter fullscreen mode Exit fullscreen mode

🟡 Use UpperCamelCase for Classes, Enums, and Namespaces

class UserProfile { ... }
enum AccountType { ADMIN, USER }
namespace AuthUtils { ... }
Enter fullscreen mode Exit fullscreen mode

🟡 Use lowerCamelCase for Variables, Methods, and Parameters

let maxRetry = 5;
function handleLogin(userName: string) { ... }
Enter fullscreen mode Exit fullscreen mode

🟡 Use UPPER_CASE_WITH_UNDERSCORES for Constants

const MAX_USER_COUNT = 10000;
Enter fullscreen mode Exit fullscreen mode

🟡 Avoid Negative Boolean Names: Use positive forms with clear prefixes like , , .ishascan

let isEnabled = true
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

🟡 Indent and in Blockscasedefaultswitch

switch (status) {
  case 0: {
    handleIdle();
    break;
  }
  default:
    break;
}

Enter fullscreen mode Exit fullscreen mode

🟡 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;
Enter fullscreen mode Exit fullscreen mode

🟡 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';
Enter fullscreen mode Exit fullscreen mode

🟡 Object Literals with >4 Properties: Multi-Line Format

let obj = {
  id: 1,
  name: 'Tom',
  age: 30,
  active: true,
  role: 'admin'
};
Enter fullscreen mode Exit fullscreen mode

🟡 and Should Follow Closing Braces on Same Lineelsecatch

if (isActive) {
  start();
} else {
  stop();
}
Enter fullscreen mode Exit fullscreen mode

🟡 Opening Braces Stay on the Same Line

function init() {
  // ...
}
Enter fullscreen mode Exit fullscreen mode

Programming Practices

🟡 Use Access Modifiers: Always specify , , or to clarify intent.privateprotectedpublic

class Counter {
  private value: number = 0;

  public increment(): void {
    this.value++;
  }
}

Enter fullscreen mode Exit fullscreen mode

🟡 Write Readable Floating-Point Numbers

let pi = 3.14;
let ratio = 0.75;
Enter fullscreen mode Exit fullscreen mode

🟡 Use to Check for NaNNumber.isNaN()

if (Number.isNaN(value)) { ... }
Enter fullscreen mode Exit fullscreen mode

🟡 Prefer Array Methods Over Loops

const doubled = numbers.map(n => n * 2);
Enter fullscreen mode Exit fullscreen mode

🟡 Avoid Assignments in Conditions: Separate the assignment from the conditional logic.

let isReady = checkStatus();
if (isReady) { ... }
Enter fullscreen mode Exit fullscreen mode

🟡 Never Use , , , or in Blocks: Doing so can suppress critical errors or override return values.returnthrowbreakcontinuefinally

try {
  return fetchData();
} catch (err) {
  handleError(err);
} finally {
  log('Completed');
}
Enter fullscreen mode Exit fullscreen mode

🟡 Avoid Using : Use typed interfaces and native types wherever possible to prevent cross-language overhead.ESObject

let obj: I = getObject(123);
Enter fullscreen mode Exit fullscreen mode

🟡 Prefer over T[]Array<T>

let names: string[] = ['Alice', 'Bob'];
Enter fullscreen mode Exit fullscreen mode

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.

References

https://developer.huawei.com/consumer/en/doc/harmonyos-guides/arkts-coding-style-guide?source=post_page-----e4a9c94c02d7---------------------------------------

https://medium.com/huawei-developers/arkts-coding-style-guide-writing-clean-readable-and-efficient-code-e4a9c94c02d7

Written by Emine INAN

Top comments (0)