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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Variable name confusion

Discussion in 'Scripting' started by davejones1, Jul 20, 2018.

  1. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I am trying to create a boolean variable called 3D_pressed;
    But I am getting an error which states
    "error CS1519: Unexpected symbol `3' in class, struct, or interface member declaration". How do I solve this?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    You cannot define variables or methods that starts with a digit in C#.
    I recommend adopting either Unity naming standard, or Microsoft one.

    E.g. if you want to declare public var, just name it Pressed. Or private - (Unity) m_Pressed, C# default (_pressed)
     
    davejones1 likes this.
  3. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Is there some documentation on why a variable can't be started with a digit?
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Here is the official Microsoft spec- whizz down to the heading "Identifiers".
     
    davejones1 likes this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    I think, it is a programming principle, not to give names, starting with numbers.
    The reason would probably be heading back to early ages of programming, when certain limitation where implied, to ensure system works. For example short naming convention. This days, generally that is not a problem, yet not necessary you can have infinite path, or variable. But they can be pretty long.

    Regarding your variable, you can rename it to
    is3Dpressed, or is3D_pressed, or is_3D_pressed
    first one is nice and clean.

    I tend use (is) prefix for Boolean, or (b_), if (is) not suitable.
    With constant prefixes, it is easier to find relevant type of variables, when many of them are in the scope.
     
  6. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Ok thanks for your suggestions. I personally end the name of each Boolean with _bo.
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    In fact out of interest, I use prefixes for most of variables.
    I.e.
    is or b_ -> Boolean
    i_ -> integer
    f_ -> float
    d_ -> double
    s_ -> string
    V3_ -> Vector3
    l_ -> list
    dic_ -> dictionary
    and got few more

    So for example I can have:
    Code (CSharp):
    1.  
    2. float f_velocity = 10.5f ;
    3. Vector3  V3_velocity = Vector.forward * f_velocity ;
    4. List <Vector3> l_velocity = new List <Vector3> () ; // I never use var
    5. l_velocity.Add ( V3_velocity ) // list of velocities
    6.  
    So as you can see, I can use same name base, for different variables.
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Personal taste, I'd be totally annoyed by all these type-indicating prefixes. :D

    One thing I use as well are prefixes such as 'Is' ('Has' and you can sometimes even find 'Can' in the .Net framework). They can be used to avoid confusion between boolean members and events, as the latter often use a present and past tense (at least that is a common convention).
     
    Munchy2007, xVergilx and davejones1 like this.
  9. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Cool insight. I tend to add mine at the end. For example - pseudo code
    3D_pressed_bo - Boolean variable
    child_object_cube_tfm - I use _tfm for Transform variables. Do you have a prefix for Transform variables.
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    yep
    Tr_ -> Transform
    Rb_ -> Rigidbody
    Q_ -> Quaternion

    But in general, I try to avoid underscore in names.
    I use them Only, if I separate category of variable type. Rarely, since then using classes instead, to group variables.
    But in case naming with multiple words, I capitalist first letter of each word, other than first word after prefix, I could write:
    Tr_guyRunningThroughForest
    V3_myCarVelocity
    Q_homeOnTheMoon -> since is quaternion, I don't specify that this is rotation

    then classes are starting with capital always
    class Car {}
    class CarGarage {}

    while methods use additionally underscore and also first capital letter
    void _GetCarPosition () {}
    void _AddCar2Garage () {}-> I use 2 as To, or 4 as For (for me much better than _AddCarToGarage, or _Add_Car_To_Garage)
    void _GetSpeed () {} ;

    etc ..

    And if I got class for example
    Code (CSharp):
    1. class SpaceCraft
    2. {
    3.     string s_name ;
    4.  
    5.     Transform Tr ; // it is abvious, it belongs to SpaceCraft, so no extra naming in variable
    6.     Rigidbody Rb ; // same here
    7.  
    8. }
     
    Last edited: Jul 21, 2018
    davejones1 likes this.
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    I know is from person to person preference.
    I took concept and adapted prefixes naming, from when I was programming PLCs.
    Serves me well ;)

    Also, rarely the case, but if happens that I am viewing the code from none C# (or relevant language) formatted editor, prefixes becomes helpful.

    In case I see
    car.acceleration = Vector2.right ;
    I will not know straight forward, that car acceleration is a Vector3; Yes, I can hover over variable etc.
    Also
    car.happiness = 3 ; // but because I haven't put (f) after number, is not obvious, that this value can take float and be
    car.happiness = 2.5f ;

    Now you have bunch of these in method and you will be checking each variable, which is int, double, or maybe float.
    Hence in my case, prefixes are very useful. :)
     
    Last edited: Jul 21, 2018
    davejones1 likes this.
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Yes, it is indeed personal preference, a very special one though.

    The thing is, you wouldn't encode type information in method names, would you? Instead, you'd simply take a look at the signature and its parameter list to actually check your options.

    In the end, we're dealing with a strongly typed language, which renders a lot of prefixes and suffixes redundant.
     
    davejones1 and xVergilx like this.