-
Notifications
You must be signed in to change notification settings - Fork 4
CityCatalyst Coding Standards
Maureen Fonseca Mora edited this page Aug 31, 2023
·
6 revisions
To ensure uniformity of our code base the following standards should be applied.
- Indenting and Formatting:
- Choose an indentation style (e.g., 2 spaces or 4 spaces) and stick with it throughout the project.
- Use consistent line breaks and spacing for improved readability.
- Naming Conventions:
- Follow consistent naming conventions for variables, functions, classes, and components.
- Use meaningful and descriptive names that convey the purpose and functionality of the code element.
- For TypeScript, consider using camelCase for variables and functions, and PascalCase for classes and components.
- Type Annotations:
- Make full use of TypeScript's static type system to catch errors early.
- Always annotate function parameters and return types with appropriate type definitions.
- Use interfaces or type aliases for complex data structures.
- Imports and Exports:
- Use explicit import and export statements for better clarity.
- Organize imports alphabetically and group them by external dependencies, internal modules, and relative imports.
- Components and Containers:
- Separate your components into presentational (dumb) components and container (smart) components.
- Presentational components focus on rendering UI and receive data via props.
- Container components handle data logic and interact with Redux, state, or context.
- State Management:
- If using Redux, structure your store with actions, reducers, and selectors.
- Use async actions and middleware for handling asynchronous operations.
- Consider using the Redux Toolkit to simplify store setup and reduce boilerplate.
- File Structure:
- Organize your files and folders logically to promote easy navigation.
- Group related components, styles, and tests in the same directory.
- Code Comments:
- Write clear and concise comments explaining complex logic, algorithms, or code decisions.
- Avoid unnecessary comments that simply repeat what the code is doing.
- Testing:
- Write unit tests and integration tests for your components and functions.
- Use a testing library like Jest along with testing utilities like React Testing Library.
- Linting and Formatting Tools:
- Use ESLint and Prettier to enforce coding standards and formatting rules.
- Configure them to automatically format code and catch potential issues.
- Code Reviews:
- Conduct regular code reviews to ensure adherence to coding standards.
- Encourage team members to provide constructive feedback during reviews.
- Documentation:
- Maintain a README file with project setup instructions, architecture overview, and any other necessary information.
- Document important decisions, libraries used, and any coding guidelines that may not be obvious.
Prettier is an opinionated code formatter with support for:
- JavaScript (including experimental features)
- JSX
- TypeScript
- CSS, Less, and SCSS
- HTML
- JSON
- GraphQL
- Markdown, including GFM and MDX v1
- YAML,
We are using prettier to enable developers stick to the standard of code. They are two ways to use prettier:
- Run the install
npm install --save-dev --save-exact prettier
- Create an empty config file in the root of you directory
echo {}> .prettierrc.json
- Next, create a .prettierignore file to let the Prettier CLI and editors know which files to not format. Here’s an example:
# Ignore artifacts:
build
coverage
node_modules
.next
- Running prettier
npx prettier . --write
- If you have a CI setup, run the following as part of it to make sure that everyone runs Prettier. This avoids merge conflicts and other collaboration issues!
npx prettier . --check
- Setting Rules - Here's and example of how you can set rules in
.prettierrc
file:
{
"semi": true, // this tells the code editor to add semi-colons on save
"overrides": [] // if there any overrides you can set them in here
}
- Search for prettier in vscode extensions tab
- Once installed goto, Settings, type on prettier in the search bar, select Text Editor select prettier as the default formatter in the dropdown,
- Search for formatonsave on the search bar enable it on the check mark.
To ensure uniformity of our code base the following standards should be applied (source)[https://peps.python.org/pep-0008/].
- Indentation:
- Spaces are the preferred indentation method.
- Use 4 spaces per indentation level. It is optional for continuation lines.
- Format:
- Close brace/bracket/parenthesis on multiline constructs may either line up under the first non-whitespace character of the last line of list.
- Limit all lines to a maximum of 79 characters (for docstrings/comments limit to 72).
- Break formulas before binary operations.
- Surround top-level function and class definitions with two blank lines.
- Method definitions inside a class are surrounded by a single blank line.
- Extra blank lines may be used (sparingly) to separate groups of related functions.
- Single-quote string and double-quoted strings are the same. Pick one and stick to it.
- Avoid trailing whitespace anywhere.
- Imports:
- Imports should usually be on separate lines.
- Imports are always put at the top of the file.
- Following order: standard library imports, related third party imports, local application/library specific imports.
- Absolute imports are recommended.
- Code Comments:
- Write clear and concise comments explaining complex logic, algorithms, or code decisions.
- Avoid unnecessary comments that simply repeat what the code is doing.
- The first word should be capitalized, unless it is an identifier that begins with a lower case latter.
- Naming Conventions:
- Names that are visible to the user as public parts of the API should follow conventions that reflect usage rather than implementation.
- Follow consistent naming conventions for variables, functions and classes.
- Use meaningful and descriptive names that convey the purpose and functionality of the code element.
- Modules should have short, all-lowercase names.
- Class names should use the CapWords convention.
- Function and variable names should be lowercase, with words separated by underscores as necessary.
- Constants are written in all capital letters with underscores separating words.
- Linting and Formatting Tools:
- Black code style is a PE 8 compliant opinionated formatter.
- Code Reviews:
- Conduct regular code reviews to ensure adherence to coding standards.
- Encourage team members to provide constructive feedback during reviews.
- Documentation:
- Document important decisions, libraries used, and any coding guidelines that may not be obvious.