Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good naming convention for Unity? [closed]

I'm pretty much a noob with Unity. As a c++ programmer, the naming conventions in Unity bothers me a little. And having OCD ontop of that makes me go crazy ;)

The objects has say a property Transform which again has a property Position. But these properties must be accessed by writing transform.position in the code using lower case. This is not very intuitive to me. So I wonder how I can look at it in order to more easily avoid complications. And what conventions I should use to be able to tell everything appart by taking a quick look at the variables.

like image 568
Ledii Avatar asked Jul 05 '14 12:07

Ledii


People also ask

What should be the naming convention?

What Does Naming Convention Mean? Naming conventions are general rules applied when creating text scripts for software programming. They have many different purposes, such as adding clarity and uniformity to scripts, readability for third-party applications, and functionality in certain languages and applications.

What convention is used in naming classes?

Class names: concatenated words each starting with upper case. Objects: lower case separated by underscores. Ivars: lower case separated by underscores.


1 Answers

The Unity convention is actually rather simple: everything is Pascal-cased, types (classes, structs, enums) and methods start with upper-case, fields and properties with lower-case. Enum values in upper-case, constants in lower-case (usually). So ClassName, MethodName, myField, myProperty { get; set; }, MyEnum.CaseA... that's it.

As for your example, Transform is a class, whereas transform is an accessor to the instance of Transform in that particular GameObject/Component. Also, Transform doesn't have a Position property, it has a position property (always lower-case).

This is more or less based on C#'s conventions and the standard .NET library (MS has very precise guidelines about it), except standard .NET uses UpperCase for public/protected methods AND properties, and lower-case for private (again, usually; what's private is more left to the taste of the coder I think).

As a side-note, with any codebase, in any language, the best way is ALWAYS to follow the existing convention. Every seasoned programmer will tell you this. I understand about OCD, believe me, but in this case I suggest you let it go. There are very little objective arguments as to why a convention would be better than another (by definition a convention is arbitrary), and even if there was, the absolute worse thing you can do is mix several conventions, because then you have 0 convention at all and never know what to expect.

At least C# tries to standardize; I've worked on several C++ codebases and I fail to see a common denominator: UpperCaseClassNames, lowerCaseClassNames, underscore_separated, tClassName, ENUMS_IN_UPPER, or not... it's rarely consistent, so the less you mix the better.

like image 152
benblo Avatar answered Sep 23 '22 15:09

benblo