Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ronronmx · Nov 15, 2012 at 07:44 AM · propertiessettersgetters

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!

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Nov 15, 2012 at 08:01 AM 0
Share

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.

avatar image ronronmx · Nov 18, 2012 at 07:29 PM 0
Share

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!

avatar image whydoidoit · Nov 18, 2012 at 07:33 PM 0
Share

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.

avatar image Steve Steven · Nov 18, 2012 at 08:19 PM 0
Share

// 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.

avatar image ronronmx · Nov 21, 2012 at 10:12 PM 0
Share

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...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · Nov 21, 2012 at 10:07 PM 0
Share

Good points, thanks $$anonymous$$e!

avatar image Demigiant · Nov 22, 2012 at 08:45 AM 0
Share

Thanks Stephane! :) (uh, this is $$anonymous$$e, but my nick appears different when commenting, for some unknown reasons)

avatar image ronronmx · Nov 22, 2012 at 08:18 PM 0
Share

yeah I have noticed the name differences on many comments, weird... :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# generics and getters/setters with Unity iPhone 1 Answer

Accessing class properties through getters & setters in editor mode 1 Answer

Proper use of c# properties? 1 Answer

Getters issue... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges