@@ -12,12 +12,12 @@ ES6 or TypeScript.
12
12
### General
13
13
14
14
#### Write useful comments
15
- Comments that explain what some block of code does are nice; they can tell you something in less
15
+ Comments that explain what some block of code does are nice; they can tell you something in less
16
16
time than it would take to follow through the code itself.
17
17
18
- Comments that explain why some block of code exists at all, or does something the way it does,
19
- are _ invaluable_ . The "why" is difficult, or sometimes impossible, to track down without seeking out
20
- the original author. When collaborators are in the same room, this hurts productivity.
18
+ Comments that explain why some block of code exists at all, or does something the way it does,
19
+ are _ invaluable_ . The "why" is difficult, or sometimes impossible, to track down without seeking out
20
+ the original author. When collaborators are in the same room, this hurts productivity.
21
21
When collaborators are in different timezones, this can be devastating to productivity.
22
22
23
23
For example, this is a not-very-useful comment:
@@ -38,6 +38,11 @@ if (!$attrs['tabindex']) {
38
38
}
39
39
```
40
40
41
+ In TypeScript code, use JsDoc-style comments for descriptions (on classes, members, etc.) and
42
+ use ` // ` style comments for everything else (explanations, background info, etc.).
43
+
44
+ In SCSS code, always use ` // ` style comments.
45
+
41
46
#### Prefer more focused, granular components vs. complex, configurable components.
42
47
43
48
For example, rather than doing this:
@@ -55,22 +60,22 @@ do this:
55
60
```
56
61
57
62
#### Prefer small, focused modules
58
- Keeping modules to a single responsibility makes the code easier to test, consume, and maintain.
59
- ES6 modules offer a straightforward way to organize code into logical, granular units.
63
+ Keeping modules to a single responsibility makes the code easier to test, consume, and maintain.
64
+ ES6 modules offer a straightforward way to organize code into logical, granular units.
60
65
Ideally, individual files are 200 - 300 lines of code.
61
66
62
- As a rule of thumb, once a file draws near 400 lines (barring abnormally long constants / comments),
63
- start considering how to refactor into smaller pieces.
67
+ As a rule of thumb, once a file draws near 400 lines (barring abnormally long constants / comments),
68
+ start considering how to refactor into smaller pieces.
64
69
65
70
#### Less is more
66
- Once a feature is released, it never goes away. We should avoid adding features that don't offer
67
- high user value for price we pay both in maintenance, complexity, and payload size. When in doubt,
68
- leave it out.
71
+ Once a feature is released, it never goes away. We should avoid adding features that don't offer
72
+ high user value for price we pay both in maintenance, complexity, and payload size. When in doubt,
73
+ leave it out.
69
74
70
- This applies especially so to providing two different APIs to accomplish the same thing. Always
75
+ This applies especially to providing two different APIs to accomplish the same thing. Always
71
76
prefer sticking to a _ single_ API for accomplishing something.
72
77
73
- ### 100 column limit
78
+ ### 100 column limit
74
79
All code and docs in the repo should be 100 columns or fewer. This applies to TypeScript, SCSS,
75
80
HTML, bash scripts, and markdown files.
76
81
@@ -105,7 +110,7 @@ Avoid `any` where possible. If you find yourself using `any`, consider whether a
105
110
appropriate in your case.
106
111
107
112
For methods and properties that are part of a component's public API, all types must be explicitly
108
- specified because our documentation tooling cannot currently infer types in places where TypeScript
113
+ specified because our documentation tooling cannot currently infer types in places where TypeScript
109
114
can.
110
115
111
116
#### Fluent APIs
@@ -123,9 +128,9 @@ class ConfigBuilder {
123
128
* Omit the ` public ` keyword as it is the default behavior.
124
129
* Use ` private ` when appropriate and possible, prefixing the name with an underscore.
125
130
* Use ` protected ` when appropriate and possible with no prefix.
126
- * Prefix * library-internal* properties and methods with an underscore without using the ` private `
131
+ * Prefix * library-internal* properties and methods with an underscore without using the ` private `
127
132
keyword. This is necessary for anything that must be public (to be used by Angular), but should not
128
- be part of the user-facing API. This typically applies to symbols used in template expressions,
133
+ be part of the user-facing API. This typically applies to symbols used in template expressions,
129
134
` @ViewChildren ` / ` @ContentChildren ` properties, host bindings, and ` @Input ` / ` @Output ` properties
130
135
(when using an alias).
131
136
@@ -138,8 +143,8 @@ All public APIs must have user-facing comments. These are extracted and shown in
138
143
on [ material.angular.io] ( https://material.angular.io ) .
139
144
140
145
Private and internal APIs should have JsDoc when they are not obvious. Ultimately it is the purview
141
- of the code reviwer as to what is "obvious", but the rule of thumb is that * most* classes,
142
- properties, and methods should have a JsDoc description.
146
+ of the code reviwer as to what is "obvious", but the rule of thumb is that * most* classes,
147
+ properties, and methods should have a JsDoc description.
143
148
144
149
Properties should have a concise description of what the property means:
145
150
``` ts
@@ -169,7 +174,7 @@ Boolean properties and return values should use "Whether..." as opposed to "True
169
174
170
175
##### General
171
176
* Prefer writing out words instead of using abbreviations.
172
- * Prefer * exact* names over short names (within reason). E.g., ` labelPosition ` is better than
177
+ * Prefer * exact* names over short names (within reason). E.g., ` labelPosition ` is better than
173
178
` align ` because the former much more exactly communicates what the property means.
174
179
* Except for ` @Input ` properties, use ` is ` and ` has ` prefixes for boolean properties / methods.
175
180
@@ -187,31 +192,52 @@ class UniqueSelectionDispatcher { }
187
192
Avoid suffixing a class with "Service", as it communicates nothing about what the class does. Try to
188
193
think of the class name as a person's job title.
189
194
195
+ Classes that correspond to a directive with an ` md- ` prefix should also be prefixed with ` Md ` .
196
+ CDK classes should not have a prefix.
197
+
190
198
##### Methods
191
- The name of a method should capture the action that is performed * by* that method.
199
+ The name of a method should capture the action that is performed * by* that method rather than
200
+ describing when the method will be called. For example,
201
+
202
+ ``` ts
203
+ /** AVOID: does not describe what the function does. */
204
+ handleClick () {
205
+ // ...
206
+ }
207
+
208
+ /** PREFER: describes the action performed by the function. */
209
+ activateRipple () {
210
+ // ...
211
+ }
212
+ ```
213
+
214
+ #### Inheritance
215
+
216
+ Avoid using inheritence to apply reusable behaviors to multiple components. This limits how many
217
+ behaviors can be composed.
192
218
193
219
### Angular
194
220
195
221
#### Host bindings
196
- Prefer using the ` host ` object in the directive configuration instead of ` @HostBinding ` and
197
- ` @HostListener ` . We do this because TypeScript preserves the type information of methods with
222
+ Prefer using the ` host ` object in the directive configuration instead of ` @HostBinding ` and
223
+ ` @HostListener ` . We do this because TypeScript preserves the type information of methods with
198
224
decorators, and when one of the arguments for the method is a native ` Event ` type, this preserved
199
- type information can lead to runtime errors in non-browser environments (e.g., server-side
200
- pre-rendering).
225
+ type information can lead to runtime errors in non-browser environments (e.g., server-side
226
+ pre-rendering).
201
227
202
228
203
229
### CSS
204
230
205
231
#### Be cautious with use of ` display: flex `
206
- * The [ baseline calculation for flex elements] ( http://www.w3.org/TR/css-flexbox-1/#flex-baselines )
207
- is different than other display values, making it difficult to align flex elements with standard
232
+ * The [ baseline calculation for flex elements] ( http://www.w3.org/TR/css-flexbox-1/#flex-baselines )
233
+ is different than other display values, making it difficult to align flex elements with standard
208
234
elements like input and button.
209
235
* Component outermost elements are never flex (block or inline-block)
210
236
* Don't use ` display: flex ` on elements that will contain projected content.
211
237
212
238
#### Use lowest specificity possible
213
- Always prioritize lower specificity over other factors. Most style definitions should consist of a
214
- single element or css selector plus necessary state modifiers. ** Avoid SCSS nesting for the sake of
239
+ Always prioritize lower specificity over other factors. Most style definitions should consist of a
240
+ single element or css selector plus necessary state modifiers. ** Avoid SCSS nesting for the sake of
215
241
code organization.** This will allow users to much more easily override styles.
216
242
217
243
For example, rather than doing this:
@@ -248,12 +274,12 @@ do this:
248
274
The end-user of a component should be the one to decide how much margin a component has around it.
249
275
250
276
#### Prefer styling the host element vs. elements inside the template (where possible).
251
- This makes it easier to override styles when necessary. For example, rather than
277
+ This makes it easier to override styles when necessary. For example, rather than
252
278
253
279
``` scss
254
280
the-host-element {
255
281
// ...
256
-
282
+
257
283
.some-child-element {
258
284
color : red ;
259
285
}
@@ -291,4 +317,4 @@ When it is not super obvious, include a brief description of what a class repres
291
317
292
318
// Portion of the floating panel that sits, invisibly, on top of the input.
293
319
.mat-datepicker-input-mask { }
294
- ```
320
+ ```
0 commit comments