JerryScript Coding Standards

This text is a brief overview of JerryScript Coding Standards. Each rule starts with a short description followed by several examples. We believe examples are better than long explanations. Please follow these guidelines when you submit a patch for review.

General rules

  • Indentation is two spaces.
  • Tab characters are not allowed.
  • Maximum line length is 120 characters (excluding newline).
  • No trailing white space is allowed.
  • Run tools/run-tests.py --check-vera to check several of the coding conventions automatically.

Comments

Only block comments (/* */) are allowed in JerryScript. Comments should be complete sentences (e.g. start with an upper case letter), except for return value, field and argument descriptions (see the exceptions below). The text part of a comment should focus on explaining why the code is doing something rather than what the code is doing.

  1. +++ Good +++
  1. /* A perfect comment. */
  2. /* A perfect multiline
  3. * comment. Each line should
  4. * start with an asterisk. */
  1. --- Bad ---
  1. // Double slash comments are not allowed.
  2. /* not a complete sentence */
  3. /* A bad mulitiline
  4. comment. */

All types, constants and functions require a description in JerryScript. These comments should start with /**. The starting /** and ending */ must be on separate lines.

  1. +++ Good +++
  1. /**
  2. * A correct description.
  3. */
  1. --- Bad ---
  1. /** An incorrect description. */

Preprocessor defines

The name of a preprocessor macro must be an uppercase string and these macros must be preceded by a description. Abbreviations are allowed but not preferred in new code.

  1. +++ Good +++
  1. /**
  2. * Short description about the constant.
  3. */
  4. #define JERRY_VALUE_SEVEN 7
  5. /**
  6. * Short description about the macro function.
  7. */
  8. #define JERRY_ADD_TWO_NUMBERS(arg1, arg2) \
  9. ((arg1) + (arg2))
  10. /**
  11. * Although this is correct, a reviewer might request
  12. * to change NUMS to NUMBERS. Hence it is recommended
  13. * to use NUMBERS in the first place.
  14. */
  15. #define JERRY_ADD_TWO_NUMS(arg1, arg2) \
  16. ((arg1) + (arg2))
  1. --- Bad ---
  1. #define JERRY_CONSTANT_WITHOUT_DESCRIPTION 5
  2. #define JeRrY_mIxEd_CaSe_NaMe "str"

Conditional preprocessor directives

A comment is required after #else and #endif in JerryScript. The defined keyword should be omitted from these comments.

  1. +++ Good +++
  1. #ifdef JERRY_A
  2. #else /* !JERRY_A */
  3. #endif /* JERRY_A */
  4. #ifdef JERRY_A
  5. #if defined JERRY_B && defined JERRY_C && (JERRY_C > 6)
  6. #else /* !(JERRY_B && JERRY_C && (JERRY_C > 6)) */
  7. #endif /* JERRY_B && JERRY_C && (JERRY_C > 6) */
  8. #endif /* JERRY_A */
  1. --- Bad ---
  1. #ifdef JERRY_A
  2. #endif
  3. #ifdef JERRY_A
  4. #endif /* defined JERRY_A */
  5. #ifdef JERRY_B
  6. /* Missing comment after else. */
  7. #else
  8. #endif /* JERRY_B */

Code blocks

Each code block must be enclosed in curly braces even if it is a single line statement. These braces must be on separate lines. There must be a single space before the opening parenthesis of the expression after if/while/switch keywords.

  1. +++ Good +++
  1. if (value > 6)
  2. {
  3. function_call ();
  4. }
  5. if (value > 1)
  6. {
  7. function_call_a ();
  8. }
  9. else
  10. {
  11. function_call_b ();
  12. }
  13. do
  14. {
  15. function_call ();
  16. value++;
  17. }
  18. while (value < 6);
  19. switch (value)
  20. {
  21. case A:
  22. {
  23. /* FALLTHRU comment is allowed if a
  24. * switch-case is not terminated by
  25. * break/continue/return. */
  26. /* FALLTHRU */
  27. }
  28. case B:
  29. case C:
  30. {
  31. break;
  32. }
  33. case D:
  34. {
  35. /* We can use continue if we are in a loop. */
  36. continue;
  37. }
  38. default:
  39. {
  40. return;
  41. }
  42. }
  1. --- Bad ---
  1. if (value > 6)
  2. function_call_a ();
  3. else
  4. function_call_b ();
  5. if (value > 6) {
  6. function_call_a ();
  7. }
  8. if (value > 6) function_call_a ();
  9. else { function_call_b (); }
  10. if
  11. (value > 6)
  12. {
  13. }
  14. switch (value) {
  15. case 0: break;
  16. default: {
  17. return 5;
  18. }
  19. }
  20. switch (value)
  21. {
  22. case A:
  23. {
  24. if (value > 6)
  25. {
  26. CASE B:
  27. {
  28. /* This is allowed in C but
  29. * not in JerryScript. */
  30. break;
  31. }
  32. }
  33. }
  34. }
  35. do
  36. value++;
  37. while (value < 5);
  38. do {
  39. value++;
  40. } while (value < 5);
  41. do
  42. {
  43. value++;
  44. } while (value < 5);

