aboutsummaryrefslogtreecommitdiff
path: root/08.CODING-STANDARDS.md
blob: 7b2dc01cf36140ea9e4a2daaf1879eadf404b601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
---
layout: page
title: Coding Standards
category: documents
permalink: /coding-standards/
---

* toc
{:toc}

# 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-format` 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.

```diff
+++ Good +++
```

```c
  /* A perfect comment. */

  /* A perfect multiline
   * comment. Each line should
   * start with an asterisk. */
```

```diff
--- Bad ---
```

```c
  // Double slash comments are not allowed.

  /* not a complete sentence */

  /* A bad multiline
     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.

```diff
+++ Good +++
```

```c
/**
 * A correct description.
 */
```

```diff
--- Bad ---
```

```c
/** 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.

```diff
+++ Good +++
```

```c
/**
 * Short description about the constant.
 */
#define JERRY_VALUE_SEVEN 7

/**
 * Short description about the macro function.
 */
#define JERRY_ADD_TWO_NUMBERS(arg1, arg2) \
  ((arg1) + (arg2))

/**
 * Although this is correct, a reviewer might request
 * to change NUMS to NUMBERS. Hence it is recommended
 * to use NUMBERS in the first place.
 */
#define JERRY_ADD_TWO_NUMS(arg1, arg2) \
  ((arg1) + (arg2))
```

```diff
--- Bad ---
```

```c
#define JERRY_CONSTANT_WITHOUT_DESCRIPTION 5

#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.

```diff
+++ Good +++
```

```c
#ifdef JERRY_A

#else /* !JERRY_A */

#endif /* JERRY_A */

#ifdef JERRY_A
#if defined JERRY_B && defined JERRY_C && (JERRY_C > 6)

#else /* !(JERRY_B && JERRY_C && (JERRY_C > 6)) */

#endif /* JERRY_B && JERRY_C && (JERRY_C > 6) */
#endif /* JERRY_A */
```

```diff
--- Bad ---
```

```c
#ifdef JERRY_A

#endif

#ifdef JERRY_A

#endif /* defined JERRY_A */

#ifdef JERRY_B
  /* Missing comment after else. */
#else

#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.

```diff
+++ Good +++
```

```c
if (value > 6)
{
  function_call ();
}

if (value > 1)
{
  function_call_a ();
}
else
{
  function_call_b ();
}

do
{
  function_call ();
  value++;
}
while (value < 6);

switch (value)
{
  case A:
  {
    /* FALLTHRU comment is allowed if a
     * switch-case is not terminated by
     * break/continue/return. */

    /* FALLTHRU */
  }
  case B:
  case C:
  {
    break;
  }
  case D:
  {
    /* We can use continue if we are in a loop. */
    continue;
  }
  default:
  {
    return;
  }
}
```

```diff
--- Bad ---
```

```c
if (value > 6)
  function_call_a ();
else
  function_call_b ();

if (value > 6) {
  function_call_a ();
}

if (value > 6) function_call_a ();
else { function_call_b (); }

if
(value > 6)
{
}

switch (value) {
  case 0: break;
  default: {
    return 5;
  }
}

switch (value)
{
  case A:
  {
    if (value > 6)
    {
      CASE B:
      {
        /* This is allowed in C but
         * not in JerryScript. */
        break;
      }
    }
  }
}

do
  value++;
while (value < 5);

do {
  value++;
} while (value < 5);

do
{
  value++;
} 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.

```diff
+++ Good +++
```

```c
if (a > 5)
{
  /* Newline must be present after the first
   * variable declarations of a code block. */
  int j = a - 1;
  int k = a * 2;

  return j + k;
}

while (a < 5)
{
  a++;

  /* It is recommended to put a newline after
   * intermediate variable declarations. */
  int i = a * 2;

  b = i - 3;
}

/* It is a recommended to put newlines around asserts. */
a = b + 5;

JERRY_ASSERT (a < 20);

c = a + 7;

/* It is a good practice to put a newline after a multiline
 * function call (see Function calls later). */
f (a,
   b,
   c);

a = 6;
```

```diff
--- Bad ---
```

```c
/* No newlines are allowed after an opening curly
 * brace or before a closing curly brace */

while (a > 0)
{

  a = 6;

}

if (a > 5)
{
  while (b < 6)
  {

    b++;
  }

}


/* Two or more newlines are not allowed. */
a = 6;


b = 7;

/* No newlines are allowed between control statements
 * and their code blocks. */

if (a > 6)

{
}

else

{
}

do

{
}

while (a < 6);
```

## Expressions

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

```diff
+++ Good +++
```

```c
a = b + c;
a = (b + c) << 3;
a = b = c + ~d;
a += ++c + d++;
call_function (a * (b + !!c) - d + (e % f));
if (a)
{
}
```

```diff
--- Bad ---
```

```c
a=b+c;
a = b+c;
a  +=  c  +  ( d );
/* Introduce temporary variables or macros
 * if the expression is too long. Occurs rarely.. */
a = b
  + c;
if ( a + b > 0 )
{
}
```

## 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.

```diff
+++ Good +++
```

```c
/* Single line form. */
a = ((c && d) || (e && f));

a = (c
     && d);

a = (c
     && (d || e)
     && f);

do
{
}
while (a
       && b);

/* This form is rarely used but it is ok. */
if (a
    && (b
        || c
        || d)
    && e)
{
}
```

```diff
--- Bad ---
```

```c
if (a || b ||
    c)
{
}

/* Parentheses are missing. */
a = b || c;

/* Misaligned &&. */
if (a
  && b)
{
}
```

## 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.

```diff
+++ Good +++
```

```c
a = (b ? c
       : d);

/* Single line form is accepted as well. */
a = (b ? c : d);

/* This form is rarely used but it is ok. */
if (a ? b
      : (c ? d
           : e))
{
}
```

```diff
--- Bad ---
```

```c
a = b ?
    c : d;

while (a ? b
       : c)
{
}

if (a
    ? b
    : c)
{
}
```

## 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.

```diff
+++ Good +++
```

```c
function_a ();
function_b (a);
function_c (a, b, c);

function_c (a,
            b,
            c);

function_c (a,
            b,
            function_c (a,
                        b,
                        c);
```

```diff
--- Bad ---
```

```c
/* No space before the opening parenthesis. */
function_f();

function_f (
);

function_g(a);

function_g
  (a
  );

/* Two arguments on the same line. */
function_h (a, b,
            c);

function_h (a,
            b, c);

/* Misaligned arguments. */
function_h (a,
     b,
     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.

```diff
+++ Good +++
```

```c
for (int i = 0; i < 10; i++)
{
  int j = i + 1;
  while (j < 10)
  {
    ++j;
  }
}

/* Multiline form of for loops. */
for (int i = 0;
     i < 10;
     i++)
{
}
```

## Type casting

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

```diff
+++ Good +++
```

```c
int a = (int) double_variable;

int a = (int) (long) (float) double_variable;
```

```diff
--- Bad ---
```

```c
/* Wrong spaces. */
int a = ( int )double_variable;

/* No multiline form. */
int a = (int)
        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.

```diff
+++ Good +++
```

```c
  int *int_p;

  /* No need to add multiple _p-s for multiple indirections.
   * It is recommended to avoid these constructs using typedef
   * declarations. A reviewer might request such a change. */
  int ***int_p;

  /* This rule applies for type casting as well. */
  char = *(char *) type_p;
```

```diff
--- Bad ---
```

```c
  /* No _p after the name. */
  int *ptr;

  /* Wrong asterisk position. */
  int* ptr_p;

  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.

```diff
+++ Good +++
```

```c
/**
 * Short description of the following structure.
 */
typedef struct
{
  /* Field descriptions do not start with capital letters
   * and there is no full stop at the end. */
  field1_t field1; /**< description of field 1 */
  field2_t field2; /**< description of field 2 */

  field_n_t field_n; /**< description of field n */
} structure_name_t;

/**
 * Another integer type.
 */
typedef int jerry_int;
```

```diff
--- Bad ---
```

```c
typedef struct
{
  field_t field_without_description;
} structure_without_description_t;

typedef struct { int a; } single_line_struct;

typedef
union {
}
wrong_newlines_t;

/*
  * Bad comment format.
   */
typedef
char wrong_newlines_again_t;
```

### Type usage conventions

 - Passing the number of arguments for a function call is always `uint32_t`
 - String size/length/position related operation should use `lit_utf8_size_t`
 - Extended objects internal fields must be `uint32_t`

## Function declarations

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

```diff
+++ Good +++
```

```c
/**
 * Short overview about the purpose of this function.
 *
 * A more detailed explanation if needed.
 *
 * Note:
 *   Extra notes if needed.
 *
 * @return short description about the value
 *         returned by the function
 */
return_value_type_t
function_name (argument1, /**< description of argument1 */
               argument2, /**< description of argument2 */
               ...
               argument_n, /**< description of argument n */
{

  /* Function body. */

} /* function_name */
```

```diff
--- Bad ---
```

```c
static int
print (char *text) /**< description of text argument */
{
  /* Missing comment before the function. */
} /* print */

/**
 * Prints the text received by the function.
 *
 * @return number of characters printed by the function
 */
int print(char *text)
{
  /* No description of text argument. */
  /* Missing comment at the end of the function. */
}
```