Skip to content

Commit 249f35b

Browse files
authored
chore: Start implementing development guidelines. (#802)
Start adding some development guidelines to the contributing doc.
1 parent ec43ac8 commit 249f35b

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

CONTRIBUTING.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,81 @@ The SDK contract test suite will run the Node.js Server version of the SDK.
4747
```bash
4848
yarn run contract-tests
4949
```
50+
51+
Tests cases should be written using `it` and should read as a sentence including the `it`:
52+
```TypeScript
53+
it('does not load flags prior to start', async () => {/* test code */}
54+
```
55+
56+
Describe blocks should be used for common setup for a series of tests:
57+
```TypeScript
58+
describe('given a mock filesystem and memory feature store', { /* tests */})
59+
```
60+
61+
These then combined to create an understandable test name:
62+
`given a mock filesystem and memory feature store > it does not load flags prior to start`
63+
64+
## Development Guidelines
65+
66+
These are a series of recommendations for developing code in this repository. Not all existing code will comply
67+
with these guidelines, but new code should unless there are specific reasons not to.
68+
69+
While we develop code in TypeScript we generally want to aim for the compiled JavaScript to not be substantially different than if it had been written as JavaScript.
70+
71+
### Avoid using TypeScript enumerations. Instead use unions of strings.
72+
73+
Bad:
74+
```TypeScript
75+
export enum ValueType {
76+
Bool = 'bool',
77+
Int = 'int',
78+
Double = 'double',
79+
String = 'string',
80+
Any = 'any',
81+
}
82+
```
83+
84+
Good:
85+
```TypeScript
86+
export type ValueType = 'bool' | 'int' | 'double' | 'string' | 'any'
87+
```
88+
89+
While we are using TypeScript not all consumers of our code will be. Using a TypeScript enum from JavaScript is not very ergonomic.
90+
Additionally the code size associated with enums is going to be larger. The enum actually generates code, where the union provides type safety, but has no impact on the generated code.
91+
92+
### Prefer interfaces over classes when reasonable, especially if publicly exposed.
93+
94+
Bad:
95+
```TypeScript
96+
class MyData {
97+
public mutable: string;
98+
constructor(private readonly value: string private readonly another: string);
99+
}
100+
```
101+
102+
Good:
103+
```TypeScript
104+
interface MyData {
105+
readonly value: string;
106+
readonly another: string;
107+
mutable: string;
108+
}
109+
110+
function createMyData(value: string, another: string, mutable: string): MyData {
111+
return {
112+
value,
113+
another,
114+
mutable
115+
}
116+
}
117+
```
118+
119+
There are several potential problems using classes and some of them may be unexpected.
120+
121+
Classes produce JavaScript code while interfaces only represent contracts and don't exist in the generated JavaScript. In client-side applications keeping size to a minimum is very important.
122+
123+
The minification of associated functions is also another major difference. Functions that are not exported from the package can have their names minified. Methods that are part of a class are generally not minified.
124+
125+
A number of classes are present in the SDKs that cannot be removed without a major version. In order to reduce the size of these classes we have added support for minification of any member that starts with an underscore.
126+
127+
Another thing to remember is that the private and readonly only really affect TypeScript. Using JavaScript you can still access and mutate. Our minification of private members starting with an underscore also helps prevent unintentional usage from JavaScript.

0 commit comments

Comments
 (0)