Sunday, 15 December 2013

Unity Code Naming Conventions

I did a quick bit of searching to find out what naming conventions were used.  It turns out that there isn't really one.

Unity supports three programming languages.  I'll ignore Boo because I know nothing about it and there are few samples.   Most of Unity's older code base is in UnityScript.  That has a very similar syntax to JavaScript.  In the UnityScript code base the only convention is that class and function names are in PascalCase and variables are in camelCase starting with a lowercase letter.

C# appears to becoming very popular with Unity developers, if not the dominant language but as the syntax is similar most people can mix and match if needed.

As C# is a Microsoft language many people have adopted the Microsoft guidelines for the use of case when  working in Unity.  I also prefer those naming conventions for case, they are:
PascalCase for public and protected properties and fields and camelCase for private properties and fields with PascalCase for all methods independent of if they are private, public or static etc.

The case is only significant for public or protected elements so that non-case sensitive languages can always reliably access them:

public Method(parameter)
protected Method(parameter)
private Method(parameter)
public Property
protected Property
private property
public Field
protected Field
private field

Some people like to use an underscore in front of _camelCase for private fields but that is their personal preference and appears to go against the recommendations in some of Microsoft's guidelines.  Again, that is only significant for public fields, what you use privately is just for readability of the code.

No comments: