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

前端 未结 10 1155
旧时难觅i
旧时难觅i 2021-02-11 11:58

For instance, take this piece of code:

var person = new Person();

or for you Pythonistas:

person = Person()

I

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

    I use this pattern a lot in method signatures. If I'm unable to provide an alternate descriptive name then IMHO, there is nothing wrong with this.

    What is wrong would be if you have two types Person and person then that is very very wrong.

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

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

    var abc = new Person();
    
    0 讨论(0)
  • 2021-02-11 12:20

    I say name for what it is: if the variable represents a person with 2 dogs, call it personWith2Dogs. It the variable has short scope (like a loop var) then person is fine.

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

    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:26

    The reason it is considered bad is if you need to have 2 Person's in the future, you can then end up with code that looks like.

    Person person = new Person();

    Person person2 = new Person();

    That would then be bordering on "Bad". However, in that case you should then refactor your orginal person in order to distinguish between the two.

    As for your example, the variable name "person" is a perfectly descriptive name for the object "Person". Therefore there is nothing wrong with it whatsoever.

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

    I suppose I'll get downvoted for saying so, but ...

    Having just come through a century witnessing epic murder and greed, we programmers are truly blessed if the most immoral thing we can do is name a variable.

    0 讨论(0)
提交回复
热议问题