DEV Community

Cover image for ArkTS programming specification(2)
liu yang
liu yang

Posted on

ArkTS programming specification(2)

Formatting Guidelines

Indentation with Spaces

  • Level: Recommendation
  • Description: Use spaces for indentation. It is recommended to use two spaces in most cases and four spaces for indentation caused by line breaks. Do not use the Tab character. Almost all IDEs and code editors support configuring the Tab key to automatically expand to two spaces. Configure your code editor to use spaces for indentation.

  • Positive Example:

class DataSource {
  id: number = 0;
  title: string = '';
  content: string = '';
}

const dataSource: DataSource[] = [
  {
    id: 1,
    title: 'Title 1',
    content: 'Content 1'
  },
  {
    id: 2,
    title: 'Title 2',
    content: 'Content 2'
  }
];

function test(dataSource: DataSource[]) {
  if (!dataSource.length) {
    return;
  }

  for (let data of dataSource) {
    if (!data || !data.id || !data.title || !data.content) {
      continue;
    }
    // some code
  }

  // some code
}
Enter fullscreen mode Exit fullscreen mode

Line Length

  • Level: Recommendation
  • Description: Lines of code should not be too long. Limiting line length can encourage shorter function and variable names, reduce nesting levels, and improve code readability. It is recommended to keep lines within 120 characters unless exceeding this limitๆ˜พ่‘— improves readability and does not hide information.
  • Exception: If a line contains a command or URL exceeding 120 characters, it can remain as one line for easy copying, pasting, and searching with grep. Preprocessed error information can also stay on one line if it exceeds 120 characters for easier reading and understanding.

Braces in Control Structures

  • Level: Recommendation
  • Description: Always use braces {} for the bodies of if, for, do, while statements. Omitting braces can lead to errors and reduce code clarity.

  • Negative Example:

if (condition)
  console.log('success');

for (let idx = 0; idx < 5; ++idx)
  console.log(idx);
Enter fullscreen mode Exit fullscreen mode
  • Positive Example:
if (condition) {
  console.log('success');
}

for (let idx = 0; idx < 5; ++idx) {
  console.log(idx);
}
Enter fullscreen mode Exit fullscreen mode

Indentation in switch Statements

  • Level: Recommendation
  • Description: In switch statements, case and default should be indented by one level (two spaces). Statements following the switch labels should be indented by an additional level (two spaces).

  • Positive Example:

switch (condition) {
  case 0: {
    doSomething();
    break;
  }
  case 1: {
    doOtherthing();
    break;
  }
  default:
    break;
}
Enter fullscreen mode Exit fullscreen mode

Expression Line Breaks

  • Level: Recommendation
  • Description: When a statement is too long or has poor readability, break lines appropriately. Place operators at the end of lines to indicate continuation. This aligns with common formatter configurations.

  • Positive Example:

// If the conditional statement exceeds the line width
if (userCount > MAX_USER_COUNT ||
  userCount < MIN_USER_COUNT) {
  doSomething();
}
Enter fullscreen mode Exit fullscreen mode

Variable Declarations and Assignments

  • Level: Requirement
  • Description: Each variable declaration should be on a separate line.

  • Negative Example:

let maxCount = 10, isCompleted = false;
let pointX, pointY;
pointX = 10; pointY = 0;
Enter fullscreen mode Exit fullscreen mode
  • Positive Example:
let maxCount = 10;
let isCompleted = false;
let pointX = 0;
let pointY = 0;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)