Is it correct to prefix variable names with their type?

14

Is it customary / correct to use variables where the first letter refers to your typing?

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

EDIT:

The name said is Hungarian notation . Is this a good practice? What is the good side and what is the bad side of this use?

    
asked by anonymous 07.07.2016 / 15:37

3 answers

22

This is called Hungarian notation (the original question is not I spoke what was). That's right ... if you have a reason to do so. Or it's wrong to do it just because you saw someone else doing it.

Looking at these basic types do not look very damaging.

In general it is not recommended to do so. In addition to being an indication that the variable name is not telling you what its function is in the code, you probably have problems in the future if you need to change the type. The type should not be important.

At least this holds true for static typing languages. In dynamic typing languages it can be quite useful to do this. I have experience with this and it helps a lot to detect problems, do 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 annotating the type, I would not do that. It's redundant information.

Curiously, a lot of people do this without realizing it in less obvious cases, like btnDeletar . This btn is Hungarian notation. This is because the person learns from bad examples, often even in the documentation of the language, and the programmer does not question why he is doing it, only reproduces the recipe that has been passed to him. That's very wrong.

It's okay to say something is a button, but if it's really important for the variable name to indicate that it's a button, then BotaoDeletar . Believe in the name, describe it right. If it is just to indicate the type that is an object of type Button , that is not usually a good idea.

Think you have a chkOpcao and a day needs to change to rdoOpcao . Imagine having to change everything in the code. Worse, imagine changing the type of object, but do not change the name. Only disgrace. It gets worse when the varable is public.

Have you heard that comment could be out of step with the code? This is the worst kind of commentary there is. It's harder to update it.

In addition, if you are not Hungarian (joke, okay?), it is harder to read such a code (imagine pronouncing).

But if you know what you're doing, put yourself where you should, when it's needed, if it helps you to document something in the best way and not cause potential problems, there's no problem in using it.

In a language I always use (the language has just over half a dozen types and therefore does not generate horrendous names). In other usage never. But even where I used it, today I feel less need to use. Organizing the code well and having the right tools, you can live without the Hungarians. Its use usually indicates other problems in the code. This is treating the symptom rather than the disease.

Good practice is doing the right thing, which solves the problem without causing others. No matter what they say it is to do, it is important to understand all the implications of using something and making your own decision.

One of the most famous articles on the subject of the founder of this site. There are those who disagree.

Disadvantages cited on Wikipedia (ironically, they seem very redundant). The whole article has relevant information.

This does not only happen with variables. There are cases where information is an absurdity of irrelevance.

    
07.07.2016 / 16:11
9

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

But the time was 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 say functional type, I do not refer to the type of the object, such as int , string , date , etc ... But some behavior, such as whether it is secure or not write the same in memory. In this case Charles Simonyi prefixed s to safe and u to unsafe .

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

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

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

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

MSDN: General Naming Conventions

    
07.07.2016 / 16:11
2

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

But do not use this nomenclature to name variables, table, object, etc.

And a name suggestive of its variable.

    
07.07.2016 / 16:01