Skip to content

Instantly share code, notes, and snippets.

@davesag
Last active August 9, 2022 10:28
Show Gist options
  • Save davesag/c9c93aec664c4f682a556d9a33784c07 to your computer and use it in GitHub Desktop.
Save davesag/c9c93aec664c4f682a556d9a33784c07 to your computer and use it in GitHub Desktop.
Development Norms

A common set of development norms

Adhering to a common set of developer norms is a vital factor when a team of people is trying to produce high quality, timely, business critical software.

I'm regularly asked to work on codebases that do not conform to any known measure of coding standards.

Issues include, but are not limited to:

  • no linting
  • no, or few, or inadequate levels of automated testing
  • no common README format
  • out of date or simply innacurate README docs
  • no common CONTRIBUTING standards
  • no standards for PR, branch, and commit message naming
  • too many branches, making it impossible for a new developer to know where to start
  • poor choice of repository or project names
  • repositories scattered across multiple accounts
  • lack of enforcement of CI/CD use

Coding standards are, by their very nature, opinionated, and the following represents my own opinion, based on decades of professional software development, of what our common coding standards ought to be. These standards are mostly independent of programming language (I've included some language specific recommendations as well however) and present a 'best-practice' approach to developing high quality software.

Repository Structure

  1. For any codebase there will be a repository, known as the 'upstream' repository.

    The benefits of enforcing this are as follows

    1. When a new developer joins the project it's easy for them to see immediately which branch contains the latest work in progress and which branch contains the latest release code.
    2. For public facing repositories it presents a clean and consistent public face for our codebase, showing us in the best possible light.
    3. Each developer can use their own fork to do whatever work they need to, and the onus is on them to keep their own repositories tidy.
  2. The Upstream Repository will only have two branches, develop, containing the latest working code, and master containing the current release.

  3. The default branch will be develop

  4. The develop and master branches are locked down such that the only way code can be contributed to them is via a peer-reviewed pull-request.

  5. Over and above the default labels provided by GitHub add documentation and feature labels.

  6. Assign teams admins with admin rights, and developers with write access to the repo.

Workflow

For work in progress developers will use the forked-git-flow process, namely they will

  1. Fork the upstream repository into their own personal accounts, clone the fork to your local machine, and set up the upstream as a remote.

     git remote add upstream git@github.com:<repo>.git
     git remote set-url --push upstream no_push
    
  2. Create a feature branch. (I use the git-flow command-line tools to do this, see below for discussion of branch, PR and commit message naming) git flow feature start ...

    Developers should not, as a general rule be working in other developer's branches, but if it's really needed they can by setting up another git remote

  3. do work

  4. ensure work is accompanied by tests

  5. ensure test coverage is not decreased by the work

  6. ensure work conforms to common linting standards (For Javascript projects I use eslint and prettier for this, but tools vary from language to language)

  7. ensure work is consistent with current documentation

  8. when work is done, publish the branch to your personal fork (git flow feature publish ...)

  9. using GitHub to create a Pull Request against the upstream develop branch (see below for PR naming scheme)

  10. assign reviewers, appropriate labels, and assign the PR to yourself

  11. wait for CI/CD to run and give a green light

  12. notify devs on slack that the PR is awaiting review

  13. respond to any review comments / make changes as appropriate. (developer can simply git push their alts to update the same PR)

  14. when all changes / PR is approved, merge the PR and delete the branch

  15. git checkout develop then git pull upstream develop, then git push to ensure your fork is in sync with the upstream.

CI/CD configuration

We use a mix of TravisCI and CircleCI for our CI/CD needs. Whichever we use (each have their own pros and cons but are essentially similar, so this advice is agnostic) the CI/CD must be configured to

  1. run linter
  2. run unit tests
  3. run integration tests
  4. deploy to staging (in the case of a run against develop), or production (in the case of a run against master)
  5. Notify Slack / Jira

Naming things

Features

Features must be named per the following pattern #{issue number}/{some_descriptive-text} — so for example, if you are working on issue ABC-1 with the title "do the thing", call your feature ABC-1/do_the-thing. Obviously use your common sense to avoid making the feature names too long.

Note this will creating a feature via git flow will create a branch called feature/{issue number}/{some_descriptive-text}.

Commit Messages

When committing something use the -m flag to add a short commit message of the format {issue number} summary of what you changed. So for example if you are working on issue ABC-1 and you added a method to the aardvark_controller you might use the following commit message "ABC-1 added anteater method to aardvark controller"

Commit messages ought to be in the past tense.

In general try to group file changes wherever appropriate, so if your controller change also involved updating something in a helper file, the one commit message can happily encompas the changes to both files. The message ought to reflect the main aim of the change.

Pull Requests

Pull requests must be named as follows [issue type, issue number] high level description of change. The following Issue Types are recognised

  • Bug Fix - the change fixes a bug
  • Feature - the change adds a new feature (the usual issue type)
  • Documentation — The change is a documentation only change
  • Optimisation - The change is an optimisation of the code base without any functional changes

If your change does not fit any of these categories, use Feature. Likewise if your change is not tied to an issue number you may use n/a instead.

So to use the above example your Pull Request would be named [Feature, ABC-1] added anteater to aardvark

README format

Every project needs a file README.md written in Github Flavoured Markdown, with sections as follows

  1. A short overview of the project

  2. Current CI/CD status for both develop and master branches (Both Circle and Travis provide badges for this)

  3. Prerequisites and expectied prior knowlede including links to associated repositories

  4. Useage instructions

  5. Development instructions

    1. How to build,
    2. How to test
    3. Any specific environment variables needed
  6. Deployment instructions

  7. Contributing nodes (ususally just an instructon to see CONTRIBUTING.md)

Linting

For Javascript projects I use eslint and prettier to enforce a common coding style, configured via the following base .eslint.json file. This can be enhanced with React specific plugins as required, but overall stick to the defaults but with enforced single quotes and no trailing semi-colons.

{
  "extends": [
    "standard",
    "prettier",
    "prettier/standard"
  ],
  "plugins": [
    "prettier",
    "standard",
    "mocha"
  ],
  "parserOptions": {
    "sourceType": "module"
  },
  "env": {
    "es6": true,
    "node": true,
    "mocha": true
  },
  "rules": {
    "prettier/prettier": ["error", { "singleQuote": true, "semi": false }]
  }
}

For solidity projects I use solhint with the following configuration

{
  "extends": "default",
  "rules": {
    "compiler-fixed": false,
    "indent": ["error", 4],
    "quotes": ["error", "single"],
    "max-line-length": ["error", 120]
  }
}

Use Docker for external dependencies

For code that requires external dependencies such as Mongo, Redis, Postgres, etc, ensure there is a docker-compose.yml file configured to run those dependencies. Do not assume that a developer has Mongo etc already installed. If the developer is a contractor with multiple clients it's often difficult or impossible for them to run such things on their bare metal.

Other services

In addition to using standard CI/CD such as Travis or Circle

  1. Use codecov.io to QA code coverage
  2. Use Greenkeeper to keep dependencies up to date
  3. For Javascript projects try and avoid using grunt or other dated build systems and stick to basic npm commands within the package.json file and use Webpack for bundling and for asset pipeline management
  4. Avoid use of transpilers such as Babel for pure Node services such as API servers or libraries. They complicate release management. Instead ensure you are using a modern version of Node (9.7 or better) which includes almost all of the modern ES6 (and some ES7) features such as object spread notation out of the box.

How to contribute to this project

Development Environment

All development is assumed to be done on a Mac running a modern version of OS X but ought to be pretty much the same no matter what unixy environment you use.

Development Process

All development is to follow the standard git-flow process, modified to allow for code-reviews.

See this handy, if ugly, cheat sheet.

Setup

  1. Fork this repo into your personal GitHub account
  2. clone your fork to your local development machine
  3. Set this repo as the upstream repo git remote add upstream <insert the upstream url>
  4. Disallow direct pushing to upstream git remote set-url --push upstream no_push
  5. create a local master branch git checkout -b master and test it via git pull upstream master
  6. ensure you have installed the git-flow command line helpers and git-flow-completion utils then run git flow init -d.

Optional Git Setup

Set up git to always rebase rather than merge.

git config --global branch.autosetuprebase always

Make sure git knows you are using your correct email.

git config user.email "username@domain.suffix"

Working on new features

  1. Create a "feature branch" for the change you wish to make via git flow feature start {feature_name}. See below for how to name features.
  2. Now work on your changes locally until you are happy the issue is resolved. See below for how to name commit messages.
  3. git flow feature publish {feature_name} will push it back up to your fork on GitHub.
  4. Use git flow feature pull {remote_name} {feature_name} to bring in any other changes, If other people have also merged changes in, and you can't merge your PR automatically you'll need to rebase their changes into your changes and then --force push the resulting changes using standard git commands.
  5. Use GitHub to raise a Pull Request. Add labels as appropriate, and set one or more reviewers. Then paste the url of the PR into the #development Slack channel with a request for someone to please review the changes. See below for how to name pull requests.
  6. Respond to any comments as appropriate, making changes and git push ing further changes as appropriate.
  7. When all comments are dealt and the PR finally gets a 👍 from someone else then merge the PR. Note we will not be using the git flow feature finish option as that merges into develop automatically without the option for review. see this stackexchange for more on that.
  8. In your command-line git checkout develop then git pull upstream develop to get the latest code and git branch -D feature/{branchname} to delete the old feature branch.

Hotfixes and Support branches

It's basically the same process but use the word hotfix or support instead of feature. git flow knows what to do. Just keep in mind that any changes are going to happen to your fork, and not the upstream repo. If you need to merge a hotfix into upstream master you may only do it va a reviewed pull request.

Releasing to production

  1. git flow release start {tag.number} (using semantic versioning)
  2. commit any changes to version info in package.json then git flow release publish {tag.number}
  3. git flow release finish {tag.number} merges the release into master of your fork, tags it, merges that back into develop on your fork and removes the release branch.
  4. Now go back to GitHub and raise a Pull Request to merge the upstream master from your fork's master branch. When that goes through you are done.
  5. In your command-line go back and clean up any outstanding branches and git pull upstream your local master and develop branches to ensure everything on your local machine is up to date with everyone's changes.

Note you will never push changes directly to the upstream project, only to your own fork.

Changes may only be introduced into the upstream project via a properly reviewed pull request.

Naming things

There are various systems, including GitHub itself, which will pick up the issue numbers from commit messages and pull requests and automatically associate them with the issues. It is therefore desirable to use a formal naming scheme for features, commit messages and pull requests.

Features

Features must be named per the following pattern #{issue number}/{some_descriptive-text} — so for example, if you are working on issue ABC-1 with the title "do the thing", call your feature ABC-1/do_the-thing. Obviously use your common sense to avoid making the feature names too long.

Note this will creating a feature via git flow will create a branch called feature/{issue number}/{some_descriptive-text}.

Commit Messages

When commiting something use the -m flag to add a short commit message of the format {issue number} summary of what you changed. So for example if you are working on issue ABC-1 and you added a method to the aardvark_controller you might use the following commit message "ABC-1 added anteater method to aardvark controller"

Commit messages ought to be in the past tense.

In general try to group file changes wherever appropriate, so if your controller change also involved updating something in a helper file, the one commit message can happily encompas the changes to both files. The message ought to reflect the main aim of the change.

Pull Requests

Pull requests must be named as follows [issue type, issue number] high level description of change. The following Issue Types are recognised

  • Bug Fix - the change fixes a bug
  • Feature - the change adds a new feature (the usual issue type)
  • Documentation — The change is a documentation only change
  • Optimisation - The change is an optimisation of the code base without any functional changes

If your change does not fit any of these categories, use Feature. Likewise if your change is not tied to an issue number you may use n/a instead.

So to use the above example your Pull Request would be named [Feature, ABC-1] added anteater to aardvark

You built it, you merge it

A developer must be responsible for their own work, from accepting a task through to merging to production. With that in mind if you review another developer's PR, please don't then merge it yourself. As a general rule you must let the developer merge her own PRs.

Likewise, don't expect someone else to merge your PR. Unless you do not have write permission on a project, you will always aim to take personal responsibility for the quality of the code that gets merged in.

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