-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
documentation_style_guide.md
1246 lines (928 loc) · 26.4 KB
/
documentation_style_guide.md
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- A reference to help give our documentation a consistent voice. -->
# Documentation Style Guide
Hello! Welcome to the guidelines for writing p5.js documentation. This document is a remix of the following resources:
- Ruby on Rails [API Documentation Guidlines](https://guides.rubyonrails.org/api_documentation_guidelines.html) (CC BY-SA 4.0)
- WordPress documentation guidelines for [accessibility](https://make.wordpress.org/docs/style-guide/general-guidelines/accessibility/) and [inclusivity](https://make.wordpress.org/docs/style-guide/general-guidelines/inclusivity/) (CC0)
- Airbnb [JavaScript Style Guide](https://airbnb.io/javascript/) (MIT)
Our community is large and diverse. Many people learn to code using p5.js, and a large subset of those people are students in K–12 classes. After reading this guide, you will know:
- How to write effective, inclusive, and accessible prose for documentation purposes.
- How to write simple code samples for documentation purposes.
## Table of Contents
### Writing
- [YUIDoc](#yuidoc)
- [English](#english)
- [Oxford Comma](#oxford-comma)
- [Wording](#wording)
- [Unbiased Documentation](#unbiased-documentation)
- [Accessibility and Disability](#accessibility-and-disability)
### Code
- [Code Samples](#code-samples)
- [Comments](#comments)
- [Whitespace](#whitespace)
- [Semicolons](#semicolons)
- [Naming Conventions](#naming-conventions)
- [Variables](#variables)
- [Strings](#strings)
- [Boolean Operators](#boolean-operators)
- [Conditionals](#conditionals)
- [Iteration](#iteration)
- [Objects](#objects)
- [Arrays](#arrays)
- [Functions](#functions)
- [Arrow Functions](#arrow-functions)
- [Chaining](#chaining)
- [Classes](#classes)
- [Assets](#assets)
## YUIDoc
We use YUIDoc to generate the p5.js API documentation. To generate the docs, navigate to the p5.js root directory, run `npm install`, and execute:
```
$ npm run grunt yui:dev
```
The output will appear in docs/reference. Refer to the [inline documentation guide](./contributing_to_the_p5js_reference.md) for more information.
**[⬆ back to top](#table-of-contents)**
## English
Please use American English (color, center, modularize, etc). See [a list of American and British English spelling differences here](https://en.wikipedia.org/wiki/American_and_British_English_spelling_differences).
**[⬆ back to top](#table-of-contents)**
## Oxford Comma
Please use the [Oxford comma](https://en.wikipedia.org/wiki/Serial_comma) ("red, white, and blue", instead of "red, white and blue").
**[⬆ back to top](#table-of-contents)**
## Wording
Write simple, declarative sentences. Brevity is a plus: get to the point.
Write in the present tense: "Returns an object that...", rather than "Returned an object that..." or "Will return an object that...".
Start comments in upper case. Follow regular punctuation rules:
```javascript
// Draws a fractal from a Julia set.
function drawFractal(c, radius, maxIter) {
// ...
}
```
Communicate the current way of doing things, both explicitly and implicitly. Use the idioms recommended in this guide. Reorder sections to emphasize favored approaches if needed. The documentation should be a model for best practices and approachable for beginners.
Documentation has to be brief but comprehensive. Explore and document edge cases. What happens for each combination of arguments? What bugs are most likely to appear in a beginner's code?
Spell names correctly: p5.js, CSS, HTML, JavaScript, WebGL. When in doubt, refer to an authoritative source like their official documentation.
**[⬆ back to top](#table-of-contents)**
## Unbiased Documentation
Write documentation without bias towards any kind of person. While documenting particularly demanding/sensitive topics, take the time to educate yourself. Ensure that your writing doesn’t hurt or offend someone unintentionally.
While writing unbiased documentation:
- Be inclusive of every gender identity and expression, sexual orientation, race, ethnicity, language, neuro-type, size, disability, class, religion, culture, subculture, political opinion, age, skill level, occupation, and background. Make examples as diverse as our community.
- Avoid politicized content. If political content is necessary, remain neutral.
- Follow accessibility guidelines.
- Avoid content that would insult or cause harm to people.
- Don’t make any generalizations about people, countries, and cultures. That includes positive or neutral generalizations.
- Don’t write prejudiced and discriminatory content against minority communities.
- Avoid terms related to historical events.
Prefer wordings that avoid "you"s and "your"s. For example, instead of:
```
If you need to declare a variable, it is recommended that you use `let`.
```
use this style:
```
Always use `let` to declare variables.
```
**Pronouns**
| Recommended | Not Recommended |
| -- | -- |
| they | he or she |
| them | him or her |
| their | his or her |
| theirs | his or hers |
| themselves | himself or herself |
**[⬆ back to top](#table-of-contents)**
## Accessibility and Disability
- Emphasize the reader rather than underlining their inconveniences.
- Don’t refer to a person with a disability as a disabled person. Use [approved terminology](https://make.wordpress.org/docs/style-guide/general-guidelines/inclusivity/#accessibility-terminology) for people with specific disabilities.
- Maintain a uniform structure throughout the p5.js documentation. Emphasize important points both stylistically and visually.
- Use a screen reader to test documentation. To test a screen reader, see [List of screen readers](https://en.wikipedia.org/wiki/List_of_screen_readers).
- Consider multi-platform accessibility for all types of devices and operating systems.
- Create examples with all types of input devices such as voice and gesture based devices, controllers, mice, and keyboards.
- Don’t use ableist language. Be inclusive and unbiased while writing about accessibility and disability.
- Take a pragmatic approach to HTML semantics. Don’t add semantics purely for the sake of semantics. If there is an HTML structure that clearly matches the content, use that element. For example, a group of links should most likely use a list element.
- Use simple tables and tabular formats. Avoid span tags (such as rowspan and colspan). Tables prove to be difficult for screen readers.
**Accessibility terminology**
The following terminiology is adapted from the WordPress documentation guidelines for [Writing inclusive documentation](https://make.wordpress.org/docs/style-guide/general-guidelines/inclusivity/#accessibility-terminology). For more background on people-first language, see the CDC's guide on [Communicating With and About People with Disabilities](https://www.cdc.gov/ncbddd/disabilityandhealth/materials/factsheets/fs-communicating-with-people.html).
| Recommended | Not Recommended |
| -- | -- |
| person with disability | the disabled, handicapped, differently abled, challenged, abnormal |
| person without disability | normal person, healthy person, able-bodied |
| has [disability] | victim of, suffering from, affected by, stricken with |
| unable to speak, uses synthetic speech | dumb, mute |
| deaf, low-hearing | hearing-impaired |
| blind, low-vision | vision-impaired, visually-challenged |
| cognitive or developmental disabilities | mentally-challenged, slow-learner |
| person with limited mobility, person with a physical disability | crippled, handicapped |
## Code Samples
Choose meaningful code samples that cover the basics as well as gotchas. Only use advanced syntax if it is necessary to explain how a feature works. Don't draw five circles to explain something when one circle will convey the idea. The code samples themselves should follow the guidelines below.
**[⬆ back to top](#table-of-contents)**
## Comments
- Use `//` for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment unless it’s on the first line of a block.
```javascript
// Bad.
let magicWord = 'Please'; // Remember this.
// Good.
// Remember this.
let magicWord = 'Please';
// Bad.
if (keyIsPressed === true) {
thing1();
// This is an important note.
thing2();
}
// Good.
if (keyIsPressed === true) {
thing1();
// This is an important note.
thing2();
}
```
- Start all comments with a space to make it easier to read.
```javascript
// Bad.
//Remember this.
let magicWord = 'Please';
// Good.
// Remember this.
let magicWord = 'Please';
```
- Use `//` for multiline comments.
```javascript
// Bad.
/**
* I will use // for multiline comments.
* I will use // for multiline comments.
* I will use // for multiline comments.
* I will use // for multiline comments.
* I will use // for multiline comments.
*/
//Bad.
/*
I will use // for multiline comments.
I will use // for multiline comments.
I will use // for multiline comments.
I will use // for multiline comments.
I will use // for multiline comments.
*/
// Good.
// I will use // for multiline comments.
// I will use // for multiline comments.
// I will use // for multiline comments.
// I will use // for multiline comments.
// I will use // for multiline comments.
```
**[⬆ back to top](#table-of-contents)**
## Whitespace
- Indent blocks 2 spaces.
```javascript
// Bad.
function setup() {
∙∙∙∙createCanvas(400, 400);
}
// Bad.
function setup() {
∙createCanvas(400, 400);
}
// Good.
function setup() {
∙∙createCanvas(400, 400);
}
```
- Place 1 space before the leading brace.
```javascript
// Bad.
function setup(){
createCanvas(400, 400);
}
// Good.
function setup() {
createCanvas(400, 400);
}
```
- Place 1 space before the opening parenthesis in control statements such as `if` and `for`. Place no space between the argument list and the function name.
```javascript
// Bad.
if(keyIsPressed === true) {
doStuff ();
}
// Good.
if (keyIsPressed === true) {
doStuff();
}
// Bad.
function setup () {
createCanvas (400, 400);
}
// Good.
function setup() {
createCanvas(400, 400);
}
```
- Place spaces between operators.
```javascript
// Bad.
let y=x+5;
// Good.
let y = x + 5;
```
## Semicolons
- Yep.
> Why? JavaScript's [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion) can lead to subtle bugs.
```javascript
// Bad.
let x = 0
// Good.
let x = 0;
```
## Naming Conventions
- Avoid single letter names. Be descriptive.
```javascript
// Bad.
function f(x, y) {
// ...
}
// Good.
function vectorField(x, y) {
// ...
}
```
- Name objects, functions, and instances using camelCase.
```javascript
// Bad.
let OBJEcttsssss = {};
// Bad.
let this_is_my_object = {};
// Good.
let thisIsMyObject = {};
```
- Name classes using PascalCase.
```javascript
// Bad.
class player {
constructor(name) {
this.name = name;
}
}
// Good.
class Player {
constructor(name) {
this.name = name;
}
}
```
- Don't use trailing or leading underscores.
> Why? JavaScript doesn't have private properties or methods.
```javascript
// Bad.
class Spy {
constructor(secret) {
this._secret = secret;
}
}
// Good.
class Spy {
constructor(secret) {
this.secret = secret;
}
}
```
**[⬆ back to top](#table-of-contents)**
## Variables
- Avoid using `var` to declare variables.
> Why? Variables declared with `var` have confusing scoping rules. These lead to subtle bugs.
```javascript
// Bad, because it looks reasonable.
circle(x, y, 50);
var x = 200;
var y = 200;
// Good, because it throws a ReferenceError.
circle(x, y, 50);
let x = 200;
let y = 200;
```
- Always use `let` to declare variables. Avoid using `const`.
> Why? Variables declared with `let` are easier to reason about than those declared with `var`. Variables are often reassigned in sketches, so it's helpful to default to `let`.
```javascript
// Bad.
flower = '🌸';
var flower = '🌸';
const flower = '🌸';
// Good.
let flower = '🌸';
```
- Use one `let` declaration per variable or assignment.
> Why? It’s easier to read and to add new variable declarations.
```javascript
// Bad.
let positions = getPositions(),
startSearch = true,
dragonball = 'z';
// Good.
let positions = getPositions();
let startSearch = true;
let dragonball = 'z';
```
- Assign variables where needed and place them in a reasonable place.
> Why? `let` is block scoped and not function scoped.
```javascript
// Bad - unnecessary search.
function getCharacter(name = 'default') {
let character = characters.find((c) => c.name === name);
if (name === 'default') {
return false;
}
if (character) {
return character;
}
return false;
}
// Good.
function getCharacter(name = 'default') {
if (name === 'default') {
return false;
}
let character = characters.find((c) => c.name === name);
if (character) {
return character;
}
return false;
}
```
- Avoid using unary increments and decrements (`++`, `--`).
> Why? Unary increment and decrement statements are subject to [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion). This can cause silent errors with incrementing or decrementing values. It's also more expressive to update variables with statements like num `+= 1` instead of `num++`.
```javascript
// Bad.
let num = 1;
num++;
--num;
// Good.
let num = 1;
num += 1;
num -= 1;
```
**[⬆ back to top](#table-of-contents)**
## Strings
- Use single quotes `''` for strings.
```javascript
// Bad.
let name = "Hilma af Klint";
// Bad - template literals should contain interpolation or newlines.
let name = `Hilma af Klint`;
// Good.
let name = 'Hilma af Klint';
```
- Don't concatenate strings that cause the line to go over 80 characters.
> Why? Broken strings are hard to read and make code less searchable.
```javascript
// Bad.
let essay = 'You see us as you want to see us: \
in the simplest terms, in the most convenient definitions.';
// Bad.
let essay = 'You see us as you want to see us: ' +
'in the simplest terms, in the most convenient definitions.';
// Good.
let essay = 'You see us as you want to see us: in the simplest terms, in the most convenient definitions.';
```
- Use template strings instead of concatenation when needed.
> Why? Template strings have a concise syntax. They also provide proper newlines and string interpolation features.
```javascript
let name = 'Dave';
// Bad.
text(name + ', this conversation can serve no purpose anymore. Goodbye.' + name, 0, 0);
// Good.
text(`${name}, this conversation can serve no purpose anymore. Goodbye.`, 0, 0);
```
- Do not unnecessarily escape characters in strings.
> Why? Backslashes harm readability.
```javascript
// Bad.
let bad = '\'this\' \i\s \"quoted\"';
// Good.
let good = 'Air quotes make you look "cool".';
```
**[⬆ back to top](#table-of-contents)**
## Boolean Operators
- Use `===` and `!==` over `==` and `!=`.
- Don't use shortcuts for booleans.
> Why? It's easier to understand for beginners.
```javascript
// Bad.
if (mouseIsPressed) {
// ...
}
// Good.
if (mouseIsPressed === true) {
// ...
}
// Bad.
if (name) {
// ...
}
// Good.
if (name !== '') {
// ...
}
// Bad.
if (collection.length) {
// ...
}
// Good.
if (collection.length > 0) {
// ...
}
```
- Don't use `switch` statements unless it's necessary.
- Use parentheses when mixing operators. The only exceptions are the arithmetic operators `+`, `-`, and `**`.
> Why? It's easier to read and avoids subtle bugs.
```javascript
// Bad.
let huh = a && b < 0 || c > 0 || d + 1 === 0;
// Good.
let huh = (a && b < 0) || c > 0 || (d + 1 === 0);
// Bad.
if (a || b && c) {
return d;
}
// Good.
if (a || (b && c)) {
return d;
}
// Bad.
let what = a + b / c * d;
// Good.
let what = a + (b / c) * d;
```
## Conditionals
- Use braces with all multiline blocks.
```javascript
// Bad.
if (mouseIsPressed === true)
circle(mouseX, mouseY, 50);
// Better.
if (mouseIsPressed === true) circle(mouseX, mouseY, 50);
// Best.
if (mouseIsPressed === true) {
circle(mouseX, mouseY, 50);
}
```
- Put `else` on the same line as the preceding `if` block’s closing brace.
```javascript
// Bad.
if (mouseIsPressed === true) {
thing1();
thing2();
}
else {
thing3();
}
// Good.
if (mouseIsPressed === true) {
thing1();
thing2();
} else {
thing3();
}
```
- Don't use an `else` block after an `if` block that always executes a `return` statement.
```javascript
// Bad.
function mouseIsOnLeft() {
if (mouseX < width * 0.5) {
return true;
} else {
return false;
}
}
// Good.
function mouseIsOnLeft() {
if (mouseX < width * 0.5) {
return true;
}
return false;
}
```
- If a condition gets too long, place each (grouped) condition on a new line. The logical operator should begin the line.
> Why? It's easier to read.
```javascript
// Bad.
if ((number === 123 || letters === 'abc') && mouseIsPressed === true && keyIsPressed === true) {
doStuff();
}
// Good.
if (
(number === 123 || letters === 'abc')
&& mouseIsPressed === true
&& keyIsPressed === true
) {
doStuff();
}
```
- Don't use selection operators in place of conditionals.
```javascript
// Bad.
refrigeratorIsRunning && goCatchIt();
// Good.
if (refrigeratorIsRunning === true) {
goCatchIt();
}
```
**[⬆ back to top](#table-of-contents)**
## Iteration
- Don’t use a `while` or `do-while` loops unless it's necessary. Use `for` loops to iterate a fixed number of times.
```javascript
let numPetals = 7;
// Bad.
let i = 0;
while (i < numPetals) {
ellipse(0, 0, 20, 80);
rotate(PI / numPetals);
i += 1;
}
// Good.
for (let i = 0; i < numPetals; i += 1) {
ellipse(0, 0, 20, 80);
rotate(PI / numPetals);
}
```
- Don’t use `for` loops to iterate over arrays.
> Why? Pure functions are easier to reason about than side effects.
> Use `forEach()` / `map()` / `every()` / `filter()` / `find()` / `findIndex()` / `reduce()` / `some()` / `...` to iterate over arrays. Use `Object.keys()` / `Object.values()` / `Object.entries()` to produce arrays for iterating over objects.
```javascript
let diameters = [50, 40, 30, 20, 10];
// Bad.
for (let i = 0; i < diameters.length; i += 1) {
circle(0, 0, diameters[i]);
}
// Bad.
for (let d of diameters) {
circle(0, 0, d);
}
// Good.
diameters.forEach((d) => circle(0, 0, d));
```
**[⬆ back to top](#table-of-contents)**
## Objects
- Use the literal syntax for object creation.
```javascript
// Bad.
let ball = new Object();
// Good.
let ball = {};
```
- Only quote properties that are invalid identifiers.
> Why? It's easier to read and improves syntax highlighting. JavaScript engines also have an easier time optimizing for performance.
```javascript
// Bad.
let secretObject = {
'x': 100,
'y': 200,
'top-secret': 'classified',
};
// Good.
let secretObject = {
x: 3,
y: 4,
'top-secret': 'classified',
};
```
- Use dot notation to access properties.
```javascript
let turtle = {
name: 'Leonardo',
color: 'dodgerblue',
weapon: '🗡️',
food: '🍕',
};
// Bad.
let turtleName = turtle['name'];
// Good.
let turtleName = turtle.name;
```
- Use bracket notation `[]` to access properties with a variable.
```javascript
let turtle = {
name: 'Leonardo',
color: 'dodgerblue',
weapon: '🗡️',
food: '🍕',
};
function getProp(prop) {
return turtle[prop];
}
let turtleName = getProp('name');
```
- Don't use leading commas.
```javascript
// Bad.
let mathematician = {
firstName: 'Ada'
, lastName: 'Lovelace'
};
// Good.
let mathematician = {
firstName: 'Ada',
lastName: 'Lovelace',
};
```
- Add a trailing comma.
```javascript
// Bad.
let artist = {
firstName: 'Lauren',
lastName: 'McCarthy'
};
// Good.
let artist = {
firstName: 'Lauren',
lastName: 'McCarthy',
};
```
**[⬆ back to top](#table-of-contents)**
## Arrays
- Use the literal syntax for array creation.
```javascript
// Bad.
let images = new Array();
// Good.
let images = [];
```
- Use [Array#push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push) instead of direct assignment to add items to an array.
```javascript
let lyrics = [];
// Bad.
lyrics[lyrics.length] = 'Little rough around the edges, but I keep it smooth';
// Good.
lyrics.push('Little rough around the edges, but I keep it smooth');
```
- Use the `slice()` method to copy arrays.
```javascript
// Bad.
let numbersCopy = [];
for (let i = 0; i < numbers.length; i += 1) {
numbersCopy[i] = numbers[i];
}
// Good.
let numbersCopy = numbers.slice();
```
- Write arrays on multiple lines when it improves readibility. Use line breaks after the opening bracket and before the closing bracket. Add a trailing comma.
```javascript
// Bad.
let matrix = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]];
// Good.
let matrix = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
];
// Also good.
let matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
```
**[⬆ back to top](#table-of-contents)**
## Functions
- Use function declarations instead of named function expressions.
> Why? Function declarations have some gotchas, but they are easier to understand for beginners.
```javascript
// Bad.
let foo = function () {
// ...
};
// Bad.
let foo = () => {
// ...
};
// Good.
function foo() {
// ...
}
```
- Use default parameter syntax. Don't mutate function arguments.
```javascript
// Bad.
function createBall(diameter) {
diameter = diameter || 50;
// ...
}
// Good.
function createBall(diameter = 50) {
// ...
}
```
- Always put default parameters last.
```javascript
// Bad.
function drawSpiral(angle = 90, length) {
// ...
}
// Good.
function drawSpiral(length, angle = 90) {
// ...
}
```
**[⬆ back to top](#table-of-contents)**
## Arrow Functions
- Use arrow function notation for anonymous functions. Callbacks are a common case for this syntax.
> Why? The syntax is more concise. It also creates a version of the function that executes in the context of `this`, which is often helpful.
> Why not? If the anonymous function is complex, rewrite it as a declared function.
```javascript
// Bad.
function setup() {
loadImage('assets/moonwalk.jpg', function (img) {
image(img, 0, 0);
});
}
// Good.
function setup() {
loadImage('assets/moonwalk.jpg', (img) => {
image(img, 0, 0);
});
}
// Bad.
function preload() {
loadImage('assets/moonwalk.jpg', (img) => {