Newlines

A newline in JerryScript is a separator which separates different parts of the source code. Its primary purpose is to improve readability. Unlike other rules developers have some freedom to add newlines to their code. However there are some rules.

  • Only a single newline separator is allowed.
  • Source files must be terminated by a newline.
  • Global declarations must be separated by a newline.
  • Newlines are not allowed after an opening curly brace or before a closing curly brace
  • No newlines are allowed between control statements (if-else, while, for, switch, etc.) and their code blocks.
  • There should be a newline after the variable declarations if they are the first statements of a block.
  1. +++ Good +++
  1. if (a > 5)
  2. {
  3. /* Newline must be present after the first
  4. * variable declarations of a code block. */
  5. int j = a - 1;
  6. int k = a * 2;
  7. return j + k;
  8. }
  9. while (a < 5)
  10. {
  11. a++;
  12. /* It is recommended to put a newline after
  13. * intermediate variable declarations. */
  14. int i = a * 2;
  15. b = i - 3;
  16. }
  17. /* It is a recommended to put newlines around asserts. */
  18. a = b + 5;
  19. JERRY_ASSERT (a < 20);
  20. c = a + 7;
  21. /* It is a good practice to put a newline after a multiline
  22. * function call (see Function calls later). */
  23. f (a,
  24. b,
  25. c);
  26. a = 6;
  1. --- Bad ---
  1. /* No newlines are allowed after an opening curly
  2. * brace or before a closing curly brace */
  3. while (a > 0)
  4. {
  5. a = 6;
  6. }
  7. if (a > 5)
  8. {
  9. while (b < 6)
  10. {
  11. b++;
  12. }
  13. }
  14. /* Two or more newlines are not allowed. */
  15. a = 6;
  16. b = 7;
  17. /* No newlines are allowed between control staments
  18. * and their code blocks. */
  19. if (a > 6)
  20. {
  21. }
  22. else
  23. {
  24. }
  25. do
  26. {
  27. }
  28. while (a < 6);

Expressions

Spaces are required around binary operators. No space is needed otherwise.

  1. +++ Good +++
  1. a = b + c;
  2. a = (b + c) << 3;
  3. a = b = c + ~d;
  4. a += ++c + d++;
  5. call_function (a * (b + !!c) - d + (e % f));
  6. if (a)
  7. {
  8. }
  1. --- Bad ---
  1. a=b+c;
  2. a = b+c;
  3. a += c + ( d );
  4. /* Introduce temporary variables or macros
  5. * if the expression is too long. Occures rarely.. */
  6. a = b
  7. + c;
  8. if ( a + b > 0 )
  9. {
  10. }

Logical operators

All expressions with && and || logical operators must be enclosed in parentheses. A single and multiline form is allowed for these expressions. In the latter case each line must start with the logical operator and each line must be aligned to the column right after the opening parenthesis.

  1. +++ Good +++
  1. /* Single line form. */
  2. a = ((c && d) || (e && f));
  3. a = (c
  4. && d);
  5. a = (c
  6. && (d || e)
  7. && f);
  8. do
  9. {
  10. }
  11. while (a
  12. && b);
  13. /* This form is rarely used but it is ok. */
  14. if (a
  15. && (b
  16. || c
  17. || d)
  18. && e)
  19. {
  20. }
  1. --- Bad ---
  1. if (a || b ||
  2. c)
  3. {
  4. }
  5. /* Parentheses are missing. */
  6. a = b || c;
  7. /* Misaligned &&. */
  8. if (a
  9. && b)
  10. {
  11. }

Ternary conditional operators

A special form of ternary conditional operators are allowed in JerryScript where the ? and : operators are on separate lines in the same column.

  1. +++ Good +++
  1. a = (b ? c
  2. : d);
  3. /* Single line form is accepted as well. */
  4. a = (b ? c : d);
  5. /* This form is rarely used but it is ok. */
  6. if (a ? b
  7. : (c ? d
  8. : e))
  9. {
  10. }
  1. --- Bad ---
  1. a = b ?
  2. c : d;
  3. while (a ? b
  4. : c)
  5. {
  6. }
  7. if (a
  8. ? b
  9. : c)
  10. {
  11. }

