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!
Simply put: bad, for various reasons.
-
It’s badly legible.
-
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”).
-
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.