โœ… Coding standards

๐Ÿ‘‹ FYI, this note is over 6 months old. Some of the content may be out of date.
On this page

Naming Conventions Jump to heading

The objective of this document is to help when naming things and make the codebase consistent and easier to read.

Name Rules Jump to heading

๐Ÿฅณ Names should be always be Descriptive & Succinct. ๐ŸŽ‰

๐Ÿ˜‘ Names should not be Obscure or Abbreviated โŒ

File Names Jump to heading

Name Convention Example
Index file index.(ts/js) index.ts
React component ComponentName.(tsx/ts/js/jsx) Button.tsx
Test file ComponentName.test.(tsx/ts/js/jsx) Button.test.tsx
TypeScript types File.models.ts Button.models.ts
Styles (CSS-in-JS) ComponentName.styles.(ts/js) Button.styles.ts
Storybook ComponentName.stories.(tsx/ts/js/jsx/mdx) Button.stories.tsx

HTML Jump to heading

Name Convention Example
Data Attributes kebab-case data-testid="button-element"
path kebab-case www.fairfx.com/this/is-a-path/

TypeScript Jump to heading

Name Convention Example
interface PascalCase interface DescriptiveInterfaceName
variables camelCase const descriptiveVariableName
function camelCase descriptiveFunctionName(){ ... }
class PascalCase class DescriptiveClassName { ... }
type PascalCase type DescriptiveTypeName
enums PascalCase enum DescriptiveEnumName { CONSTANT_A, CONSTANT_B, CONSTANT_C }
constant CONSTANT_CASE DESCRIPTIVE_CONSTANT_NAME

React Jump to heading

Name Convention Example
Component Props Interface PascalCaseProps ComponentNameProps
Component State Interface PascalCaseState ComponentNameState
Component Copy Interface PascalCaseCopy ComponentNameCopy

Function Component Jump to heading

import React, { FunctionComponent } from 'react'

const DescriptiveComponentName: FunctionComponent<IDescriptiveComponentNameProps> = (
props
) =>
{
return ()
}

Class Component Jump to heading

import React, { Component } from 'react'

class DescriptiveComponentName<
IDescriptiveComponentNameProps,
IDescriptiveComponentNameState
> extends Component {
render () {
return ()
}
}

Code Structure Jump to heading

The objective of this document is to help when structuring your code, to make the codebase more consistent, reusable and easy to read.

This is a guide on how to structure your code.

Linting and code styling ๐Ÿ’… Jump to heading

  • โ—๏ธCode to be formatted using prettier
  • โ€ผ๏ธ All code should follow the TypeScript standard and always make use of types

React Jump to heading

Composability Jump to heading

react_composition_vs_inheritance

Components should ideally be composable, this makes them more flexible and reusable.

General rules about writing new components ๐Ÿšฆ Jump to heading

A standard practice is to avoid having too much functionality in one page with gigantic renders.
Each file should have 1 set of functionality.
Everything else should be broken into a new component and be included as a child.

Generic Jump to heading

Imports Jump to heading

Avoid * imports, itโ€™s best to explicitly define what you want to export.

Types Jump to heading

Avoid a type of any it is normally not allowed.

Spread Syntax Jump to heading

Spread_Syntax

Use nested spread syntax when appropriate. Code should be readable, donโ€™t use nested spread syntax if it becomes hard to read.

Preferable Example โœ… Jump to heading

class MyComponent extends Component {
public render() {
const {
myProp,
nestedProps: { myNestedProp },
} = this.props
}
}

Also Good โœ… Jump to heading

class MyComponent extends Component {
public render() {
const { myProp, nestedProps } = this.props
const { myNestedProp } = nestedProps
}
}

Ok, if itโ€™s only referenced once โœ… Jump to heading

class MyComponent extends Component {
public render() {
return this.props.nestedProps.myNestedProp.myMoreNestedProp
}
}

File Structure rules ๐Ÿšจ๐Ÿ‘ฎ Jump to heading

Description โ„น๏ธ Jump to heading

These rules must be followed by all team members in order to have a consistent and well structured codebase.

Pages Jump to heading

/src/client/pages
    /PageName
        index.ts
        PageName.tsx
        PageName.test.tsx
        PageName.schema.json
    /OtherPageName
        OtherPageName.tsx

Components should by default be placed in /components directory.

Components should be grouped into folders logically. If they are only used once and they are used within another component, they should be colocated with their parent component.

Example: Jump to heading

components/
โ”œโ”€ ComponentName/
โ”‚ โ”œโ”€ __tests__/
โ”‚ โ”‚ โ”œโ”€ ComponentNameView.spec.tsx
โ”‚ โ”‚ โ”œโ”€ ComponentNameContainer.spec.tsx
โ”‚ โ”œโ”€ ComponentName.operations.middleware.gql // Graphql queries
โ”‚ โ”œโ”€ ComponentName.messages.ts // i18n content
โ”‚ โ”œโ”€ ComponentName.models.ts // TS types
โ”‚ โ”œโ”€ ComponentNameView.tsx // "dumb" React component
โ”‚ โ”œโ”€ ComponentNameView.stories.tsx // Storybook stories
โ”‚ โ”œโ”€ ComponentNameContainer.tsx // "smart" React component. fetches data etc
โ”‚ โ”œโ”€ index.ts
โ”‚ โ”œโ”€ README.md

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿซ Storybook Jump to heading

Components should be built and tested with a ComponentName.stories.tsx and have a README.md file included.

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ”ฌ Test Jump to heading

Where possible ComponentName.test.tsx other files like utils will need a UtilName.test.ts.

๐Ÿ’๐Ÿปโ€โ™€๏ธ Styled Component Jump to heading

Separate styles from the main code, styles should live in ComponentName.styles.ts.

๐Ÿ‘ท๐Ÿปโ€โ™€๏ธ Models Jump to heading

Separate interfaces and types from the main code, models should live in ComponentName.models.ts


← Back home