I’ve been programming in C# for 3 years and I’ve seen it done both ways. Normally I would say it’s just a matter of opinion. But MonoBehaviour throws mud in the water by exposing lots of public fields in camelCase. So I’m wondering if one convention is better than the other for Unity.
Consider the following 2 code samples:
_convention = "Underscore prefix "; // Assign to a private field
string myText = _codingConvention + name; // name looks like a local variable
_transform = transform; // No name collision when caching the transform
this.convention = "This dot prefix"; // Assign to a private field
string myText = this.codingConvention + this.name; // Longer but more clear
this.cachedTransform = this.transform; // To avoid a name collision
Thanks @Glader . [SerializeField] is great for controlling what shows up in the inspector (and what gets serialized). We could probably do a whole thread on the virtues of hiding public fields, and why Unity decided not to follow that advice.
But we still have to deal with public fields like transform, gameObject, name, renderer, etc whenever we extend MonoBehaviour. Check my original post and you’ll see I’m actually talking about the variable naming convention. Do you use this.myVariable or _myVariable as a general rule?
Would you care to elaborate?
I am in the process of porting some code to Unity 5 and I’d like to know what you’re referring to. I cache various components that are used often. Partly because it was advised in Unity 4 and partly because I don’t like writing ‘x.GetComponent().blah’ every damn time.
OnTopic:
I prefer the this-prefix. What’s far more important than which prefix you use, is making sure that you always use the same => Consistency.
I’m not so sure this is just a matter of preference as a variable with a preceding underscore does not necessarily signify that it is a private field, whereas an object referenced with “this” keyword will always be… if that makes any sense.
I used to use underscores and this.whatever in Flash AS 2 & 3. I don’t in Unity. I Just CamelCase with a lowerCase letter starting in Unity. i like my vars public so i can watch them all. This is my form of debugging to some degree…for watching things that do not throw errors but i need to/want to/like to track without the console spamming per frame.
I have used both ways in Unity, and right now I prefer using ‘this’. Underscore is a nice way to do things when your entire code base makes all public members PascalCase. But with unity throwing in camelCase, I think the ‘this’ prefix is more clear. The only caveat I’ve seen so far is you can’t have any private members that mirror public MonoBehaviour properties with the same name, like in the _transform vs. cachedTransform example.
BTW: @hippocoder , I’m looking forward to Unity 5!
Well…it’s still faster to cache transform in Unity 5. It’s a little faster to use transform without caching in Unity 5 compared to Unity 4, but it’s not a huge difference. However, in the large majority of cases, it won’t actually make any difference either way anyway, unless you’re accessing transform really a lot (once a frame is not a lot).
Many of the camelCase inherited members in MonoBehaviour are going away in Unity 5, but it’s still true that in general across the API we use camelCase for our public properties.
Personally, I use _underscore because that’s what Resharper encourages me to do, and I believe it matches the style used by the .NET framework itself. But it’s not that big a deal; I could just as easily configure Resharper to enforce a different style (and should do, if I decided a different style were warranted).
Neither, underscores are ugly and typing “this.” is pretty pointless. I think camelCase makes the most sense since it looks better, and most variables SHOULD be private on any particular class IMO. Therefore would it not make more sense to distinguish public variable names, since they are against the norm…