Skip to content

Instantly share code, notes, and snippets.

@earonesty
Last active February 1, 2024 21:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earonesty/ccee25a56be7adeb5f670cf44e5fa479 to your computer and use it in GitHub Desktop.
Save earonesty/ccee25a56be7adeb5f670cf44e5fa479 to your computer and use it in GitHub Desktop.

Coding Guidelines

General

New Projects

  • Master branch must require code reviews and passing tests

  • All new projects must have a full CI pipeline, including linters, tests

  • Coverage enforcement is required

  • If a project has dependencies, it must use and commit a lockfile of some sort

Object-Oriented Design

  • Avoid global variables & long parameter lists

  • Limit dependencies of an object (entities an object depends on).

  • Limit an object's dependents (entities that depend on an object).

  • Prefer composition over inheritance.

  • Prefer inheritance over callbacks and branches.

  • Prefer small methods. Between one and five lines when it makes sense

  • Prefer small classes with a single, well-defined responsibility. When a
    class exceeds 100 lines, it may be doing too many things.

  • Tell, don't ask.

Opening files, using API's:

  • Client facing systems should never hit the internet/connect to a network filesystem, open a file that might be on a network drive, etc. ... as a precondition for startup. Missing network/internet always needs to be gracefully handled.

  • When opening a file or reading data from the internet in any way, assume it contains deliberately constructed malicious data. Write a test to prove that appropriate exceptions are thrown, and are gracefully handled.

  • When doing IPC, assume the other service can be down. Ensure timeouts & failures are handled.

Python

  • Avoid putting any real logic in del override

Javascript

  • Start new projects with Typescript

  • Use maps, not objects, as much as possible.

Testing

  • Don't test private methods.

  • Use intermediate assertions about state during test progression.

  • When at all possible, put your debug logs in the test, not in the code.

  • Use both integration tests and unit tests not one or the other.

  • Don't evade coverage checks.

Relational Databases

  • [Index foreign keys].

  • Constrain most columns as [NOT NULL].

  • In a SQL view, only select columns you need (i.e., no SELECT table.*).

  • Use an ORDER BY clause on queries where the results will be displayed to a
    user, as queries without one may return results in a changing, arbitrary
    order.

  • Never put meaningful or computed data in the "id" or "primary key" field of a table.

  • Sqlite need not be considered a "relational database", depending on what you're doing with it.

    • Use as key-value store is OK

    • Use as indexed "blob storage" instead of filesystem is OK

CI/CD & Scripts

  • No complex bash scripts.   Scripts that are complex enough to need stuff like functions and loops should be moved into a language suitable for complexity and testing, like python or node.

  • No one-off complex scripts.  Scripts that manage deployments, requirements, etc should, ideally be abstracted and run in many projects.... rather than each project having its own requirements, versioning and deployments systems.   This is especially true for libraries.

Web

  • Avoid rendering delays caused by synchronous javascript hits.

  • Use HTTPS instead of HTTP when linking to assets.

  • Use UTF-8 only

Security Best Practices

Yes, it's important for developers to consider security during every aspect of development. But especially when writing webites, public endpoints, storing customer data and more. Here's a quick list of some threats to consider and learn about so you can accurately review and code with security in mind:

  • Open Redirect
  • Insecure Direct Object References (IDOR)
  • Cross-Site Scripting (XSS)
  • Cross-Site Request Forgery (CSRF)
  • SQL Injection
  • Server-Side Request Forgery (SSRF)
  • The current OWASP Top 10-changes as vulns change, check it out. It's a cool site.

Changes to this document

To add new standards:

  • First make sure your proposal doesn't suck. Coding standards that suck have the following attributes:

  • Open up a slack discussion with at least 3 other devs and propose one or more additions or changes. Include the CTO and the Engineering Manager.

  • Edit this page with the verbiage everyone agrees on.

@CharlyJazz
Copy link

This is good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment