Home  >  Article  >  Backend Development  >  Hungarian notation for C++ function naming

Hungarian notation for C++ function naming

王林
王林Original
2024-04-25 09:36:01286browse

Hungarian nomenclature is a C naming convention that specifies type information for variables, functions, and types by using prefixes (indicating types) and suffixes (indicating usage). Its advantages include high readability, ease of debugging and maintenance. The disadvantages are that it is lengthy, visually cluttered, and potentially ambiguous, so it needs to be used with caution.

C++ 函数命名的匈牙利式命名法

Hungarian notation for C function naming

Hungarian notation is a naming convention for specifying variables, functions, and Type information for the type. This convention uses a prefix to indicate the type of data and a suffix to indicate the purpose or intent of the data.

Prefix

Member variableIntegerFloating point numberSizePointerObjectarray##str_bool_Suffix
Prefix Type
# #m_
i_
f_
sz_
p_
obj_
arr_
string
Boolean value

SuffixInOutHelperGetSetCreateDestroyPractical case
Use
Input parameters
Output parameters
Helper function
Value function
Set value function
Create function
Destruction function

Consider the following class:

class Person {
public:
    Person(const std::string& name, int age);
    void PrintInfo() const;

private:
    std::string m_name;
    int m_age;
};

Use Hungarian nomenclature, The constructor and member functions of this class can be rewritten as:

class Person {
public:
    Person(const std::string& str_Name, int i_Age);
    void PrintInfo() const;

private:
    std::string m_name;
    int m_age;
};

Advantages

    Readable:
  • Function names clearly convey their parameter types and uses.
  • Easy to debug:
  • The prefix can help identify the variable type and speed up debugging.
  • Code maintainability:
  • Hungarian nomenclature helps maintain code style consistency and facilitates collaboration.
  • Disadvantages

    Longitude:
  • Prefixes and suffixes can make function names verbose.
  • Visual clutter:
  • Excessive prefixes and suffixes can make code difficult to read.
  • Ambiguousness:
  • The meaning of some prefixes and suffixes may vary depending on the context.
  • Conclusion

Hungarian nomenclature is a valid naming convention in C, but should be used with caution to avoid poorly readable or maintainable code.

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

Statement:
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