Function calls

There must be a space after the function name. Each argument must be in the same or separated lines. In the former case there must be a space before the next argument and in the latter case all arguments must be aligned to the same column.

  1. +++ Good +++
  1. function_a ();
  2. function_b (a);
  3. function_c (a, b, c);
  4. function_c (a,
  5. b,
  6. c);
  7. function_c (a,
  8. b,
  9. function_c (a,
  10. b,
  11. c);
  1. --- Bad ---
  1. /* No space before the opening parenthesis. */
  2. function_f();
  3. function_f (
  4. );
  5. function_g(a);
  6. function_g
  7. (a
  8. );
  9. /* Two arguments on the same line. */
  10. function_h (a, b,
  11. c);
  12. function_h (a,
  13. b, c);
  14. /* Misaligned arguments. */
  15. function_h (a,
  16. b,
  17. c);

Variable declarations

JerryScript is a pure C99 codebase so variable declarations can be anywhere in the code including inside for-loops. It is recommended to declare a variable before the first use.

  1. +++ Good +++
  1. for (int i = 0; i < 10; i++)
  2. {
  3. int j = i + 1;
  4. while (j < 10)
  5. {
  6. ++j;
  7. }
  8. }
  9. /* Multiline form of for loops. */
  10. for (int i = 0;
  11. i < 10;
  12. i++)
  13. {
  14. }

Type casting

There must be a space after the closing parenthesis of the type cast. Type casting has no multiline form in JerryScript.

  1. +++ Good +++
  1. int a = (int) double_variable;
  2. int a = (int) (long) (float) double_variable;
  1. --- Bad ---
  1. /* Wrong spaces. */
  2. int a = ( int )double_variable;
  3. /* No multiline form. */
  4. int a = (int)
  5. double_variable;

Pointers and asterisk character

Each pointer in JerryScript must be a lowercase string which is ending with a _p suffix. Furthermore there must be a space before the asterisk character.

  1. +++ Good +++
  1. int *int_p;
  2. /* No need to add multiple _p-s for multiple indirections.
  3. * It is recommended to avoid these constructs using typedef
  4. * declarations. A reviewer might request such a change. */
  5. int ***int_p;
  6. /* This rule applies for type casting as well. */
  7. char = *(char *) type_p;
  1. --- Bad ---
  1. /* No _p after the name. */
  2. int *ptr;
  3. /* Wrong asterisk position. */
  4. int* ptr_p;
  5. char_p = * (char*)type_p;

Types

Each type in JerryScript must be a lowercase string which ends with a _t suffix. Furthermore each type declaration must be preceded by a short description of the type and each field must have a short description as well.

  1. +++ Good +++
  1. /**
  2. * Short description of the following structure.
  3. */
  4. typedef struct
  5. {
  6. /* Field descriptions do not start with capital letters
  7. * and there is no full stop at the end. */
  8. field1_t field1; /**< description of field 1 */
  9. field2_t field2; /**< description of field 2 */
  10. field_n_t field_n; /**< description of field n */
  11. } structure_name_t;
  12. /**
  13. * Another integer type.
  14. */
  15. typedef int jerry_int;
  1. --- Bad ---
  1. typedef struct
  2. {
  3. field_t field_without_description;
  4. } structure_without_description_t;
  5. typedef struct { int a; } single_line_struct;
  6. typedef
  7. union {
  8. }
  9. wrong_newlines_t;
  10. /*
  11. * Bad comment format.
  12. */
  13. typedef
  14. char wrong_newlines_again_t;

Function declarations

Function declarations in JerryScript are verbose but this format reduces the maintenance cost and allows faster understanding of the code.

  1. +++ Good +++
  1. /**
  2. * Short overview about the purpose of this function.
  3. *
  4. * A more detailed explanation if needed.
  5. *
  6. * Note:
  7. * Extra notes if needed.
  8. *
  9. * @return short description about the value
  10. * returned by the function
  11. */
  12. return_value_type_t
  13. function_name (argument1, /**< description of argument1 */
  14. argument2, /**< description of argument2 */
  15. ...
  16. argument_n, /**< description of argument n */
  17. {
  18. /* Function body. */
  19. } /* function_name */
  1. --- Bad ---
  1. static int
  2. print (char *text) /**< description of text argument */
  3. {
  4. /* Missing comment before the function. */
  5. } /* print */
  6. /**
  7. * Prints the text received by the function.
  8. *
  9. * @return number of characters printed by the function
  10. */
  11. int print(char *text)
  12. {
  13. /* No description of text argument. */
  14. /* Missing comment at the end of the function. */
  15. }