Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Is it good to use variables for calculations?

Discussion in 'Scripting' started by Barty200, Jul 16, 2021.

  1. Barty200

    Barty200

    Joined:
    Jul 13, 2021
    Posts:
    1
    So I'm just learning Unity and I was wondering which of these 2 are the preferred way of doing calculations.

    float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
    Vector2 Position= new Vector2(mousePosInUnits, transform.position.y);
    transform.position = Position;

    or

    transform.position = new Vector2(Input.mousePosition.x / Screen.width * screenWidthInUnits, transform.position.y);

    Does the top method take up more memory and reduce performance or are they both the same in terms of memory usage and performance?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I prefer the top method.
    First of all, compilers are kinda smart. If the top method would take more memory or would be slower, they would optimize them to the bottom version.
    The top version is self documenting code. For the bottom version, you would maybe need comments, especially if you would have more complex code there. On the other hand the top one with good variable naming would be its own comment.
    Although the second line is a bit redundant there.
    But the best method is usually to use a built in that's already well known, like https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
     
    Bunny83, Barty200 and mopthrow like this.
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    ZO5KmUG6R and Barty200 like this.
  4. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    274
    Struct variables beyond some memory limit (float, Vector3, Quaternion, Color etc.) are always allocated on stack, i.e it's very very fast and there is no garbage generated. You should never worry about that.

    With such calculations, write them the way they easier for debug (more variables, more lines), write clean and nice looking code, readable is always less prone to bugs and easier to understand.
     
    Barty200 and Yoreki like this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    There's sort of a misconception people have about local intermediate variables. People think they take up extra memory or something and don't want to use them. This is simply not true at all. Whether you create a variable or not makes no difference at all to the amount of memory the computer uses to accomplish the task.

    All of the intermediate calculations need to be done and stored somewhere no matter what. Sometimes they get stored in a CPU register (super fast) and sometimes in memory (not as fast). But whether it goes into memory or in a register has nothing to do with whether you've created a named variable for the calculation result or not. Anyone who has spent any time writing assembly code of any kind can tell you this (MIPS, ARM, x86, IL, Java Bytecode, etc).

    All intermediate variables do is increase the readability, maintainability, and debug-ability of your code.
     
    Last edited: Jul 16, 2021
    Lekret, mopthrow and Barty200 like this.
  6. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    If you are worried about performance this soon, you will never finish your game. The time to worry about performance is when you experience performance issues. This type thing will never create a performance issue.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Splitting complex lines up into multiple lines has benefits for code readability and debugging.
     
    Barty200 and PraetorBlue like this.