Search Unity

Question Vote for coding standards

Discussion in 'Open Projects' started by Kamyker, Oct 1, 2020.

?

Pick max 2 preferred coding standards/naming conventions

  1. C# (uppercase public fields)

  2. C++ (lowercase public fields)

  3. NO prefix for private fields (playerHealth)

  4. "_" prefix for private fields (_playerHealth)

  5. "m_" prefix for private fields (m_playerHealth) (used in fps microgame)

  6. Whatever (aka complete mess)

Multiple votes are allowed.
Results are only viewable after voting.
  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Probably all votes from m_ would go to _ instead of no prefix. So it's 130:100 for prefixes.
     
  2. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    How they became variables? Not using CAPS doesn't make them static or something.
    Code (CSharp):
    1. public class Character
    2. {
    3.     private struct Consts
    4.     {
    5.         public const float GravityAmount = -9.81f;
    6.     }
    7. }
    Now you know that they're constant and inside this class only. It looks very clean, without distracting attention of caps.

    I don't want to type / look at Dictionary<GameObject, Component> two times in a row.

    upload_2020-10-8_12-51-35.png
    Seriously? :confused::confused:
    So many people using them just cause some YouTuber told them to <insert C++ reasons>.
    I've tried it myself and it was super ugly.
    We have
    this.
    for rare cases where local is named the same as member.

    I feel like these are bad practices from 90's era.
    But we're teaching good ones there, right?
    So we need to avoid questionable principles as much as possible, and stick to the most human-friendly standards of C#.

    ~In my honest opinion
     
    Last edited: Oct 8, 2020
  3. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Not gonna lie, you are pretty good at making stuff up. Other thread:
     
    JoNax97 likes this.
  4. MileyUnity

    MileyUnity

    Joined:
    Jan 3, 2020
    Posts:
    92
    Personally I prefer sing a _ prefix so that if I'm deep in the code I don't have to look at the signature of a field in order to know whether it's public or private, I can just see the _ in front of it and know that this is a private field.

    Also, why exactly would these be bad practices?
     
    adamgolden, kcastagnini and MUGIK like this.
  5. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Maybe try to convince Unity to add "Find References In Project" option (similar to current "Find References In Scene") to the engine. Something like https://github.com/ogxd/project-curator. Would help a lot with any SO workflow.
     
  6. MileyUnity

    MileyUnity

    Joined:
    Jan 3, 2020
    Posts:
    92
    If that were to be added it would only come in 2020.2 at the earliest so that wouldn't work for our project in 2019.4

    I do agree that that could help out, I'll forward it to the QoL team :)
     
    Neonage and Kamyker like this.
  7. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    Sorry if my harsh expression leads to misinterpretation.
    These are coming from my personal experience and reading of the thread.
    I'm trying to discuss actual problems here :)

    We've already said how in the posts above.
    It goes against some C# standards, as a result makes code much harder to read, and newcomers may think of these as something special in terms of compilation. (ex. Python coders with their __class__)
     
  8. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Still making stuff up? :)
    Microsoft:
    Google:
    .NET Runtime:
     
    MUGIK likes this.
  9. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    So it means "use what's best to solve your problem", right?

    As I see '_' can be helpful in monster classes, where it is actually hard to find members you need.
    But in that case, maybe you're doing something wrong and you better split your logic into multiple encapsulated classes :)

    That's why I feel prefixes would not fit in this project
     
  10. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    Yeah, I know. But they are not like 'really private':D
    I mean serialized fields are injected through Unity's serialization, so maybe this particular subject worst discussing.

    Personally, I use '_' for serialized fields too. But I don't have sureness about this.
    I think it should be explicitly declared in conventions.

    And I would vote for a separate folder for scenes. Because scenes will generate scene-specific folders for Lightmaps and NavMeshes. It's not cool to have such folders in the root of the project. Maybe you can move them to a different place after creation, but I've never tried this. And this is not convenient at all.
     
  11. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    And about monsters.
    Try to avoid the usage of Singletons. Explore usage of ScriptableObjects (1, 2) for a similar, centralised class that can be accessible from multiple objects.
    Something like that would be nice:
    Avoid structure of "Monster" classes. Learn about Single Responsibility in S.O.L.I.D. principles and more:

    Note: we use Inspector and ScriptableObjects for Dependency Injection principle.
     
    GhAyoub likes this.
  12. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Seems like it's possible in Quick Search. Version 2.0 is a bit wonky compared to 3.0 (available in 2020.1) but better than nothing:


    Ver 3.0:
    Consider adding that package.
     
    Neonage likes this.
  13. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    Good find! It looks a little nicer in 2.0 for me :)
    upload_2020-10-9_0-49-8.png
     
  14. deivsky

    deivsky

    Joined:
    Nov 3, 2019
    Posts:
    87
    I'm sad about the prefix on privates, the var ban and the caps on constants, however there's been plenty of discussion around those topics already.

    Now, I'd like to raise a couple of other things:
    • First, my personal preference on properties:
      • Usually I only use public fields in structs, never in classes. I'd say, when exposing a field, use properties instead of the field itself, particularly when you only need to read the value from outside. eg:
        Code (CSharp):
        1.     public string Name => _name;
        2.     [SerializeField] private string _name;
      • PascalCase properties, even private ones. I used Rider a while ago and if I recall it suggests doing that by default, and it stuck with me when I switched back to VS. But anyway, My reason behind it is, I like using private properties mainly to improve readability on repeatedly accessed data from different members, or to easily get common values that don't really require dedicating a field to. Basically I use them as simpler versions of Get methods to reduce boilerplate, that's why I believe they should be differentiated from fields and PascalCase suits them better. eg:
        Code (CSharp):
        1. private Vector3 Velocity => _characterController.velocity;
        2. private bool IsGrounded => _characterController.isGrounded;
        3. private bool IsFalling => !IsGrounded && Velocity.y < 0f;
    • Second, any words about capitalization of protected fields? Some people (like myself) treat them as private, some as public, and some use a different prefix—so this should be in the document as well.
    • And finally, this is all syntactic sugar and I'm in favor of all of it, but I know some people aren't. It doesn't say anything in the document about them so I'm just wondering if we should get a consensus or leave it at the discretion of the programmer.
      • Ternary operator ? "yes" : "no";
      • Both Null?.Propagation() and Null ?? Coallescing (except with UnityEngine.Object (and no ??= cuz c# 8 when? :())
      • Expression-bodied member => expression;
      • No brackets on single line statements.
        Code (CSharp):
        1. for (int i = 0; i < length; i++)
        2.     for (int j = 0; j < length; j++)
        3.         if (isTrue)
        4.             WhoNeedsBrackets();
    Any thoughts?
     
    MUGIK and Neonage like this.
  15. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    Yes. This please.

    I'm trying to avoid private properties, as it's not always clear what they represent.
    For ex. if I make IsGrounded inside PlayerUI class, would it make any sense?

    If it's used by user then it's public :)

    I think we won't use them on UnityObjects. Any other sugar is cool for me :p
     
  16. deivsky

    deivsky

    Joined:
    Nov 3, 2019
    Posts:
    87
    That seems more like a naming problem. You could call it IsPlayerGrounded and I guess it'd make sense.

    What do you mean? If it's serialized it's PascalCase, if not it's _camelCase? That would be extremely odd imo. What does it have to do with the fact that it's protected?

    That's what I said ;)
     
  17. tmcdonald

    tmcdonald

    Joined:
    Mar 20, 2016
    Posts:
    160
    I'd only do it if it was basically a calculated private field or something. I support just avoiding them altogether to prevent confusion.

    EDIT: Also I'll go ahead and throw it here since this seems to be one of the few places where there is actual discussion, but I'm rather annoyed that there has not been any official comments in the Inventory System thread. I'm rather disinclined to participate in the project at this stage, which is a shame because I was very interested in it at the start.
     
    MUGIK and Neonage like this.
  18. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    If protected field is exposed to user from abstract class then it's camelCase. For properties - PascalCase
     
  19. shuttle127

    shuttle127

    Joined:
    Oct 1, 2020
    Posts:
    183
    Ciro made a revised story post in the narrative thread and asked that we finalize it by today so that the other systems can be worked on, which includes the inventory system. I assume that locking down the story means that either he or one of the other Unity people will make a post later today/over the weekend/early next week about the other systems, so only a little more time to wait.
     
    cirocontinisio likes this.
  20. Little-Big-Monkey

    Little-Big-Monkey

    Joined:
    Mar 4, 2014
    Posts:
    40
    Just for your information, for all of you, who use the "_" as a visual indicator for private field, did you know that VS already show a different icon in the intellisense base on the access modifier ? Plus, you can Alt-F to show only fields.

    upload_2020-10-9_12-53-42.png
     
    alexchesser, JoNax97 and Neonage like this.
  21. deivsky

    deivsky

    Joined:
    Nov 3, 2019
    Posts:
    87
    Oh okk, agreed then!

    This.
     
    Neonage likes this.
  22. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    147
    I'd also like to call out the fact that VSCODE is free and available on all platforms mac/linux/windows. Rider and visual studio both have a cost associated with them.

    If there is an interest in being an inclusive community project, then VSCODE is definitely going to in the toolset of *at least some* of the developers.
     
  23. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    61
    Visual Studio Community is free.
     
    alexchesser likes this.
  24. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    147
    I suppose so! If you look at the VS community license it includes unlimited use "for contributing to open source projects."

    Since unity open project 1 is an open source project, then I guess VS community would be good to go without limitation if used in this context. I'm so used to telling new/junior developers to uninstall community and get a licensed version that I forgot.

    Thanks!
     
    kcastagnini likes this.
  25. Soundguy

    Soundguy

    Joined:
    Oct 30, 2009
    Posts:
    49
    does anyone have a rider settings file for this?