Skip to content

Commit bbfad88

Browse files
committed
chore: add more naming and inheritence guidance to coding standards
1 parent 1326234 commit bbfad88

File tree

1 file changed

+58
-32
lines changed

1 file changed

+58
-32
lines changed

CODING_STANDARDS.md

+58-32
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ ES6 or TypeScript.
1212
### General
1313

1414
#### 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
1616
time than it would take to follow through the code itself.
1717

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.
2121
When collaborators are in different timezones, this can be devastating to productivity.
2222

2323
For example, this is a not-very-useful comment:
@@ -38,6 +38,11 @@ if (!$attrs['tabindex']) {
3838
}
3939
```
4040

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+
4146
#### Prefer more focused, granular components vs. complex, configurable components.
4247

4348
For example, rather than doing this:
@@ -55,22 +60,22 @@ do this:
5560
```
5661

5762
#### 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.
6065
Ideally, individual files are 200 - 300 lines of code.
6166

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

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

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
7176
prefer sticking to a _single_ API for accomplishing something.
7277

73-
### 100 column limit
78+
### 100 column limit
7479
All code and docs in the repo should be 100 columns or fewer. This applies to TypeScript, SCSS,
7580
HTML, bash scripts, and markdown files.
7681

@@ -81,7 +86,7 @@ Avoid `any` where possible. If you find yourself using `any`, consider whether a
8186
appropriate in your case.
8287

8388
For methods and properties that are part of a component's public API, all types must be explicitly
84-
specified because our documentation tooling cannot currently infer types in places where TypeScript
89+
specified because our documentation tooling cannot currently infer types in places where TypeScript
8590
can.
8691

8792
#### Fluent APIs
@@ -99,9 +104,9 @@ class ConfigBuilder {
99104
* Omit the `public` keyword as it is the default behavior.
100105
* Use `private` when appropriate and possible, prefixing the name with an underscore.
101106
* Use `protected` when appropriate and possible with no prefix.
102-
* Prefix *library-internal* properties and methods with an underscore without using the `private`
107+
* Prefix *library-internal* properties and methods with an underscore without using the `private`
103108
keyword. This is necessary for anything that must be public (to be used by Angular), but should not
104-
be part of the user-facing API. This typically applies to symbols used in template expressions,
109+
be part of the user-facing API. This typically applies to symbols used in template expressions,
105110
`@ViewChildren` / `@ContentChildren` properties, host bindings, and `@Input` / `@Output` properties
106111
(when using an alias).
107112

@@ -114,14 +119,14 @@ All public APIs must have user-facing comments. These are extracted and shown in
114119
on [material.angular.io](https://material.angular.io).
115120

116121
Private and internal APIs should have JsDoc when they are not obvious. Ultimately it is the purview
117-
of the code reviwer as to what is "obvious", but the rule of thumb is that *most* classes,
118-
properties, and methods should have a JsDoc description.
122+
of the code reviwer as to what is "obvious", but the rule of thumb is that *most* classes,
123+
properties, and methods should have a JsDoc description.
119124

120125
Properties should have a concise description of what the property means:
121126
```ts
122127
/** The label position relative to the checkbox. Defaults to 'after' */
123128
@Input() labelPosition: 'before' | 'after' = 'after';
124-
```
129+
```
125130

126131
Methods blocks should describe what the function does and provide a description for each parameter
127132
and the return value:
@@ -145,7 +150,7 @@ Boolean properties and return values should use "Whether..." as opposed to "True
145150

146151
##### General
147152
* Prefer writing out words instead of using abbreviations.
148-
* Prefer *exact* names over short names (within reason). E.g., `labelPosition` is better than
153+
* Prefer *exact* names over short names (within reason). E.g., `labelPosition` is better than
149154
`align` because the former much more exactly communicates what the property means.
150155
* Except for `@Input` properties, use `is` and `has` prefixes for boolean properties / methods.
151156

@@ -163,31 +168,52 @@ class UniqueSelectionDispatcher { }
163168
Avoid suffixing a class with "Service", as it communicates nothing about what the class does. Try to
164169
think of the class name as a person's job title.
165170

171+
Classes that correspond to a directive with an `md-` prefix should also be prefixed with `Md`.
172+
CDK classes should not have a prefix.
173+
166174
##### Methods
167-
The name of a method should capture the action that is performed *by* that method.
175+
The name of a method should capture the action that is performed *by* that method rather than
176+
describing when the method will be called. For example,
177+
178+
```ts
179+
/** AVOID: does not describe what the function does. */
180+
handleClick() {
181+
// ...
182+
}
183+
184+
/** PREFER: describes the action performed by the function. */
185+
activateRipple() {
186+
// ...
187+
}
188+
```
189+
190+
#### Inheritance
191+
192+
Avoid using inheritence to apply reusable behaviors to multiple components. This limits how many
193+
behaviors can be composed.
168194

169195
### Angular
170196

171197
#### Host bindings
172-
Prefer using the `host` object in the directive configuration instead of `@HostBinding` and
173-
`@HostListener`. We do this because TypeScript preserves the type information of methods with
198+
Prefer using the `host` object in the directive configuration instead of `@HostBinding` and
199+
`@HostListener`. We do this because TypeScript preserves the type information of methods with
174200
decorators, and when one of the arguments for the method is a native `Event` type, this preserved
175-
type information can lead to runtime errors in non-browser environments (e.g., server-side
176-
pre-rendering).
201+
type information can lead to runtime errors in non-browser environments (e.g., server-side
202+
pre-rendering).
177203

178204

179205
### CSS
180206

181207
#### Be cautious with use of `display: flex`
182-
* The [baseline calculation for flex elements](http://www.w3.org/TR/css-flexbox-1/#flex-baselines)
183-
is different than other display values, making it difficult to align flex elements with standard
208+
* The [baseline calculation for flex elements](http://www.w3.org/TR/css-flexbox-1/#flex-baselines)
209+
is different than other display values, making it difficult to align flex elements with standard
184210
elements like input and button.
185211
* Component outermost elements are never flex (block or inline-block)
186212
* Don't use `display: flex` on elements that will contain projected content.
187213

188214
#### Use lowest specificity possible
189-
Always prioritize lower specificity over other factors. Most style definitions should consist of a
190-
single element or css selector plus necessary state modifiers. **Avoid SCSS nesting for the sake of
215+
Always prioritize lower specificity over other factors. Most style definitions should consist of a
216+
single element or css selector plus necessary state modifiers. **Avoid SCSS nesting for the sake of
191217
code organization.** This will allow users to much more easily override styles.
192218

193219
For example, rather than doing this:
@@ -224,12 +250,12 @@ do this:
224250
The end-user of a component should be the one to decide how much margin a component has around it.
225251

226252
#### Prefer styling the host element vs. elements inside the template (where possible).
227-
This makes it easier to override styles when necessary. For example, rather than
253+
This makes it easier to override styles when necessary. For example, rather than
228254

229255
```scss
230256
the-host-element {
231257
// ...
232-
258+
233259
.some-child-element {
234260
color: red;
235261
}
@@ -267,4 +293,4 @@ When it is not super obvious, include a brief description of what a class repres
267293

268294
// Portion of the floating panel that sits, invisibly, on top of the input.
269295
.mat-datepicker-input-mask { }
270-
```
296+
```

0 commit comments

Comments
 (0)