Home  >  Article  >  Backend Development  >  The evolution of C++ function naming rules

The evolution of C++ function naming rules

WBOY
WBOYOriginal
2024-04-24 15:09:01417browse

C Function naming rules have evolved from the classic "Hungarian notation" to modern descriptive naming. Modern rules include: use meaningful names, abstract, avoid prefixes, use camelCase, and consider namespaces. Modern naming is more readable and descriptive compared to the classic rules, for example "sum(int first, int second)" is clearer than "AddNumbers(int nNum1, int nNum2)".

C++ 函数命名规则的演变

The evolution of C function naming rules

Introduction

Function naming is C A key style guide in programming. Good function naming can improve the readability and maintainability of code. Over time, C's function naming conventions have changed.

Classic Naming Rules

Early versions of C adopted "Hungarian notation", in which a prefix identifier indicates the type and scope of a variable. For example:

int nCount;
char* szName;

Modern Naming Rules

In recent years, the C community has moved toward more readable and descriptive function naming. Modern rules follow these principles:

  • Use meaningful names: Function names should describe their purpose clearly and concisely.
  • Use verbs or nouns to abstract: Choose verbs or nouns that embody the purpose of the function.
  • Avoid using prefixed identifiers: Do not use type or range prefixes in function names unless there is a special reason.
  • Use camelCase notation: For example, sumNumbers(), not Sum_numbers().
  • Consider namespaces: If the function name exists in multiple namespaces, use namespace qualifiers.

Practical

Classic naming rules:

int AddNumbers(int nNum1, int nNum2)
{
    // ...
}

Modern naming rules:

int sum(int first, int second)
{
    // ...
}

The modern version is more descriptive and the code is more readable.

Advantages of CamelCase Nomenclature

CamelCase nomenclature is more consistent with natural language than underline nomenclature and reduces cognitive load. For example, the function get_count() is not as easy to read as getCount().

Conclusion

C's function naming rules have evolved from classic Hungarian notation to modern descriptive naming. By following modern rules, you can write code that is easier to read and maintain.

The above is the detailed content of The evolution of C++ function naming rules. 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