Search Unity

Best Code Practices (help)

Discussion in 'Scripting' started by MadeThisName, Sep 25, 2015.

  1. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    If a Vector3 is called in multiple functions of the same class is better for performance to keep it outside the scope of the functions and have one instance rather than multiple per function? If so would it be better to have a separate class with a static function?

    Example would be this vector3 that is used in multiple functions.
    Code (CSharp):
    1. Vector3 playerPosition = new Vector3 (player.position.x, player.position.y + 0.65f, player.position.z) - muzzlePosition;
    If you happen to know of any good links with information in regards to best practices with this in mind i would love to read them. Thankyou Guys.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I wouldn't worry about the performance. Vector3 is a struct, and structs get a bit weird with instances anyway.

    What I would worry about is maintainatilibty and development speed. If the particular spot 0.65 units above the player object is important, then you want to move it out of the function and into its own member. That way when your play testers come back and say it should actually be 0.5 units instead you can change it once.

    Google magic numbers for a more in depth discussion on the topic.
     
    Korno likes this.
  3. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Once again thank you for your knowledge BoredMormon.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Am I the only one who hates using the ctor to build a new vector based on another one?
    Code (csharp):
    1.  
    2. playerPosition = player.position + Vector3.up * 0.65f - muzzlePosition;
    3.  
     
    image28 likes this.
  5. solkar

    solkar

    Joined:
    Aug 15, 2012
    Posts:
    38
    No, you're not alone.
     
    KelsoMRK likes this.
  6. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    I am just curious, why do you hate this, and how would you do it?

    [e] Nevermind. I thought you were talking about
    Code (CSharp):
    1. playerPosition = player.position + Vector3.up * 0.65f - muzzlePosition;
    And I couldn't figure out what's wrong with it. Now I am realizing that your statement is about the use of New() in the initial post. :D Sorry.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    My brain would just rather do the math I guess. And it feels like a lot of redundant typing.

    And my post showed how I do it.
     
    Last edited: Sep 25, 2015
  8. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    I didn't know i could go about it that way KelsoMRK since i am self taught from examples and don't yet have the knowledge for best practice.

    How would you approach something like this so i know for future reference. Would this be a situation that a new vector should be used?
    Code (CSharp):
    1. playerPosition += new Vector3 (Random.Range (-maxSpread, maxSpread) * shotSpread, Random.Range (-maxSpread, maxSpread) * shotSpread, 1);
    Thankyou for the example :). Do you know of any good books or links in regards to best practices?
     
    Last edited: Sep 25, 2015
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'm not sure what you mean when you say "Best Practices" as your original question was more geared to performance and my post was about personal preference when doing a particular operation. Best Practices fall more into the category of "here's how you name your variables", "how to organize your namespaces", "how to write a for loop", "when to use var and when to explicitly declare the type", etc etc.

    To that end, MSDN does have some articles but admits that there is no hard-and-fast "Correct" way to do something. Not to mention the fact that Best Practices vary from company to company (and even within a company if it's large enough). What you'll find is that you'll develop a way of writing code that works for you - just like you develop a manner of speaking; or writing; or taking a shower in the morning (or evening!). If the end product performs at the same level of efficiency and is clear when you read it again in 3 years then you're well on your way to writing good code (whatever that means).

    I'm completely self-taught too btw :)
     
    Kiwasi, MadeThisName and sluice like this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For readability on complex vectors I often do this

    Code (CSharp):
    1. Float x = somethingCrazy;
    2. Float y = somethingElse;
    3. Vector2 vector = new Vector2 (x,y);
     
    MadeThisName likes this.