For instance, take this piece of code:
var person = new Person();
or for you Pythonistas:
person = Person()
I'm told constantly how bad this is, but have yet to see an example of the immorality of these two lines of code. To me, person is a Person and trying to give it another name is a waste of time. I suppose in the days before syntax highlighting, this would have been a big deal. But these days, it's pretty easy to tell a type name apart from a variable name. Heck, it's even easy to see the difference here on SO.
Or is there something I'm missing? If so, it would be helpful if you could provide an example of code that causes problems.
Not immoral, but if your best name for your variable is the name of the type, something wrong or you just making a proof of concept or something like that. For me a variable name must refer to the meaning in the business context and not to the programming language. It will be more difficult to understand the code.
I often use
Person person = new Person()
myself. Commonly used in Java/C#.Although I ended up wondering yesterday why
doesn't work in C#...
Especially seeing how you can use
String
,string
,Double
,double
,... at will in C#.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.
It depends.
If you have a strict capitalization style, so variables begin lowercase (and use either under_scores or camelCase for word breaks), and Classes begin with Capital Letters, then it's obvious that person is a variable and Person is a class, and when somebody understand this, they won't seem to be in overlapping namespaces. (Similarly, people almost never get confused between the verb or noun "polish" and the adjective "Polish".)
If you don't have such a style, then you've got two names that can easily be confused, and differ only in case. That's bad.
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.
Absolutely nothing wrong with it subject to caveats pointed out by others (summarizing here for convenience): not doing it with primitive types, refactoring the original instance if another instance is added later, not using char-case to differentiate class names, etc.
My rule of thumb? Statements in code should read like simple English sentences.
Person person = new Person();
Employee employee = person.getRole(EMPLOYEE);
Parent parent = person.getRole(PARENT);
person.getFullName();
employee.getSalary();
parent.getChildren();
parent.getFullName(); // assuming decorator pattern at play
if (person.hasRole(EMPLOYEE)) {
...
}
And so forth.
If the variable's scope is limited (the encapsulating method is 10-15 lines, for instance) I might even use 'p' instead of 'person'. Shorter variable names are less of a distraction when trying to hold context in your head. Avoid gratuitous prefixes such as 'a' or (shudder) Hungarian notation and offshoots thereof. (Mind you, I have nothing against such prefixes when used in the appropriate context - C++ / COM / ATL / Win32 API code etc., where it helps to keep assignments / typecasting straight).
My two(!) bits :-)