|
| 1 | +# Boostnote Code Style |
| 2 | +When submitting your PR, please make sure that your code is well tested and follow the code style of Boostnote. |
| 3 | + |
| 4 | +The code style of Boostnote is listed in [`.eslintrc`](.eslintrc). We also have additional code styles that is not mentioned in that file. |
| 5 | + |
| 6 | +## Additional code styles |
| 7 | +### Use ES6 Object Destructing |
| 8 | + |
| 9 | +Please use Object Destructing whenever it's possible. |
| 10 | + |
| 11 | +**Example**: Importing from library |
| 12 | + |
| 13 | +```js |
| 14 | + |
| 15 | +// BAD |
| 16 | +import Code from 'library' |
| 17 | +const subCode = Code.subCode |
| 18 | +subCode() |
| 19 | + |
| 20 | +// GOOD |
| 21 | +import { subCode } from 'library' |
| 22 | +subCode() |
| 23 | +``` |
| 24 | + |
| 25 | +**Example**: Extract data from react component state |
| 26 | + |
| 27 | +``` |
| 28 | +// BAD |
| 29 | +<h1>{this.state.name}</h1> |
| 30 | +
|
| 31 | +// GOOD |
| 32 | +const { name } = this.state |
| 33 | +<h1>{name}</h1> |
| 34 | +``` |
| 35 | + |
| 36 | +### Use meaningful name |
| 37 | +This is actually not a "code style" but rather a requirement in every projects. Please name your variables carefully. |
| 38 | + |
| 39 | +**Example**: Naming callback function for event |
| 40 | + |
| 41 | +```js |
| 42 | +// BAD |
| 43 | +<h1 onclick={onClick}>Name</h1> |
| 44 | + |
| 45 | +// GOOD |
| 46 | +<h1 onclick={onNameClick}>Name</h1> |
| 47 | +``` |
| 48 | + |
| 49 | +### Don't write long conditional statement |
| 50 | +When writing a conditional statement, if it's too long, cut it into small meaningful variables. |
| 51 | + |
| 52 | +```js |
| 53 | +// BAD |
| 54 | +if (note.type == 'markdown' && note.index == 2 && note.content.indexOf('string') != -1) |
| 55 | + |
| 56 | +// GOOD |
| 57 | +const isSecondMarkdownNote = note.type == 'markdown' && note.index == 2 |
| 58 | +const isNoteHasString = note.content.indexOf('string') != -1 |
| 59 | +if (isSecondMarkdownNote && isNoteHasString) |
| 60 | +``` |
| 61 | + |
| 62 | +### Use class property instead of class methods |
| 63 | +When writing React components, try to use class property instead of class methods. The reason for this is explained perfectly here: |
| 64 | +https://codeburst.io/use-class-properties-to-clean-up-your-classes-and-react-components-93185879f688 |
| 65 | + |
| 66 | +**Example**: |
| 67 | + |
| 68 | +```js |
| 69 | +// BAD |
| 70 | +class MyComponent extends React.Component { |
| 71 | + myMethod () { |
| 72 | + // code goes here... |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// GOOD |
| 77 | +class MyComponent extends React.Component { |
| 78 | + myMethod = () => { |
| 79 | + // code goes here... |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
0 commit comments