That’s called hungarian notation (the original question did not say what it was). It is correct... if you have a reason to do it. Or it is wrong to do it just because you saw someone doing it.
Looking like that these basic types don’t seem very damaging.
This is not generally recommended. It is an indication that the variable name is not saying what its function is in the code, and you may probably have problems in the future if you need to change the type, the type should not be important.
At least that goes for static typing languages. In dynamic typing languages it can be quite useful to do so. I have experience with this and it helps a lot to detect problems, not get lost in what you are using. Today it helps less because there are other tools that help, but they are not always available in all cases.
In the case shown where the typing seems to be static, or at least can note the type, I would not do it, it is redundant information.
Interestingly a lot of people do this without noticing in less obvious cases, like btnDeletar
. That one btn
is a Hungarian notation. This is because one learns from bad examples, often even in the language documentation, and the programmer does not question why he is doing it, only reproduces the recipe they passed to him. Then it’s very wrong.
It’s okay to say something’s a button, but if it’s really important for the variable name indicating it is a button, then do BotaoDeletar
. Believe the name, describe it correctly. If it is only to indicate the type that is an object of the type Button
, then it’s not usually a good idea.
Think you got a chkOpcao
and one day you need to change to rdoOpcao
. Imagine having to change everything in the code. Worse, imagine changing the type of object, but not changing the name. Just disgrace. It gets worse when the variable has public visibility.
Have you heard that comment may lag behind the code? This is the worst kind of comment there is. It’s harder to update it.
Also if you’re not Hungarian (joke, okay? ) it’s harder to read code like that (imagine pronouncing).
But if you know what you’re doing, if you put it where it should, when it’s needed, if it helps to document something in the best way and doesn’t cause potential problems, then it’s okay to use it.
In one language I always use (the language has just over half a dozen types and therefore does not generate horrendous names). In other use never. But even where I used to, today I feel less need to use.
By organizing the code well and having the right tools, you can live without the Hungarians. Its use usually indicates other problems in the code. That’s treating the symptom, not the disease.
Good practice is to do the right thing, which solves the problem without causing others. No matter what they say it’s to do, it’s important to understand all the implications of using something and making your own decision.
One of the most famous articles on the subject founder of this site. There are those who disagree.
Disadvantages cited in Wikipedia (ironically seem quite redundant). The whole article has relevant information.
This is not only with variables. There are cases that information is an absurdity of irrelevance.
This exists but is not common(still), has other strange things like prefixing variables with
v
,o
for objects,t
tables,c
fields.– rray
Related: Best practices for naming functions
– rray
A great book to read on the subject would be the "Clean Code" by Robert Cecil Martin. There’s a whole chapter talking about the names we give the variables. I won’t post anything because the answer given by @bigown sums up quite a part of that chapter.
– Filipe Moraes