DEV Community

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

Posted on

ArkTS programming specification(3)

Expressions and Formatting

Expression Line Breaks

  • Level: Recommendation
  • Description: When a statement is too long or readability is poor, break lines appropriately. Place operators at the end of lines to indicate continuation and align with common formatting tool configurations.

  • Positive Example:

// Assuming 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 declare only one variable. This approach simplifies adding new declarations and avoids errors when changing ; to ,. It also makes debugging easier by allowing step - by - step examination of each variable.

  • 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

Use of Spaces

  • Level: Recommendation
  • Description: Spaces should highlight keywords and important information. Follow these guidelines:

    • Add a space between keywords (if, for, while, switch) and the left parenthesis (.
    • No space between function names and the left parenthesis ( in function definitions and calls.
    • Add a space between else or catch and the preceding right brace }.
    • Add a space before any left brace {, except:
      • When the object is the first parameter of a function or the first element of an array, no space is needed before the object, e.g., foo({ name: 'abc' }).
      • In templates, no space is needed, e.g., abc${name}.
    • Add spaces around binary operators (+ - * = < > <= >= === !== && ||) and on both sides of the ternary operator (? :).
    • Add a space after commas in array initializations and between parameters in functions.
    • No space before commas (,) or semicolons (;).
    • Do not add spaces inside the brackets of arrays ([]).
    • Avoid multiple consecutive spaces unless they are used for indentation.
  • Negative Example:

// No space between if and the left parenthesis (
if(isJedi) {
  fight();
}

// Space between function name fight and the left parenthesis (
function fight (): void {
  console.log('Swooosh!');
}
Enter fullscreen mode Exit fullscreen mode
  • Positive Example:
// Space between if and the left parenthesis (
if (isJedi) {
  fight();
}

// No space between function name fight and the left parenthesis (
function fight(): void {
  console.log('Swooosh!');
}
Enter fullscreen mode Exit fullscreen mode
  • Negative Example:
if (flag) {
  // ...
}else {  // No space between else and the preceding right brace }
  // ...
}
Enter fullscreen mode Exit fullscreen mode
  • Positive Example:
if (flag) {
  // ...
} else {  // Add space between else and the preceding right brace }
  // ...
}
Enter fullscreen mode Exit fullscreen mode
  • Positive Example:
function foo() {  // Add a space before the left brace { in function declarations
  // ...
}

bar('attr', {  // Add a space before the left brace {
  age: '1 year',
  sbreed: 'Bernese Mountain Dog',
});
Enter fullscreen mode Exit fullscreen mode
  • Positive Example:
const arr = [1, 2, 3];  // Add a space after commas in array initializations
myFunc(bar, foo, baz);  // Add a space after commas between function parameters
Enter fullscreen mode Exit fullscreen mode

String Quotation Marks

  • Level: Recommendation
  • Description: Single quotes are generally preferred over double quotes.

  • Negative Example:

let message = "world";
console.log(message);
Enter fullscreen mode Exit fullscreen mode
  • Positive Example:
let message = 'world';
console.log(message);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)