It is important to note that these are not meant to serve as an overall style guide: in particular, some of the choices in structure are solely made for the sake of teaching, and are not choices that necessarily should be replicated in code outside of this course.
About: Style Conventions
This course follows a strict style guide in order to make sure the example code is consistent and easy to follow. This is intended to help guide students without potentially distracting changes in form.
This style guide describes the conventions used in this course, along with the reasoning on why any contentious choices were made.
Naming
- Variables should be camelCased.
let myValue
- Functions should be camelCased.
function helloWorld() {
- Namespaces should be all lowercase, and generally a single word.
namespace game {
- Descriptive variable names are highly preferred, even for simple functions.
Coming up with a descriptive name for each variable gives students time to reflect on their code,
and identify what exactly they are intending to do with each variable.
- Exception: variables with short names due to convention or accuracy (e.g.:
i
j
andk
for||loops:for||
loops, orx
andy
) do not need to be extended, as the short name is descriptive enough on it’s own
- Exception: variables with short names due to convention or accuracy (e.g.:
Spacing
- Four spaces are used per indent.
- Single Indents are used to indicate a new ‘block’ of code,
generally surrounded by curly braces
{}
. - Double Indents are used to indicate a line that is continuing from the previous line.
- Exception: template strings will be indented as needed.
- Operations should be surrounded by whitespace.
7 * 6
- Parentheses should be surrounded by whitespace on the outside, but not inside.
if (true) {
Syntax
- Semicolons are used to end each statement. This is meant to give a strong indication of where a line of code begins (the beginning of a line), and where it ends (at the semicolon).
- ‘Normal’ Functions are strongly preferred over arrow functions.
This includes anonymous functions.
function () {}
is used over() => {}
- Anonymous functions are preferred over named functions when being passed as parameters, except in the case of redundancy.
Form
- Single line conditionals and loops are not covered and avoided. Especially for new programmers, omitting semicolons and using the single line form can often lead to confusing and hard to diagnose bugs.
- Namespaces are used in a non-standard way on occasion. Generally, namespaces should avoid having code that runs on it’s own, as this can lead to code where it’s non-trivial to identify what is running, and in what order.
- Types are used for almost every variable declaration.
This is done for consistency, and to make sure students are away of the types of variables,
and don’t need to try to infer the type.
- Exception: loop variables in a for loop are commonly left as inferred types.
Comments
- Single line comments are used to indicate the behavior of potentially confusing code. Note that what might be confusing differs from person to person, so this is likely to be overdone in this course for non-trivial code snippets to cover a wide variety of users.
- Comments for functions follow the format described in the section on Function Comments. These are generally similar to JavaDoc and JSDoc style comments.
Other
- As this course does not cover object oriented programming (beyond what is necessary to describe creating objects), instance methods are described as functions in much of the course.