Home > Backend Development > C++ > body text

C++ function naming: Comparison of Hungarian notation and naming conventions

WBOY
Release: 2024-05-04 08:18:02
Original
1127 people have browsed it

C Comparison of function naming conventions: Hungarian notation and naming conventions. Hungarian notation indicates types through variable name prefixes, which enhances readability but is verbose; naming conventions use more concise names to improve readability. Hungarian notation enforces type checking, which improves maintainability but can be confusing; the naming convention is more flexible. Hungarian notation has better reusability but poorer naming convention.

C++ 函数命名:匈牙利表示法与命名规范的比较

C Function Naming: Comparison of Hungarian Notation and Naming Conventions

Introduction

Function naming is crucial to maintaining a clear, maintainable code base. Two popular naming conventions are provided in C: Hungarian notation and naming conventions. This article will compare these two methods and provide practical examples to illustrate their differences.

Hungarian notation

Hungarian notation indicates the variable type by adding a prefix before the variable name. For example, iAge represents an integer age variable, and sName represents a string type name variable.

Naming convention

Naming convention uses a more general naming convention. It is recommended to use:

  • CamelCase naming: use lowercase letters for variable names, and start each new word with an uppercase letter (for example, ageInYears).
  • Pascal Naming: Use uppercase letters for variable names, with each word starting with an uppercase letter (for example, AgeInYears).

Practical case

The following two functions demonstrate the violation of Hungarian notation and naming conventions:

Hungarian notation

int getAge(int iAge) {
  return iAge;
}
Copy after login

Naming convention (camel case)

int getAgeInYears(int ageInYears) {
  return ageInYears;
}
Copy after login

Comparison

  • readable Performance: Hungarian notation provides more explicit variable type information, which can enhance readability. However, it also produces lengthy variable names. Naming conventions use more concise names, which improves readability.
  • Maintainability: Hungarian notation enforces type checking, which can help prevent type errors. However, it can lead to cluttered code, especially when using multiple variable types. The naming convention is more flexible, allowing easy conversion between different types.
  • Reusability: Hungarian notation has better reusability because different types of variables can be more easily identified using type prefixes. The naming convention is less reusable because it does not provide explicit type information.

Conclusion

Hungarian notation and naming conventions each have their own advantages and disadvantages. The final choice depends on the specific needs of the project and the developer's personal preferences.

The above is the detailed content of C++ function naming: Comparison of Hungarian notation and naming conventions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template