Am I immoral for using a variable name that differs from its type only by case?

前端 未结 8 1016
醉梦人生
醉梦人生 2021-02-11 11:43

For instance, take this piece of code:

var person = new Person();

or for you Pythonistas:

person = Person()

I

相关标签:
8条回答
  • 2021-02-11 12:11

    Jason - I'm not sure who has told you that this is bad. A number of authors use this as a standard way of expressing an Instance (lower case) of a Class (capitalized).

    I use this quite often as I find that the lower-cased variable actually communicates to me not only that this is an instance but also the name of the class.

    Unless someone has a solid argument to the contrary, I'll certainly continue doing this.

    0 讨论(0)
  • 2021-02-11 12:12

    If the Person is a general Person in the context, then "person" is a really good name. Of course if the Person has a specific role in the code then it's better to name her using the role.

    0 讨论(0)
  • 2021-02-11 12:15

    What is the reasoning of those telling you this is bad? I do this all the time. It is the simplest, expressive way to name a single variable of a type. If you needed two Person objects then you could prefix person with meaningful adjectives like

    fastPerson
    slowPerson
    

    otherwise just

    person
    

    is fine with me.

    0 讨论(0)
  • 2021-02-11 12:26

    I use it all the time for temporary object references. I would avoid it like the plague for primitive data types.

    Person person = new Person(); // okay
    
    int Int = 42; // pure evil
    
    0 讨论(0)
  • 2021-02-11 12:29

    I don't think it's necessarily "bad", but obviously if you can qualify it to give it more context, like what sort of person it is (you are dealing with only one of presumably many possible persons), then someone else picking it up may understand better.

    0 讨论(0)
  • 2021-02-11 12:30

    If someone says that is evil, ask them if this is better:

    var abc = new Person();
    
    0 讨论(0)
提交回复
热议问题