GPR103 Coding Conventions
Why A Style Guide?
While there are rules for naming identifiers (i.e., variable, function, class names)
within C#, such as starting with a letter or underscore, containing only
letters/numbers/underscores and no spaces, this leaves programmers with enormous
flexibility. It is the same for the space between and around identifiers, operators and
other elements of our code. (continues).
Teams have often found that adding a few additional agreed rules and conventions
help their coders to work together better and faster. Since they are not universal or
built into the language, there is no true right or wrong convention; the benefit comes
from consistency. A programmer either infers the conventions from existing code
from coworkers, lecturers and authors or follows a style guide/reference.
This document is a reference for GPR103 2D Game Programming.
Capitalisation/underscores:
Identifiers in C# cannot contain spaces, but your variable, function and class names are
often multi-word, for example, bool isintact; . Humans struggle to read lower-case
words that run together, but despite having tools to create word separation in C#
identifiers - capitalisation (identifiers are case-sensitive) and underscores -
programmers have many solutions.
Expand this section to learn some of the most common that are used in GPR103
2D Game Programming.
camelCase
Use camelCase for variable names, with the first letter of the first word in
lowercase and the first letter of subsequent words in uppercase, such as
myVariable
PascalCase
GPR103 Coding Conventions 1
Use PascalCase for class names, with the first letter of each word in
uppercase, for example, MyClass .
Use PascalCase for method names, with the first letter of each word in
uppercase, for example, MyMethod
Indentation/White space:
Use indentation to improve readability and show the structure of your code.
Example 1:
if (condition) {
// Indent the code inside the block
// to show that it is part of the if statement
// ...
}
Use blank lines to separate sections of code and improve readability.
Example 2:
// Use a blank line before a method definition
public void MyMethod() {
// Method body goes here
}
// Use a blank line between different logical sections of code
public void MyMethod() {
// Do some work
// ...
// Do some more work
// ...
}
Name Variables for What They Represent
New programmers will often choose names for variables based on how they were
acquired or created rather than what they represent and what they mean to the task.
GPR103 Coding Conventions 2
When you read a variable name, you want to know what it is for and why you need to
store it.
💡 DO NOT heavily abbreviate variable names or use 1 or 2-letter names.
Exceptions:
1. Established labels/names/letters that would definitely be recognised by all
programmers on the project.
a. Common units of measure: kg and lb (weight), kph and mph (speed).
b. Domain knowledge: For physics, e , m and c for energy, mass, and
speed of light.
2. Essentially universal coding conventions used for iterators in loops like i ,
j, k
3. Networking, tx and rx - although you can probably see this is already
getting iffy.
4. You want out of your job but can’t quit because your parents will get mad.
Unclear:
int number = 0;
// What am I using this for?
int random1 = Random.Range(80,100)
int random2 = Random.Range(10,30)
// okay.. what should I do with those?
Better:
int playerHealth;
// It is clear what this variable is used for
int player_grenaded_damage = Random.Range(80,100)
int grenade_splash_damage = Random.Range(10,30)
// Oh so that's what I'm creating random variables for
GPR103 Coding Conventions 3
Functions Have Doing Names
Variables store data. Functions do things, so you should name them that way (and
make the argument names clear!).
Unclear:
void names(string id)
WinMultiplier win(int num1, int num2, int num3)
Better:
void displayStudentNames(string course)
Prize checkForWinningHand(int card1, int card2, int card3)
GPR103 Coding Conventions 4