OLD | NEW |
---|---|
1 --- | 1 --- |
2 layout: article | 2 layout: article |
3 title: "Dart Style Guide" | 3 title: "Dart Style Guide" |
4 rel: | 4 rel: |
5 author: bob-nystrom | 5 author: bob-nystrom |
6 description: "Follow these guidelines for consistent, readable Dart code." | 6 description: "Follow these guidelines for consistent, readable Dart code." |
7 has-permalinks: true | 7 has-permalinks: true |
8 article: | 8 article: |
9 written_on: 2011-10-27 | 9 written_on: 2011-10-27 |
10 updated_on: 2015-02-17 | 10 updated_on: 2015-02-17 |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
599 } | 599 } |
600 {% endprettify %} | 600 {% endprettify %} |
601 </div> | 601 </div> |
602 | 602 |
603 | 603 |
604 ## Names | 604 ## Names |
605 | 605 |
606 #### DO name types using `UpperCamelCase`. | 606 #### DO name types using `UpperCamelCase`. |
607 {:.no_toc} | 607 {:.no_toc} |
608 | 608 |
609 Classes and typedefs should capitalize the first letter of each word (including | 609 Classes, enums, and typedefs should capitalize the first letter of each word |
610 the first word), and use no separators. | 610 (including the first word), and use no separators. |
611 | 611 |
612 <div class="good" markdown="1"> | 612 <div class="good" markdown="1"> |
613 {% prettify dart %} | 613 {% prettify dart %} |
614 class SliderMenu { | 614 class SliderMenu { |
615 // ... | 615 // ... |
616 } | 616 } |
617 | 617 |
618 class HttpRequest { | 618 class HttpRequest { |
619 // ... | 619 // ... |
620 } | 620 } |
621 | 621 |
622 typedef num Adder(num x, num y); | 622 typedef num Adder(num x, num y); |
623 {% endprettify %} | 623 {% endprettify %} |
624 </div> | 624 </div> |
625 | 625 |
626 | 626 |
627 #### PREFER using `lowerCamelCase` for constant names. | 627 #### PREFER using `lowerCamelCase` for constant names. |
628 {:.no_toc} | 628 {:.no_toc} |
629 <!-- https://github.com/dart-lang/www.dartlang.org/issues/831 --> | 629 <!-- https://github.com/dart-lang/www.dartlang.org/issues/831 --> |
630 | 630 |
631 In new code, use `lowerCamelCase` for constant variables. | 631 In new code, use `lowerCamelCase` for constant variables, including enum |
632 values. | |
632 | 633 |
633 In existing code that uses `ALL_CAPS_WITH_UNDERSCORES` for constants, you | 634 In existing code that uses `ALL_CAPS_WITH_UNDERSCORES` for constants, you |
634 may continue to use all caps to stay consistent. | 635 may continue to use all caps to stay consistent. |
635 | 636 |
636 <div class="good"> | 637 <div class="good"> |
637 {% prettify dart %} | 638 {% prettify dart %} |
638 const pi = 3.14; | 639 const pi = 3.14; |
639 const defaultTimeout = 1000; | 640 const defaultTimeout = 1000; |
640 final urlScheme = new RegExp('^([a-z]+):'); | 641 final urlScheme = new RegExp('^([a-z]+):'); |
641 | 642 |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1002 Readability studies show that long lines of text are harder to read because your | 1003 Readability studies show that long lines of text are harder to read because your |
1003 eye has to travel farther when moving to the beginning of the next line. This is | 1004 eye has to travel farther when moving to the beginning of the next line. This is |
1004 why newspapers and magazines use multiple columns of text. | 1005 why newspapers and magazines use multiple columns of text. |
1005 | 1006 |
1006 If you really find yourself wanting lines longer than 80 characters, our | 1007 If you really find yourself wanting lines longer than 80 characters, our |
1007 experience is that your code is likely too verbose and could be a little more | 1008 experience is that your code is likely too verbose and could be a little more |
1008 compact. Do you really need to call that class | 1009 compact. Do you really need to call that class |
1009 `AbstractWidgetFactoryManagerBuilder`? | 1010 `AbstractWidgetFactoryManagerBuilder`? |
1010 | 1011 |
1011 | 1012 |
1012 #### DO place the operator on the preceding line in a multi-line expression. | 1013 #### DO place binary operators on the preceding line in a multi-line expression. |
1013 {:.no_toc} | 1014 {:.no_toc} |
1014 | 1015 |
1015 There are valid arguments for both styles but most of our code seems to go this | 1016 There are valid arguments for both styles but most of our code seems to go this |
1016 way, and consistency matters most. | 1017 way, and consistency matters most. |
1017 | 1018 |
1018 <div class="good"> | 1019 <div class="good"> |
1019 {% prettify dart %} | 1020 {% prettify dart %} |
1020 if (isDeepFried || | 1021 if (isDeepFried || |
1021 (hasPieCrust && !vegan) || | 1022 (hasPieCrust && !vegan) || |
1022 containsBacon) { | 1023 containsBacon) { |
(...skipping 12 matching lines...) Expand all Loading... | |
1035 </div> | 1036 </div> |
1036 | 1037 |
1037 <div class="bad"> | 1038 <div class="bad"> |
1038 {% prettify dart %} | 1039 {% prettify dart %} |
1039 bobLikes() | 1040 bobLikes() |
1040 => isDeepFried || (hasPieCrust && !vegan) || containsBacon; | 1041 => isDeepFried || (hasPieCrust && !vegan) || containsBacon; |
1041 {% endprettify %} | 1042 {% endprettify %} |
1042 </div> | 1043 </div> |
1043 | 1044 |
1044 | 1045 |
1046 #### DO place ternary operators on the next line in a multi-line expression. | |
1047 {:.no_toc} | |
1048 | |
1049 Also, if you break the line before one of the operators, prefer breaking | |
1050 around both. | |
1051 | |
1052 <div class="good"> | |
1053 {% prettify dart %} | |
1054 return someCondition | |
1055 ? whenTrue | |
1056 : whenFalse; | |
1057 {% endprettify %} | |
1058 </div> | |
1059 | |
1060 <div class="bad"> | |
1061 {% prettify dart %} | |
1062 return someCondition ? | |
1063 whenTrue : whenFalse; | |
Bob Nystrom
2015/02/26 00:19:52
Let's make this one:
return someCondition
? whe
Kathy Walrath
2015/02/26 16:15:14
Done.
| |
1064 return someCondition ? | |
1065 whenTrue : | |
1066 whenFalse; | |
1067 {% endprettify %} | |
Bob Nystrom
2015/02/26 00:19:52
Also, maybe swap the order of the bad examples so
Kathy Walrath
2015/02/26 16:15:14
Done.
| |
1068 </div> | |
1069 | |
1070 | |
1045 #### DO place the `.` on the next line in a multi-line expression. | 1071 #### DO place the `.` on the next line in a multi-line expression. |
1046 {:.no_toc} | 1072 {:.no_toc} |
1047 | 1073 |
1048 This supercedes the previous rule. Unlike other operators, if you split an | 1074 This supercedes the previous rule. Unlike other operators, if you split an |
1049 expression on a `.`, then put that at the beginning of the second line. | 1075 expression on a `.`, then put that at the beginning of the second line. |
1050 | 1076 |
1051 <div class="good"> | 1077 <div class="good"> |
1052 {% prettify dart %} | 1078 {% prettify dart %} |
1053 someVeryLongVariable.withAVeryLongProperty | 1079 someVeryLongVariable.withAVeryLongProperty |
1054 .aMethodOnThatObject(); | 1080 .aMethodOnThatObject(); |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1520 'Wear your wildest ${decade}s outfit.' | 1546 'Wear your wildest ${decade}s outfit.' |
1521 {% endprettify %} | 1547 {% endprettify %} |
1522 </div> | 1548 </div> |
1523 | 1549 |
1524 <div class="bad"> | 1550 <div class="bad"> |
1525 {% prettify dart %} | 1551 {% prettify dart %} |
1526 'Hi, ${name}!' | 1552 'Hi, ${name}!' |
1527 "Wear your wildest ${decade}'s outfit." | 1553 "Wear your wildest ${decade}'s outfit." |
1528 {% endprettify %} | 1554 {% endprettify %} |
1529 </div> | 1555 </div> |
1556 | |
1557 | |
1558 ## Ordering | |
1559 | |
1560 #### DO specify dart: imports, then package: imports, and then relative imports | |
1561 {:.no_toc} | |
1562 | |
1563 Separate import sections from each other with a single blank line. | |
1564 | |
1565 Within each section, prefer sorting alphabetically. If you use a | |
1566 `package:` import to import from your own package, consider putting | |
1567 that import in the relative import section. | |
1568 | |
1569 <div class="good"> | |
1570 {% prettify dart %} | |
1571 import 'dart:async'; | |
1572 import 'dart:convert' show JSON; | |
1573 import 'dart:html'; | |
1574 | |
1575 import 'package:bar/bar.dart' | |
1576 import 'package:bar/foo.dart' | |
1577 import 'package:foo/bar.dart' | |
1578 | |
1579 import 'a.dart'; | |
1580 {% endprettify %} | |
1581 </div> | |
1582 | |
1583 <div class="bad"> | |
1584 {% prettify dart %} | |
1585 import 'dart:html'; | |
1586 import 'dart:async'; | |
1587 import 'dart:convert' show JSON; | |
1588 | |
1589 import 'a.dart'; | |
1590 import 'package:bar/bar.dart' | |
1591 import 'package:foo/bar.dart' | |
1592 import 'package:bar/foo.dart' | |
1593 {% endprettify %} | |
1594 </div> | |
1595 | |
1596 #### PREFER specifying exports in a separate section after all imports | |
1597 {:.no_toc} | |
1598 | |
1599 Put a single blank line above the exports section. | |
1600 | |
1601 <div class="good"> | |
1602 {% prettify dart %} | |
1603 import 'src/error.dart'; | |
1604 import 'src/string_source.dart'; | |
1605 | |
1606 export 'src/error.dart'; | |
1607 {% endprettify %} | |
1608 </div> | |
1609 | |
1610 <div class="bad"> | |
1611 {% prettify dart %} | |
1612 import 'src/error.dart'; | |
1613 export 'src/error.dart'; | |
1614 | |
1615 import 'src/string_source.dart'; | |
1616 {% endprettify %} | |
1617 </div> | |
1618 | |
1619 | |
1620 #### PREFER specifying a name for every library | |
1621 {:.no_toc} | |
1622 | |
1623 Although Dart allows you to omit `library` from an app file | |
1624 (a file that has a top-level `main()` function) | |
1625 we recommend always specifying a library name. | |
Bob Nystrom
2015/02/26 00:19:52
We do?
Kathy Walrath
2015/02/26 16:15:14
I was surprised, too, but that was in seanegan's s
Bob Nystrom
2015/02/26 20:52:03
My reading comprehension was bad. :(
I like the r
| |
1626 | |
1627 <div class="good"> | |
1628 {% prettify dart %} | |
1629 library my_app; | |
1630 | |
1631 void main() { | |
1632 ... | |
1633 } | |
1634 {% endprettify %} | |
1635 </div> | |
1636 | |
1637 <div class="bad"> | |
1638 {% prettify dart %} | |
1639 void main() { | |
1640 ... | |
1641 } | |
1642 {% endprettify %} | |
1643 </div> | |
OLD | NEW |