Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gillissie · Jun 29, 2011 at 04:26 PM · classclassesproperties

class design preference

If you were designing a class that could be used for a few different types of objects, and not every property was used for every object type how would you do it between the two versions here?

Mainly, sometimes an object will always only have one "transition" value, and sometimes it will have more than one. Would you use an array even for object that only need one value, or would you use a single property for those and an array for objects that need more than one value?

class myClass
{
    public string transition;     // Use for objects that only need one value.
    public string[] transitions;  // Ignore for objects that only need one value.
    public int otherProperty;
    ...etc.
}
or
{
    public string[] transitions;  // Use when one or more values are needed.
    public int otherProperty;
    ...etc.
}
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by testure · Jun 29, 2011 at 05:17 PM

Personally, I just define it as an array. it's just cleaner that way. If you're juggling two variables, you're always going to end up with a check to see if transitions is null or empty before falling back to the singular variable- so it just seems like a lot of extra wasted work that makes things overly convoluted.

To me it's much easier to just initialize an array with one index and have all of my logic code remain the same.. less margin for error and it's easier to read.

Comment
Add comment · Show 2 · 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 Gillissie · Jun 29, 2011 at 06:45 PM 0
Share

Thanks for the answer. I would like to hear from several more people too, whether you agree or disagree with testure's answer.

avatar image _Petroz · Jun 29, 2011 at 08:54 PM 0
Share

Testure is right, having variables which may or may not be relevant is bad design. Every variable in scope should mean something, polymorphism via 'if' statements is an anti-pattern to be avoided.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to list all the properties and methods from the class? 1 Answer

Editting a custom javascript class in the inspector 1 Answer

Access class variables of selected object 1 Answer

Unity C# Start function without extending Mono 2 Answers

storing scripts that implements an interface 0 Answers


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