Skip to content

The Field Guide's Guide to Style

Code style can be subjective, divisive, and conterversial — but it is important. Each software project has its own style guide, and while style guides will change between different projects or organisations, what’s key is that your code is consistent within a project. This helps others become familiar with a cohesive codebase, and ensures teams are on the same page.

This document will highlight, with examples and reasons, our own style guide which we will follow in our code. If you chose not to follow parts of this guide that’s okay — but make sure you are consistent within your own project.


Braces ({ and })

Place braces on the line following the declaration.

Comments

Comments are a great way to explain what your code is doing, or why it is doing it. However, comments can also be a distraction, and can become outdated or redundant. Only comment code when necessary or where the code may be unclear to the reader. Don’t overcomment your code in unecessary places, which can clutter the codebase, or become outdated or redundant. Whether or not a comment is necessary is subjective.

Naming Identifiers

Naming your identifiers is one of the most important things you can do to make your code readable. As the famous quote goes, “There are only two hard things in Computer Science: cache invalidation and naming things.” We suggest you use descriptive names for your identifiers, and avoid abbreviations where possible.

Constants

Any variable which could be made a constant, should be. Constants should be named according to the style guide of the language.

Global Variables

Global variables should not be used.

Indentation

Between any new pair of brackes ({, }), write your code 4 further spaces in from the left.

Statements

You should only have one statement per line.

Be careful of these language features

  • break: can make it difficult to track the flow of a program, and can lead to bugs. There may be occasions where break statements are necessary, but they should be avoided where possible.
  • continue: can make it difficult to track the flow of a program, and can lead to bugs. There may be occasions where continue statements are necessary, but they should be avoided where possible.

Be aware of these common mistakes

  • switch statements without a default case.
  • switch statements without a break statement.

Avoid these language features

The following low-level control flow features should be avoided, as they can lead to bugs and make code difficult to read.

  • goto: can make it difficult to track the flow of a program, and can lead to bugs. Never use it.