Is it correct to prefix variable names with their type?

Asked

Viewed 1,076 times

16

It is usual/correct to use variables where the first letter references their typing?

Ex.: string sVariavel; int iVariavel;, etc....

EDIT:

The name of it is hungarian notation. Is that good practice? What’s the good side and what’s the bad side?

  • 2

    This exists but is not common(still), has other strange things like prefixing variables with v, o for objects, t tables, c fields.

  • 2
  • 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.

3 answers

25


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.

  • Very well placed! :)

  • Joel’s article is very good, too bad you didn’t include his argument in your answer. In a nutshell, the idea of the prefix of the Hungarian notation is to indicate something that is not in the language type system. Examples: Is this string safe or insecure to insert into an HTML page? Is this int in window or page coordinates? Is this size in bytes or number of elements? Tobymosque’s answer talks about that. With prefixes, the error is most evident when (for example) you add 5 pixels with 30 oranges: pxLargura + lrjQuantidade.

  • @But from what I understand this is not prefixing with types, which is what the question says. Unless you’re talking about this information being guys, then it’s not for you to do. What Joel’s article says is exactly what I said, I just didn’t set an example.

12

This type of conversion is known as Notação Húngara and was developed by Charles Simonyi and was used for many years in the internal projects of Microsoft.

But the times were different, at that time it developed a little closer to the machine language, so there was no clear way to identify the functional type of the variable.

When I speak of the functional type, I don’t mean the type of the object, as for example int, string, date, etc... But yes to some behavior, as for example, whether it is safe or not to write the same in memory. In this case Charles Simonyi adopted the prefix s for safe and u for unsafe.

The complete document with the Hungarian Notation specification can be found in the MSDN: Hungarian Notation

As this notation was widely used in Microsoft and brought some advantages, other people and companies began to use it, but at some point there was a flaw in the interpretation of Hungarian Notation, messed up the idea of the same with the OO, and started using prefixes that identify the type of the variable.

So with this we have ended up having two Hungarian Notation, to App and the System, where the App Hungarian Notation would be the initial definition of the.

Now a very important point, Hungarian Notation was born within the Microsoft to solve a problem of the time, a problem from which modern languages do not perish, hence the Microsoft does not recommend the use of Hungarian Notation with C#.

MSDN: General Naming Conventions

4

In older systems variables with this nomenclature are quite common, and even in courses (college, etc.) older teachers teach this way.

But don’t use this nomenclature to name variables, table, object, etc.

And a suggestive name to its variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.