Skip to content

Commit 563bdaf

Browse files
committed
update style guide
1 parent 186621f commit 563bdaf

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ The name was coined in fall 2016 and changed around spring 2017.
2828

2929
## Contributors
3030

31-
### YOU?!
32-
33-
New -- Feb 2022: If you want to work on something at https://github.com/sagemathinc/cocalc/issues, [contact us](email:[email protected]), and we might be able to pay you!
34-
35-
### Contributors
36-
3731
- Greg Bard
3832
- Rob Beezer
3933
- Blaec Bejarano
@@ -52,7 +46,7 @@ New -- Feb 2022: If you want to work on something at https://github.com/sagemat
5246
- Jonathan Thompson
5347
- Todd Zimmerman
5448

55-
... and _many_ others: See https://github.com/sagemathinc/cocalc/graphs/contributors
49+
... and others: See https://github.com/sagemathinc/cocalc/graphs/contributors
5650

5751
## Copyright/License
5852

@@ -92,3 +86,4 @@ We are grateful to BrowserStack for providing infrastructure to test CoCalc.
9286
### Google
9387

9488
We thank Google for donating over \$150K in cloud credits since 2014 to support this project.
89+

src/dev/STYLE.md

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
1-
CoCalc Style Guide
1+
# CoCalc Style Guide
22

3-
- Use the _defaults_ with the latest version of prettier on all of our Javascript and Typescript code.
3+
## Language (Javascript/Typescript/Python)
44

5-
- Language: Always prefer Typescript over Javascript and CoffeeScript
5+
- Prettier: Use the _defaults_ with the latest version of prettier on all of our Javascript and Typescript code. Use yapf for Python.
66

7-
- Use the standard Javascript camelCase convention for variable names, unless there is a good reason otherwise. Good reasons include: variable names that are also used in PostgreSQL and interop with Python (lower case with underscores).
7+
- NOTE: prettier's defaults change over time, but we don't ever just run it on our full massive codebase.
88

9-
- React: Always prefer functional components over class components
9+
- Typescript: Always prefer Typescript over Javascript and CoffeeScript
1010

11-
- React: Use the style of https://react.dev/learn/typescript for React with Typescript. In particular, use
11+
- NOTE: there's still some coffeescript code in CoCalc; it's terrifying and should all be rewritten in Typescript.
12+
13+
- Variable Names: Use the standard Javascript camelCase convention for variable names, unless there is a good reason otherwise. Good reasons include: variable names that are also used in PostgreSQL and interop with Python (lower case with underscores).
14+
15+
- NOTE: there's a lot of Javascript code in cocalc that uses Python conventions. Long ago Nicholas R. argued "by using Python conventions we can easily distinguish our code from other code"; in retrospect, this was a bad argument, and only serves to make Javascript devs less comfortable in our codebase, and make our code look weird compared to most Javascript code. Rewrite it.
16+
17+
- Javascript Methods: Prefer arrow functions for methods of classes.
18+
19+
- it's standard
20+
- avoids subtle problems involving "this" binding
21+
- easier to search for function definition in editor
22+
- NOTE: there's a lot of code in cocalc that uses non-arrow-functions; rewrite it and don't add more of this.
23+
24+
- Async Programming: Prefer async/await to callbacks if at all possible.
25+
26+
- NOTE: CoCalc used to not use async/await or promises at all, so there is still some code that uses the callback [async library](https://github.com/caolan/async). This code is terrifying, and it should all be rewritten.
27+
28+
## React
29+
30+
- Functional Components and hooks: Always prefer functional components with hooks over class components
31+
32+
- NOTE: there are still a lot of class components in cocalc; rewrite them.
33+
34+
- Use the style of https://react.dev/learn/typescript for React with Typescript:
35+
- typescript detects unused props,
36+
- defaults are natural and do not need MyButton.defaultProps, which is deprecated,
37+
- uses minimal notation (less characters typed),
38+
- very common in tutorials and other code
39+
- NOTE: a lot of our code used to be class components, hence still is written like `prop.[propname]`
40+
41+
In particular, use
1242

1343
```ts
1444
function MyButton({ title, disabled }: MyButtonProps) {
@@ -24,17 +54,28 @@ function MyButton(props: MyButtonProps) {
2454
}
2555
```
2656

27-
also, do NOT use
57+
or
2858

2959
```ts
3060
const MyButton: React.FC<MyButtonProps> = (props) => {
3161
return <button disabled={props.disabled}>{props.title}</button>;
3262
};
3363
```
3464

35-
Advantages of the first version:
65+
- Memoization: avoid using `React.memo`
66+
67+
- it leads to subtle bugs
68+
- it's far better to render 20 items slowly, e.g., using virtualization, then to render 20000 items quickly
69+
- NOTE: there's 100\+ uses of React.memo in cocalc right now; I'm not happy with this, though I wrote them. It's almost always a bad idea.
70+
71+
## UI Design
72+
73+
- As much as possible, use [Antd components](https://ant.design/) in the standard way.
74+
75+
- Avoid doing new design if possible; use the conventions and components of Antd.
76+
- If there is a cancel button next to another button, then cancel goes first, following the Antd convention, e.g., see Popconfirm.
77+
- NOTE: We wrote a lot of custom components, e.g., for number input, before switching fully to Antd, and those are still partly in use. Rewrite all of that to use Antd.
3678

37-
- typescript detects unused props,
38-
- defaults are natural and do not need MyButton.defaultProps, which is deprecated,
39-
- uses minimal notation (less characters typed),
40-
- very common in tutorials and other code
79+
- Bootstrap:
80+
- CoCalc used to use jquery + bootstrap (way before react even existed!) for everything, and that's still in use for some things today (e.g., Sage Worksheets). Rewrite or delete all this.
81+
- CoCalc also used to use react-bootstrap, and sadly still does. Get rid of this.

0 commit comments

Comments
 (0)