Home > Backend Development > C++ > Common conventions for naming C++ functions

Common conventions for naming C++ functions

PHPz
Release: 2024-04-24 18:24:02
Original
1017 people have browsed it

C Common conventions for function naming include: 1. Lowercase camel case naming; 2. Start with a verb; 3. Use descriptive names; 4. Moderate length; 5. Avoid using abbreviations. By following these conventions, you can improve the readability and maintainability of your code.

C++ 函数命名的常用约定

Common conventions for naming functions in C

In C, function naming is crucial as it helps improve the code readability and maintainability. The following are common conventions for naming C functions:

1. Lowercase CamelCase

Lowercase CamelCase nomenclature connects the words in the function name with lowercase letters, each Capitalize the first letter of a word, except for the first word.

bool isValidInput(const std::string& input);
double calculateArea(double radius);
Copy after login

2. Start with a verb

Function names usually start with a verb, indicating the function of the function.

void createFile(const std::string& path);
int findIndex(const std::vector<int>& vec, int value);
Copy after login

3. Use descriptive names

Function names should clearly convey the purpose of the function. Avoid using vague or generic names.

// 不佳
void doSomething(int x, int y);

// 改进
void calculateSum(int x, int y);
Copy after login

4. Moderate length

Function names should be long enough to describe what the function does, but not so long that they are difficult to read.

// 不佳
void veryVeryVeryLongFunctionNameThatIsHardToRead();

// 改进
void getLongestWord(const std::string& str);
Copy after login

5. Avoid using abbreviations

Avoid using abbreviations unless they are widely used as they can be ambiguous.

// 不佳
void prnt(const std::string& str);

// 改进
void print(const std::string& str);
Copy after login

Practical case:

The following is an example of a C function that follows the above convention:

bool isPalindrome(const std::string& str) {
  // ... 函数体 ...
}
Copy after login

Example description:

  • The function name is named isPalindrome in lowercase camel case, clearly conveying its function.
  • The function name starts with the verb is.
  • The function name is descriptive, indicating that the function checks whether the given string is a palindrome.
  • Function names are of moderate length and easy to read.
  • The function name does not contain any abbreviations.

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

Related labels:
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