Search Unity

Do not use "var" in project which supposed to be a project for showcase and learning

Discussion in 'FPS.Sample Game' started by Artaani, Oct 27, 2018.

  1. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    410
    There is "var" everywhere in the code of this FPS Sample.
    Maybe "var" is easy to write, but the fact is - it is a pure evil if you want to read a code.
    "var" it is "write and forget", which is not suitable for projects for learning.

    Just look at any official Unity tutorial
    For example this
    "var" is never used in any tutorial.

    So if this FPS sample designed for learning, I recommend to replace all "var" with proper variable type and stop using "var" in the future development of this project.
     
    Romenics likes this.
  2. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,464
    The FPS Sample tutorial is for fairly advanced learners who can deal with all those "var". :)
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    14,540
    I just finished skimming through a code file picked at random (linked below) and I didn't see any situation where they used it in a way that isn't immediately obvious what the type for the variable is. Furthermore every situation the variables were used in were temporary. Any and every long term variable has its type specified.

    I'm not a fan of using "var" but I don't see any problems with the way it's being used.

    https://github.com/Unity-Technologi...Assets/Scripts/ECS/BaseComponentDataSystem.cs

    All of the tutorials at the links you provided are intended to be used by beginners. This project is not a beginner project by any stretch of the imagination and it's not a tutorial either. I wish they hadn't added it to the Getting Started section of the forum because it's simply too difficult a topic for the people normally asking for help in that section.
     
    Last edited: Oct 27, 2018
    TheMessyCoder and antoripa like this.
  4. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    410
    No matter how advanced developer is. "var" makes code harder to read for everyone. This is a fact.
    What a reason to make a learning process more complex?

    There is a reasons why "var" never used in official Unity tutorials on the link above. It is sad to see that Unity started to violate its own rules. A rules which makes the Unity great, such as "easy to learn".

    Another sad fact, it is how many people thinking that it is absolutely normal.
    If someone thinking that I am a hater - this is not correct. On the contrary, I am love Unity, this is why I am worrying about it.
     
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,464
    I remember another discussion about var where people said about the same things and I remember posting that image that can be found on the documentation page, scripting section:


    It seems that the use of var is very commonly accepted by professionals and the FPS Sample project is aimed at people who are experienced; it's not a tutorial like @Ryiah said.
     
    Ryiah likes this.
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    14,540
    No, this is just your opinion. Just do a search for "C# var readability" and you'll come across many arguments both for and against it including from individuals on these forums like @AndersMalmgren. It's very much subjective and dependent on the way you learned C# and your experience with it as to whether it improves or hinders readability.

    Yes, there are reasons why it was never used in official tutorials, but said reasons might not be for the reasons you seem to believe. One very easy reason is that Unity hasn't always supported the keyword. I don't remember exactly when supported was added, but it was definitely added sometime after I started using Unity.

    Additionally, like I mentioned earlier, it's use is very much subjective. Some people love it and some people hate it. Just because the person who wrote the tutorials didn't use it doesn't mean that the company had a policy against its use. If anything we know they don't have a policy against it thanks to the picture linked by @AnneSchmidt.

    Finally, the "var" keyword has quirks in its usage that can be confusing to a beginner and thus leaving it out of beginner tutorials makes sense. Leaving it out of more advanced material doesn't necessarily make sense. If someone can't understand how to use it by that point they definitely are way over their head with this sample project.
     
    Last edited: Oct 27, 2018
  7. drhodor

    drhodor

    Joined:
    Aug 18, 2013
    Posts:
    20
    Anything the devs want to do that saves them time so they can spend more of it on pure gold like this sample project is fine by me.
     
  8. petera_unity

    petera_unity

    Unity Technologies

    Joined:
    May 23, 2016
    Posts:
    39
    This is a really good point. We moved the sticky thread now. Hope it does not cause too much confusion but I think we were actively misleading people about the nature of the project with the current location. So better get it over with :)

    - peter
     
    drhodor, Enzi, TheMessyCoder and 2 others like this.