- Declaring variables close to where they're used limits their scope, reducing the cognitive load.
- Use JSDoc for documentation (description, @todo, @returns, @param).
- Use normal comments for implementation details.
- Function declarations are clearer and hoistable.
- Prefer arrow functions for callbacks since they are simpler and bind
this.
- Non-default exports add type-safety.
- Exception: If the filename matches the export name, default exports can be used since they stand out and the syntax is cleaner.
- Marking parameters and
statevariables as readonly ensures immutability.
- Positional parameters of the same type are easy to mix up.
- Using a distinct type for failure makes the failure obvious.
- Prefer
nulloverundefinedto ensure the failure is handled even with optional parameters.
- Making illegal states unrepresentable, precise types and brands increase confidence and type safety, and reduce implementation complexity.
- Note: Big unions might hurt performance.
- Using a more generic argument type, as long as it doesn't affect the implementation, is more convenient.