Unity — C# Coding Standards

Samuel Asher Rivello
4 min readJul 11, 2020

Unity3D is a powerful suite of tools (Project IDE, Code IDE, and run-time) for game development.

Unity supports several languages, but the community consensus is to use only C#. Having good coding standards and an established project structure is beneficial to your project and your team.

See my recommended Unity Project Structure article on Medium.

Microsoft created C# and published recommended coding standards. I created my own standards with many differences. While Microsoft's standards are not created with Unity in mind, mine are.

I am a Unity Game & Tools Developer with over 20 years of game development experience. I am available for hire (Remote, Contract).

SamuelAsherRivello.com

Pros of Coding Standards

Regardless of which convention you choose to use for Unity or other platforms, having some sort of convention is considered a good idea by the majority of the development community. A hallmark of Getting Things Done efficiently is to automate what can be automated. Once a team has standards and adopts them (both admittedly time consuming) the time improvements in the daily workflow are notable. There is less discussion on why or how to name things. It just gets done.

  • Consistency — The codebase is consistent in presentation regardless of team location, spoken language, or individual programmers.
  • Integration — The code, the review of the code, and your team are more tightly integrated. Theoretically, the code is more interoperable (copy/paste) and mutually intelligible.
  • Maintenance — Conventions aid readability. Readability helps new and existing programmers revisit the code base for fixes and improvements.
  • Communication — Developers more implicitly understand each other in written and verbal communication.
  • Ownership — More consistency in coding standards across a code-base encourages Collective Ownership, the feeling of respect, care, and reverence developers for their own work and for those who contribute thoughtfully to their own work.

To be fair, there are drawbacks too. The additional time required per team to develop standards, implement standards while coding each day, police such standards is considerable. You may find certain rebellious personalities and certain cultures which value independence may not take to the idea of standardization well. This may limit the hiring options and lengthen the onboarding process. What are the other drawbacks?

Cons of Coding Standards

Few developers argue the benefits of coding conventions, yet many argue if the time and discipline required is worth it. Some developers simply refuse to add ‘cosmetics’ to their code. In their eyes, if it functions well, the code is good enough.

More contentious yet — what are the criteria of a good standard? A few criteria most people can agree on are here.

  • Consistency — Whatever rules there are should be applied evenly and applied everywhere.
  • Pragmatism — The convention should be concerned with ‘important’ things and nothing else.

Naming Conventions

  • Naming/Coding Conventions — This relates to how a developer chooses to name and capitalize the custom keywords he uses. Generally, each platform/language has a recommended format but NOT a required format. Some IDE’s offer compiler warnings (not errors) which encourage the adoption of its recommended format. That is the focus of this post.
  • Style Conventions — This relates to how a developer chooses to indent, comment, and add whitespace to his or her programming. Generally, each platform/language has a recommended format but NOT a required format. Some IDE’s offer an ‘autoformat’ which will convert your file to its recommended format. That is NOT the focus of this post.
  • Lower Camel Case Naming convention where the first letter of a name is not capitalized and each element of the name begins with a capital letter. e.g. iPhoneCase.
  • Upper Camel Case (aka Pascal Case) — Naming convention where the first letter of a name is capitalized and each element of the name begins with a capital letter. e.g. PowerPoint.

Coding Templates

namespace RMC.Templates
{
// Namespace Properties ------------------------------
// Class Attributes ---------------------------------- /// <summary>
/// Replace with comments...
/// </summary>
public class TemplateComponent : MonoBehaviour
{
// Events ---------------------------------------
// Properties -----------------------------------
public string SamplePublicText
{
get { return _samplePublicText; }
set { _samplePublicText = value; }
}
// Fields ---------------------------------------
private string _samplePublicText;
// Unity Methods ------------------------------
protected void Start ()
{

}
// Other Methods --------------------------------
public string SamplePublicMethod (string message)
{
return message;
}
// Event Handlers -------------------------------
private void Target_OnCompleted(string message)
{
}
}
}

Downloads

Complete source code and examples are provided for you. Enjoy!

References

More By Samuel Asher Rivello

Available For Unity Hire: Remote, Contract

20 years of game development experience. SamuelAsherRivello.com

Comments?

Let me know! Twitter.com/srivello

--

--

Samuel Asher Rivello

Web3 Game Dev & Instructor - Unity Certified. 20+ years of game dev XP. Available: Remote, Contract. http://www.SamuelAsherRivello.com