- Home /
Properties names, same as class okay or bad?
Very quick question on best practices for using property names that match the class they are caching, for example:
private MyClassName _myClassName = null;
// Is using the same exact name for the property a bad idea?
public MyClassName MyClassName
{
get
{
if( _myClassName == null ) _myClassName = GetComponent<MyClassName>();
return _myClassName;
}
Thanks for any feedback on the matter guys!
Hmm, can be confusing if you ask me. In the case of caching, like you are doing here, it makes sense, but the Unity (not the .NET) convention is the use a lower case first letter for things that are data attributes.
But it doesn't have any side effect that I am unaware of, does it? Other then the fact that it can be confusing, it really doesn't matter what I name my properties?
Thanks!
Just for legibility sake it's worth sticking to a na$$anonymous$$g convention. You can't write a function called $$anonymous$$yClassName but you can make a property, so there will be no side effect.
// Is using the same exact name for the property a bad idea?
Well, I think that is not a good idea! You have to extend you class with another.
I mean why you use: "public class NameOfTheScript : $$anonymous$$onobehavior"?
Because you need to extends your class by another class (in this case a class pre-created by Unity), so you don't have to develop what Unity has did before.
So the good use is to extend by another, selft extend as not sense.
whydoidoit, I just have a hard time sometimes co$$anonymous$$g up with names for my properties, so maybe I should adopt the Unity na$$anonymous$$g convention with Properties and start them with a lowercase ins$$anonymous$$d of the .NET Uppercase convention...but I do like the Uppercase convention because then i know right away that I am accessing a property and not just a public variable. Argh I can't decide lol :)
Steve, what??? That wasn't my question at all...
Answer by Demigiant · Nov 18, 2012 at 08:45 PM
Simply put: bad, for various reasons.
1) It's badly legible.
2) It's wrong, conceptually. A property references an instance of a class, not the class itself. If you have a property that references a class called "Book", for example, its name should also help understand what type of "Book" are you referencing (like "philosophyBook" instead than simply "book").
3) You could encounter naming conflicts, especially if you change your naming style. For example, maybe one day you'll want to use a more "official" naming style (where public properties start with an uppercase), and thus you'll have a property that has the same exact name of a class, which is not allowed.
Thanks Stephane! :) (uh, this is $$anonymous$$e, but my nick appears different when commenting, for some unknown reasons)
yeah I have noticed the name differences on many comments, weird... :)