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.

C# naming conventions for Unity

Discussion in 'General Discussion' started by Demigiant, May 11, 2012.

  1. Hoorza

    Hoorza

    Joined:
    May 8, 2016
    Posts:
    39
    There is as many conventions as people I guess.
    What I am trying to stick to is conventions from: https://www.codeproject.com/Tips/261001/Csharp-Tips-Tricks for C# and for Unity I am following how most people are spelling but I am thinking about how close I can get naming in Unity to the naming convention in C#. If you have any thoughts on this please share, if it is good or bad idea and why.
    I am only starting to learn but the fact that it is different in both environments drives me crazy.
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    976
  3. Argzero

    Argzero

    Joined:
    Feb 17, 2015
    Posts:
    1
    I grew up with C# as lowerCase camelCase for anything inaccessible to external, non-friend, non-linked through inheritance variables and functions/methods.

    For anything accessible outside the class or its "associates" I use UpperCase CamelCase and nothing else.

    Underscores do not have a clear meaning to anyone not familiar with the convention.
    Similarly, appending variable prefixes can be confusing as it may create difficulty for types with similar starting letters, unnecessarily bloating variable names.
    In either case, the variable name is being made unnecessarily longer with little added value.

    If I wanted to know whether my variable/method was private/protected/otherwise, I'd hover the variable and look at the tooltip.
    I would name variables with names that are consistent and easy to understand rather than using unnecessarily compressed lingo, except where absolutely necessary.

    If your code is documented properly / well-commented, any information you needed to know about a variable or method should be visible on a tooltip especially if your code is closed source in a DLL for example.
     
    Ryiah likes this.
  4. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    I know it's an old discussion and everyone has their naming conventions which is perfectly ok (as long as you're not in a team and have to fight out which one to use), but I can't resist commenting on what you just wrote @Argzero
    Having to rollover a variable to get a tooltip that tells you if it's private or public is really slow imo. Which is, again, perfectly ok if you like it, but for me it's always nicer when you can understand at a glance what's public and what's not.
     
    Ryiah likes this.
  5. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    1,995
    Oh hello old topic...

    VS 2019 color codes local vs member variables by default so not even hover-over for this required.

    I notice Unity learning material containing m_, k_ variables which is a bit old school, but without a survey I don't know how uncommon
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Personally, I find the "m_", "k_" etc. method a bit too wordy. I find that just an underscore for non-public fields works well enough:
    private int _someInt;
    .
    If I have an inherited class that needs to access a field from its super-class, Visual Studio seems to prefer making a protected property, but I generally avoid inheriting classes that aren't abstract as much as possible, and in those cases, I think it's fine to make the field protected instead.
    I like to reserve the public access modifier purely for external objects only, excluding sub-objects.
     
    Socrates likes this.