Skip to content

Latest commit

 

History

History

how-to-name-functions

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

How to name functions

Naming functions is an essential aspect of writing clean and maintainable code. A well-chosen function name should be descriptive, concise, and meaningful, providing a clear indication of its purpose and behavior.

Tips…

Use descriptive names: Choose names that clearly describe what the function does, like "calculateTotal," "sendEmail," or "validateInput". Avoid generic names like "process," "execute," or "handle."

Follow a consistent style: Stick to a consistent naming convention throughout your codebase. Common conventions include camelCase (e.g., calculateInterestRate) or snake_case (e.g., check_file_exists).

Use verbs for actions: Begin function names with action verbs that indicate what the function does. , such as an operation or action. For example, "saveData" or "generateReport."

Use nouns for simple operations: For functions that return a value without performing any action, using nouns can be appropriate. For example, "getUserName" or "getTotalAmount."

Avoid abbreviations: Use descriptive names rather than abbreviations. However, if an abbreviation is widely understood within your domain, consider using it, such as "URL" for Uniform Resource Locator.

Avoid overloading: Don't use the same function name for different behaviors (function overloading) unless the functions are clearly related and perform similar operations. Overloading can lead to confusion.

Choose specific names over comments: If the function name is descriptive enough, it can replace the need for extensive comments, making the code more self-documenting.