Coding Conventions
JavaScript
-
Arrow functions params
- Zero params:
() => {}
- One param:
x => {}
- Multiple params:
(x, y) => {}
- Zero params:
-
Arrow functions implicit returns
() => true;
() => { return true; };
-
if, for, else, ...
if () { something; }
-
Semicolons not optional
-
Naming
camelCase
- Except components and classes, those are
PascalCase
- Except components and classes, those are
-
Destructuring
- In functions
function something(arg) { const {a, b, c} = arg; ...}
- In local code
const {a, b, c} = this.props;
- In functions
-
Imports
import x from './something';
import * as x from './something';
x.a()
-
Requires
const x = require('./something');
-
Exports
export default value;
export default function;
export const f1 = () => {...} export const f2 = () => {...}
function f1() {...} function f2() {...} module.exports = { f1, f2 }
module.exports = value;
module.exports = function;
-
Async/await everywhere
React
-
Components are folders with two files -
ComponentUi.js
andindex.js
- UI component is stateless functional component that defines UI
- Container component renders UI component and attaches handlers, state and connects it to Redux IF NEEDED
-
Functional components
- destructuring
const Comp = ({p1, p2}) => {...}
- destructuring
-
Code organization
- Components
- Component
ComponentsUi.js
index.js
- Component
- Screens
- module
- submodule - concrete screen in module
- Component1
Component1Ui.js
index.js
- Component2
Component2Ui.js
index.js
- ModuleScreenName
ModuleScreenUi.js
index.js
index.js
- Component1
- submodule - concrete screen in module
- module
- Components
-
Render method
- When using conditionals, use this form:
- something && something &&
Component
...Component
- something && something &&
- When using conditionals, use this form:
-
Event handlers
- When onsomething use handlesomething
onClick={handleClick}
- When onsomething use handlesomething
Redux
- Separate properties for "data", "state" and "screens"?
Translations
- Naming must be done so it reflects what it is used for, not from technological point of view, but instead from user point of view
- If there is a text that acts like a button, it should be named like it is a button or a link since from users point of view, that is a button or link, not a text
- Naming must be done so that name itself is enough to know what it represents on a screen, without opening screen every time to check
linkButton
is not a good name since it doesn't have enough info on what this button links to — it should be something likechangeEmailButton
, even if this is the only button on the screen
- Global namespace should be used only for reusable, generic translations. Email is translated as Email, OK is translated as OK
- But if there is an action dialog with two options (success and discard actions), it might be better to have
dialogSuccessButton
anddialogDiscardButton
translations instead of globalok
andcancel
ones, even if the text is the same for those two- Think and choose what makes more sense for every use case
- But if there is an action dialog with two options (success and discard actions), it might be better to have
- Translations should be divided into module per screen with one global module
- Inside a module, all translations should be flat without additional nesting
- Buttons
- somethingButton
- Inputs
- somethingInputLabel
- somethingInputPlaceholder
- Messages
- somethingMessage
- somethingText
- Header
- somethingHeader