You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-37Lines changed: 38 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -118,7 +118,7 @@ function getUser(): User;
118
118
119
119
### Use searchable names
120
120
121
-
We will read more code than we will ever write. It's important that the code we do write is readable and searchable. By not naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable. Tools like [TSLint](https://palantir.github.io/tslint/rules/no-magic-numbers/) can help identify unnamed constants.
121
+
We will read more code than we will ever write. It's important that the code we do write is readable and searchable. By *not* naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable. Tools like [TSLint](https://palantir.github.io/tslint/rules/no-magic-numbers/) can help identify unnamed constants.
122
122
123
123
**Bad:**
124
124
@@ -658,11 +658,11 @@ console.log(name);
658
658
659
659
### Avoid Side Effects (part 2)
660
660
661
-
In JavaScript, primitives are passed by value and objects/arrays are passed by reference. In the case of objects and arrays, if your function makes a change in a shopping cart array, for example, by adding an item to purchase, then any other function that uses that cart array will be affected by this addition. That may be great, however it can be bad too. Let's imagine a bad situation:
661
+
In JavaScript, primitives are passed by value and objects/arrays are passed by reference. In the case of objects and arrays, if your function makes a change in a shopping cart array, for example, by adding an item to purchase, then any other function that uses that `cart` array will be affected by this addition. That may be great, however it can be bad too. Let's imagine a bad situation:
662
662
663
-
The user clicks the "Purchase", button which calls a purchase function that spawns a network request and sends the cart array to the server. Because of a bad network connection, the purchase function has to keep retrying the request. Now, what if in the meantime the user accidentally clicks "Add to Cart" button on an item they don't actually want before the network request begins? If that happens and the network request begins, then that purchase function will send the accidentally added item because it has a reference to a shopping cart array that the *addItemToCart* function modified by adding an unwanted item.
663
+
The user clicks the "Purchase", button which calls a `purchase` function that spawns a network request and sends the `cart` array to the server. Because of a bad network connection, the purchase function has to keep retrying the request. Now, what if in the meantime the user accidentally clicks "Add to Cart" button on an item they don't actually want before the network request begins? If that happens and the network request begins, then that purchase function will send the accidentally added item because it has a reference to a shopping cart array that the `addItemToCart` function modified by adding an unwanted item.
664
664
665
-
A great solution would be for the *addItemToCart* to always clone the cart, edit it, and return the clone. This ensures that no other functions that are holding onto a reference of the shopping cart will be affected by any changes.
665
+
A great solution would be for the `addItemToCart` to always clone the `cart`, edit it, and return the clone. This ensures that no other functions that are holding onto a reference of the shopping cart will be affected by any changes.
Polluting globals is a bad practice in JavaScript because you could clash with another library and the user of your API would be none-the-wiser until they get an exception in production. Let's think about an example: what if you wanted to extend JavaScript's native Array method to have a diff method that could show the difference between two arrays? You could write your new function to the `Array.prototype`, but it could clash with another library that tried to do the same thing. What if that other library was just using `diff` to find the difference between the first and last elements of an array? This is why it would be much better to just use classes and simply extend the `Array` global.
693
+
Polluting globals is a bad practice in JavaScript because you could clash with another library and the user of your API would be none-the-wiser until they get an exception in production. Let's think about an example: what if you wanted to extend JavaScript's native Array method to have a `diff` method that could show the difference between two arrays? You could write your new function to the `Array.prototype`, but it could clash with another library that tried to do the same thing. What if that other library was just using `diff` to find the difference between the first and last elements of an array? This is why it would be much better to just use classes and simply extend the `Array` global.
// The generator doesn't keep the array of all numbers.
1022
1023
function* fibonacci():IterableIterator<number> {
1023
1024
let [a, b] = [0, 1];
1024
-
1025
+
1025
1026
while (true) {
1026
1027
yielda;
1027
1028
[a, b] = [b, a+b];
@@ -1040,9 +1041,9 @@ function print(n: number) {
1040
1041
print(10);
1041
1042
```
1042
1043
1043
-
There are libraries that allow working with iterables in a simillar way as with native arrays, by
1044
+
There are libraries that allow working with iterables in a similar way as with native arrays, by
1044
1045
chaining methods like `map`, `slice`, `forEach` etc. See [itiriri](https://www.npmjs.com/package/itiriri) for
1045
-
an example of advanced manipulation with iterables (or [itiriri-async](https://www.npmjs.com/package/itiriri-async) for manipulation of async iterables).
1046
+
an example of advanced manipulation with iterables (or [itiriri-async](https://www.npmjs.com/package/itiriri-async) for manipulation of async iterables).
Testing is more important than shipping. If you have no tests or an inadequate amount, then every time you ship code you won't be sure that you didn't break anything.
1981
1982
Deciding on what constitutes an adequate amount is up to your team, but having 100% coverage (all statements and branches)
1982
-
is how you achieve very high confidence and developer peace of mind. This means that in addition to having a great testing framework, you also need to use a good coverage tool.
1983
+
is how you achieve very high confidence and developer peace of mind. This means that in addition to having a great testing framework, you also need to use a good [coverage tool](https://github.com/gotwarlost/istanbul).
1983
1984
1984
-
There's no excuse to not write tests. There are plenty of good JS test frameworks with typings support for TypeScript, so find one that your team prefers. When you find one that works for your team, then aim to always write tests for every new feature/module you introduce. If your preferred method is Test Driven Development (TDD), that is great, but the main point is to just make sure you are reaching your coverage goals before launching any feature, or refactoring an existing one.
1985
+
There's no excuse to not write tests. There are [plenty of good JS test frameworks](http://jstherightway.org/#testing-tools) with typings support for TypeScript, so find one that your team prefers. When you find one that works for your team, then aim to always write tests for every new feature/module you introduce. If your preferred method is Test Driven Development (TDD), that is great, but the main point is to just make sure you are reaching your coverage goals before launching any feature, or refactoring an existing one.
1985
1986
1986
1987
### The three laws of TDD
1987
1988
@@ -1997,15 +1998,15 @@ There's no excuse to not write tests. There are plenty of good JS test framework
1997
1998
1998
1999
Clean tests should follow the rules:
1999
2000
2000
-
***Fast** tests should be fast because we want to run them frequently.
2001
+
-**Fast** tests should be fast because we want to run them frequently.
2001
2002
2002
-
***Independent** tests should not depend on each other. They should provide same output whether run independently or all together in any order.
2003
+
-**Independent** tests should not depend on each other. They should provide same output whether run independently or all together in any order.
2003
2004
2004
-
***Repeatable** tests should be repeatable in any environment and there should be no excuse for why they fail.
2005
+
-**Repeatable** tests should be repeatable in any environment and there should be no excuse for why they fail.
2005
2006
2006
-
***Self-Validating** a test should answer with either *Passed* or *Failed*. You don't need to compare log files to answer if a test passed.
2007
+
-**Self-Validating** a test should answer with either *Passed* or *Failed*. You don't need to compare log files to answer if a test passed.
2007
2008
2008
-
***Timely** unit tests should be written before the production code. If you write tests after the production code, you might find writing tests too hard.
2009
+
-**Timely** unit tests should be written before the production code. If you write tests after the production code, you might find writing tests too hard.
2009
2010
2010
2011
**[⬆ back to top](#table-of-contents)**
2011
2012
@@ -2362,19 +2363,19 @@ Formatting is subjective. Like many rules herein, there is no hard and fast rule
2362
2363
2363
2364
For TypeScript there is a powerful tool called [TSLint](https://palantir.github.io/tslint/). It's a static analysis tool that can help you improve dramatically the readability and maintainability of your code. There are ready to use TSLint configurations that you can reference in your projects:
2364
2365
2365
-
*[TSLint Config Standard](https://www.npmjs.com/package/tslint-config-standard) - standard style rules
2366
+
-[TSLint Config Standard](https://www.npmjs.com/package/tslint-config-standard) - standard style rules
*[TSLint Clean Code](https://www.npmjs.com/package/tslint-clean-code) - TSLint rules inspired by the [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)
2370
+
-[TSLint Clean Code](https://www.npmjs.com/package/tslint-clean-code) - TSLint rules inspired by the [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)
2370
2371
2371
-
*[TSLint react](https://www.npmjs.com/package/tslint-react) - lint rules related to React & JSX
2372
+
-[TSLint react](https://www.npmjs.com/package/tslint-react) - lint rules related to React & JSX
2372
2373
2373
-
*[TSLint + Prettier](https://www.npmjs.com/package/tslint-config-prettier) - lint rules for [Prettier](https://github.com/prettier/prettier) code formatter
2374
+
-[TSLint + Prettier](https://www.npmjs.com/package/tslint-config-prettier) - lint rules for [Prettier](https://github.com/prettier/prettier) code formatter
2374
2375
2375
-
*[ESLint rules for TSLint](https://www.npmjs.com/package/tslint-eslint-rules) - ESLint rules for TypeScript
2376
+
-[ESLint rules for TSLint](https://www.npmjs.com/package/tslint-eslint-rules) - ESLint rules for TypeScript
2376
2377
2377
-
*[Immutable](https://www.npmjs.com/package/tslint-immutable) - rules to disable mutation in TypeScript
2378
+
-[Immutable](https://www.npmjs.com/package/tslint-immutable) - rules to disable mutation in TypeScript
2378
2379
2379
2380
Refer also to this great [TypeScript StyleGuide and Coding Conventions](https://basarat.gitbooks.io/typescript/docs/styleguide/styleguide.html) source.
2380
2381
@@ -2495,7 +2496,7 @@ class PerformanceReview {
2495
2496
2496
2497
private lookupManager() {
2497
2498
returndb.lookup(this.employee, 'manager');
2498
-
}
2499
+
}
2499
2500
2500
2501
private getSelfReview() {
2501
2502
// ...
@@ -2597,7 +2598,7 @@ The use of a comments is an indication of failure to express without them. Code
2597
2598
2598
2599
### Prefer self explanatory code instead of comments
2599
2600
2600
-
Comments are an apology, not a requirement. Good code mostly documents itself.
2601
+
Comments are an apology, not a requirement. Good code *mostly* documents itself.
2601
2602
2602
2603
**Bad:**
2603
2604
@@ -2643,7 +2644,7 @@ type User = {
2643
2644
2644
2645
### Don't have journal comments
2645
2646
2646
-
Remember, use version control! There's no need for dead code, commented code, and especially journal comments. Use git log to get history!
2647
+
Remember, use version control! There's no need for dead code, commented code, and especially journal comments. Use `gitlog` to get history!
2647
2648
2648
2649
**Bad:**
2649
2650
@@ -2748,7 +2749,7 @@ function getActiveSubscriptions(): Promise<Subscription[]> {
2748
2749
}
2749
2750
```
2750
2751
2751
-
**Good**
2752
+
**Good:**
2752
2753
2753
2754
```ts
2754
2755
function getActiveSubscriptions():Promise<Subscription[]> {
0 commit comments