We use different naming conventions while programming. Hungarian notation is one such method to define variable names. In this article, we will discuss Hungarian notation in various programming languages like C#, C++, and Python. We will also discuss this notation’s advantages, disadvantages, and alternatives in different programming languages.
- What is Hungarian Notation?
- Different Types of Hungarian Notations
- Advantages of the Hungarian Method in Programming
- Disadvantages of the Hungarian Method in Programming
- Hungarian Notation in C#
- Hungarian Notation in C and C++
- Hungarian Notation in Python
What is Hungarian Notation?
Hungarian notation in programming is a naming convention used to denote the data type of a variable while defining it. In the Hungarian method, each variable name starts with or ends with some characters that indicate its data type.
Hungarian notation was designed to be language-independent. It was majorly used in the BCPL programming language. BCPL has no data types other than the machine word. Due to this, programmers found it difficult to specify and remember the data type of the variables. Using the Hungarian method, we can explicitly define the data type of a variable in the variable name itself. Due to this, BCPL programmers found this notation very useful while programming.
To understand the Hungarian method, consider the following examples.
|Variable||Data Type||Variable Name in Hungarian Notation|
In the above table, you can observe that variable names in Hungarian notation depend on the data type of the variables. Here, each variable name in the Hungarian method has two parts.
- The prefix of the variable names defines the data type of the variables and is in small case letters.
- The actual name of the variable we defined to use in our program is specified in the CamelCase pattern. Hence, the first letter of the user-defined names is always capitalized. This helps us identify the variable’s data type as all the small case letters before the first capital letter specify the variable’s data type.
However, this is only part of the picture. Let us discuss different types of Hungarian method to understand variable naming in a better manner.
Different Types of Hungarian Notations
There are two types of Hungarian notation i.e. Systems Hungarian and Apps Hungarian.
Systems Hungarian Method
The Systems Hungarian method focuses on specifying the data type of the variable in its name. All the variable names used in the previous table are named using systems Hungarian. As we need to encode the actual data type of the variable, there is a specified prefix for each data type in systems Hungarian as shown in the following table.
|Data Type||Prefix in Systems Hungarian|
|Character||c or ch|
|Floating Point||f or fp|
|Doble precision floating points||d or db|
|Unsigned 32-bit integer||u32|
Apps Hungarian Method
The Apps Hungarian notation is a bit different from the Systems Hungarian. In Apps Hungarian, the prefix in the variable name represents the purpose of the variable. For example, a variable representing length gets the prefix l. If you create a variable Distance, it will be named as lDistance. Thus, Apps Hungarian represents the logical type instead of the physical data type.
Advantages of the Hungarian Method in Programming
Hungarian notation provides various advantages to programmers or even people reading code. Following are some of the advantages of the Hungarian method.
- In weakly-typed programming languages, you can use Systems Hungarian to define variable names. It will help you identify the data type of variable instantly by looking at the code itself.
- While using the Hungarian method, you need to define variable names in CamelCase notation. Hence, it will also help you with consistent variable naming in your program.
- Hungarian notation helps create multiple variables with the same name and different data types. For example, you can create a variable Length and add different prefixes to create different variables of multiple data types such as iLength for integer and fLength for floating point value.
- The Hungarian method can also help you identify errors in casting and type-mismatch by simply reading the code. As each variable name has its data type, you can easily identify if you are putting variables with incompatible data types into a single statement.
- You can also avoid naming collisions using Hungarian notation. If two objects of different class types have the same attributes, you can use Apps Hungarian to define the purpose of each member attribute and the classes they belong to.
Disadvantages of the Hungarian Method in Programming
Despite the advantages, there are also many disadvantages of the Hungarian method. Let us discuss some of them.
- The Hungarian notation decreases code readability. Appending a prefix before the variable name can make it hard for you to read the variable names.
- We have programming languages with strong typing and better naming conventions. Almost all the compilers have in-built type checking. Hence, the Hungarian method is redundant for most programming languages nowadays.
- Hungarian Notation becomes confusing when it is used to represent several properties. This is due to the reason that the prefix becomes longer leading to confusing variable names.
- The Hungarian method may lead to inconsistency when we modify or share the code. If we change a variable’s data type, the prefix in the name of the variable will be inconsistent with the new type. Hence, we will need to change the variable name too.
- Hungarian notation reduces the benefits of using code editors that support code completion. In Hungarian method, we always need to type the prefix for the data type for every variable. Here, the prefix is most likely to collide with other variables and we won’t be able to autocomplete the variable names without typing three or four characters.
Suggested Reading: Best coding practices
Hungarian Notation in C#
Nowadays, Hungarian notation isn’t used in C# development practices. The original form of Hungarian method uses prefixes such as “sz” for zero-terminated strings, “n” for integer numbers, “p” for pointers, and so on. However, this style of Hungarian method fell out of favor because it can make code harder to read and maintain.
In C#, the use of Hungarian notation is generally discouraged. Instead, we use the semantic naming convention. In semantic naming, we rely on clear and meaningful variable and function names that convey their purpose and intent. By using descriptive names, it becomes easier to understand the purpose and context of the code, without relying on prefixes to denote data types.
For example, instead of using a Hungarian notation like “
strName” for a string variable, we can simply use the variable “
name” because names are mostly strings. Even if you are the son of Elon Musk, your name might contain certain special characters. But it will still be a string. Similarly, instead of using “
intCount” for an integer variable, “
count” would be preferred.
Hungarian Notation in C and C++
Just like C#, the use of Hungarian notation in C++ or C has become less common in recent years. The C++ Core Guidelines strongly discourage the use of Hungarian method. The guidelines advocate for using clear and descriptive names that reflect the purpose of variables and functions without relying on prefixes to convey their types as in semantic naming.
Hungarian Notation in Python
Hungarian notation is not commonly used in Python. Python is a dynamically typed language and variable types are determined at runtime. Hence, explicit type annotations are optional. However, some programmers use an adaptation of the Hungarian method for variable names. For example, you might have seen people naming Pandas dataframes with the suffix df. For example, if a dataframe contains grades of students in a class, it is named grades_df.
Python’s strong emphasis on readability and simplicity encourages developers to write code that is self-explanatory without relying on naming conventions like Hungarian method. Instead of using prefixes and suffixes to indicate data types, we can rely on the following naming conventions for better code readability in Python.
- Use lowercase words separated by underscores for variable and function names (e.g., count, calculate_average).
- For class names, use CamelCase(e.g., MyClass, CarModel).
In Python 3.5 and above, we can use type hints to declare the type of variables or function parameters. However, type hints are not part of Hungarian notation. We use type hints for static analysis and documentation purposes rather than influencing the actual runtime behavior of Python code.
In this article, we discussed the Hungarian notation for variable naming in different languages. We also discussed the advantages and disadvantages of the Hungarian method. To learn more about code quality, you can read this article on code refactoring techniques. You might also like this article on the Zen of Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.