| OLD | NEW |
| (Empty) | |
| 1 >>> DO use ; instead of {} for empty constructor bodies |
| 2 class Point { |
| 3 int x, y; |
| 4 Point(this.x, this.y) {} |
| 5 } |
| 6 <<< |
| 7 class Point { |
| 8 int x, y; |
| 9 Point(this.x, this.y); |
| 10 } |
| 11 >>> DO indent block bodies two spaces. |
| 12 hi() { |
| 13 if (condition) { print('hi'); } |
| 14 } |
| 15 <<< |
| 16 hi() { |
| 17 if (condition) { |
| 18 print('hi'); |
| 19 } |
| 20 } |
| 21 >>> DON'T indent lines that are continued with a function expression. |
| 22 var callback = new Future.delayed(const Duration(seconds: 1), () { |
| 23 print('I am a callback'); |
| 24 }); |
| 25 <<< |
| 26 var callback = new Future.delayed(const Duration(seconds: 1), () { |
| 27 print('I am a callback'); |
| 28 }); |
| 29 >>> DO place the opening curly brace ({) on the same line as what it follows. |
| 30 class Foo { |
| 31 method() |
| 32 { |
| 33 if (true) |
| 34 { |
| 35 print('true'); |
| 36 } else { |
| 37 print('false'); |
| 38 } |
| 39 } |
| 40 } |
| 41 <<< |
| 42 class Foo { |
| 43 method() { |
| 44 if (true) { |
| 45 print('true'); |
| 46 } else { |
| 47 print('false'); |
| 48 } |
| 49 } |
| 50 } |
| 51 >>> DO use curly braces for all flow control structures. |
| 52 flow() { |
| 53 if (true) print('sanity'); |
| 54 else |
| 55 print('opposite day!'); |
| 56 } |
| 57 <<< |
| 58 flow() { |
| 59 if (true) { |
| 60 print('sanity'); |
| 61 } else { |
| 62 print('opposite day!'); |
| 63 } |
| 64 } |
| 65 >>> ... short if statements with no else may omit the braces ... |
| 66 except() { |
| 67 if (arg == null) return defaultValue; |
| 68 } |
| 69 <<< |
| 70 except() { |
| 71 if (arg == null) return defaultValue; |
| 72 } |
| 73 >>> DO indent switch cases two spaces and case bodies four spaces |
| 74 switches() { |
| 75 switch (fruit) { |
| 76 case 'apple': |
| 77 print('delish'); |
| 78 break; |
| 79 |
| 80 case 'durian': |
| 81 print('stinky'); |
| 82 break; |
| 83 } |
| 84 } |
| 85 <<< |
| 86 switches() { |
| 87 switch (fruit) { |
| 88 case 'apple': |
| 89 print('delish'); |
| 90 break; |
| 91 |
| 92 case 'durian': |
| 93 print('stinky'); |
| 94 break; |
| 95 } |
| 96 } |
| 97 >>> DO use spaces around binary and ternary operators, etc... |
| 98 spaces() { |
| 99 a=1+2/(3* - b); |
| 100 c= ! condition==a>b; |
| 101 d= condition?b:object.method(a,b,c); |
| 102 if (obj is !SomeType) print('not SomeType'); |
| 103 } |
| 104 <<< |
| 105 spaces() { |
| 106 a = 1 + 2 / (3 * -b); |
| 107 c = !condition == a > b; |
| 108 d = condition ? b : object.method(a, b, c); |
| 109 if (obj is! SomeType) print('not SomeType'); |
| 110 } |
| 111 >>> DO place spaces around in, and after each ; in a loop. |
| 112 loop() { |
| 113 for (var i = 0;i<100;i++) { |
| 114 print(i); |
| 115 } |
| 116 for (final item in collection) { |
| 117 print(item); |
| 118 } |
| 119 } |
| 120 <<< |
| 121 loop() { |
| 122 for (var i = 0; i < 100; i++) { |
| 123 print(i); |
| 124 } |
| 125 for (final item in collection) { |
| 126 print(item); |
| 127 } |
| 128 } |
| 129 >>> DO use a space after flow-control keywords. |
| 130 flow() { |
| 131 while(foo) { |
| 132 print(foo); |
| 133 } |
| 134 |
| 135 try{ |
| 136 flow(); |
| 137 }catch(e) { |
| 138 print(e); |
| 139 } |
| 140 } |
| 141 <<< |
| 142 flow() { |
| 143 while (foo) { |
| 144 print(foo); |
| 145 } |
| 146 |
| 147 try { |
| 148 flow(); |
| 149 } catch (e) { |
| 150 print(e); |
| 151 } |
| 152 } |
| 153 >>> DON'T use a space after (, [, and {, or before ), ], and }. |
| 154 spaces() { |
| 155 var numbers = <int> [ 1, 2,( 3+4 ) ]; |
| 156 } |
| 157 <<< |
| 158 spaces() { |
| 159 var numbers = <int>[1, 2, (3 + 4)]; |
| 160 } |
| 161 >>> DO use a space before { in function and method bodies. |
| 162 getEmptyFn(a){ |
| 163 return (){}; |
| 164 } |
| 165 <<< |
| 166 getEmptyFn(a) { |
| 167 return () { |
| 168 }; |
| 169 } |
| 170 >>> DO format constructor initialization lists with each field on its own line. |
| 171 class MyClass { |
| 172 var firstField, secondField, thirdField; |
| 173 MyClass() : firstField = "some value", secondField = "another", |
| 174 thirdField = "last"; |
| 175 } |
| 176 <<< |
| 177 class MyClass { |
| 178 var firstField, secondField, thirdField; |
| 179 MyClass() |
| 180 : firstField = "some value", |
| 181 secondField = "another", |
| 182 thirdField = "last"; |
| 183 } |
| 184 >>> DO use a space after : in named parameters and named arguments. |
| 185 class ListBox { |
| 186 bool showScrollbars; |
| 187 |
| 188 ListBox({this.showScrollbars: false}); |
| 189 } |
| 190 |
| 191 main() { |
| 192 new ListBox(showScrollbars:true); |
| 193 new ListBox(showScrollbars : true); |
| 194 } |
| 195 <<< |
| 196 class ListBox { |
| 197 bool showScrollbars; |
| 198 |
| 199 ListBox({this.showScrollbars: false}); |
| 200 } |
| 201 |
| 202 main() { |
| 203 new ListBox(showScrollbars: true); |
| 204 new ListBox(showScrollbars: true); |
| 205 } |
| 206 >>> DO use a spaces around = in optional positional parameters. |
| 207 class HttpServer { |
| 208 static Future<HttpServer> listen([int port=80]) { |
| 209 print('hi!'); |
| 210 } |
| 211 } |
| 212 <<< |
| 213 class HttpServer { |
| 214 static Future<HttpServer> listen([int port = 80]) { |
| 215 print('hi!'); |
| 216 } |
| 217 } |
| 218 >>> DO use four spaces for method cascades |
| 219 var list = new List() |
| 220 ..addAll([1, 2, 3]) |
| 221 ..addAll([4, 5, 6]); |
| 222 <<< |
| 223 var list = new List() |
| 224 ..addAll([1, 2, 3]) |
| 225 ..addAll([4, 5, 6]); |
| OLD | NEW |