Code Style & Standards in Unity

You Maverick
public void SayHello()
public void HelloSay()

Unity you Maverick

The underscore

private int count = 0;
private int _count = 0;
public void SayHelloManyTime(int count)
// the next line doesn't use the param
if (this.count > 10)...
private int count = 0;
public void SayHelloManyTime(int count)
// what variable will the next line use??? (it's the param)
if (count > 10)...
private int _count = 0;
public void SayHelloManyTime(int count)
if (_count > 10)...

Casing in Unity

this.transform.position
this.CompareTag("Player");
// oh dear....
private Transform transform;
public Transform transform {get;]

Unity’s inheritance

var gameobject = this.transform.gameObject;
var gameObject2 = this.gameObject;
Definition of this.transform.gameObject
Definition of this.gameObject

Why are standards important?

  • Easier to learn the subject — if you adopt the same style and standards as the majority of Unity developers you will be able to read their code, and they yours
  • Team work — when you work in a team you want people to be able to consume your code as easily as possible, if everyone is using the same style & standards then this is much easier. “Have a got a verb-action method for this, let me type “Get…ah there it is…”
  • Maintenance — having a consistent style & standards makes it a lot easier to change code
  • Legal — probably not one that springs to mind for most Unity developers but when you deliver code you do have a responsibility for the quality of your work. This is important if you sell your work, especially in an corporate/enterprise arena. One way to demonstrate that your care about the quality of your work is by adhering to a working set of style & standards. This is really a nice side-effect, hopefully you should already be convinced without needing this point.

Other standards?

  • Project Structure — how you structure your project is important for all the same reasons. If someone wants to edit a script then they should know where it is likely to be. Unfortunately there does not seem to be a standard for this, so it is up to you and your team to publish and evolve these for yourself
  • Scene Hierarchy — Similar to Project Structure but this about how you structure the objects in the scene hierarchy and how to name them. Tracking items with poor names can make your life very difficult.
No style or standards
Named containers and items

Summary

My coding style recommendations & standards

private int _underscoreCamelCasePrivateFields;
private float PascalCaseProperties => _camelCaseField;
private PascalCaseMethodsOrFunctions() {}
public async Task SuffixAsyncMethodsWithAsync() {}
public void DoNotUseJavascriptBlocks()
{
if (_thisCodeIsJavaScript) {
}

if (_thisCodeIsCSharp)
{
}
}
// never omit a code block
if (_thisCodeIsNaughty) DoSomethingBad();
if (_thisCodeIsGood)
{
DoSomethingNice();
}
// Do Not Use bools For State Flags, an enum is a flag
bool isStateOff = false;
bool isStateOn = false;
bool isDimmed = false;
[Flags]
private enum State
{
Off = 0,
On = 1,
Dimmed = 2
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store