DEV Community

Andriy Ovcharov
Andriy Ovcharov

Posted on

How to Name Functions in JavaScript: A Practical Guide

Hello friends! I'm happy to present you with the third part of my thoughts on naming in JavaScript. If you missed the first and second, you can catch up here:

"Naming Conventions in JavaScript (Classes, Components, Events, APIs)" (first part)
"JavaScript Variable Naming: Rules You Can’t Ignore" (second part)

Good function naming makes code clearer, more maintainable, and less prone to errors. Here are standardized rules with examples:

πŸ“Œ 1. Use Verbs for Actions

// πŸ‘ Good  
function getUserData() { ... }  

// πŸ‘Ž Bad  
function userData() { ... }  // Is this a function or variable?  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 2. Predicate Functions: is, has, can

// πŸ‘ Good  
function isValidUser(user) { ... }  

// πŸ‘Ž Bad  
function checkValidUser(user) { ... }  // Less clear  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 3. Avoid Generic Names (handle, process, doSomething)

// πŸ‘ Good  
function formatDate(date) { ... }  

// πŸ‘Ž Bad  
function handleData(data) { ... }  // What does it handle?  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 4. Focus on "What," Not "How"

// πŸ‘ Good  
function getFullName(user) { ... }  

// πŸ‘Ž Bad  
function composeFullName(user) { ... }  // Overly specific  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 5. Async Functions: Use fetch, load, get

// πŸ‘ Good  
async function fetchUser() { ... }  

// πŸ‘Ž Bad  
async function getUser() { ... }  // Unclear if async  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 6. Unique soon (cream wider, like id, url)

// πŸ‘ Good  
function generateUserId() { ... }  
function parseRequestURL() { ... }  

// πŸ‘Ž Bad  
function genUID() { ... }       // `UID` – not obvious  
function parseReqUrl() { ... }  // `Req` – non-standard abbreviation  

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 7. Consistency in code
Use the same prefixes for similar functions:

// πŸ‘ Good  
function getUser() { ... }  
function getPosts() { ... }   

// πŸ‘Ž Bad  
function getUser() { ... }  
function fetchPosts() { ... }  // Why not `getPosts`?  

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 8. Reflect the level of abstraction

  • Low level (details):
function convertCelsiusToFahrenheit(temp) { ... }  
Enter fullscreen mode Exit fullscreen mode
  • High level (business logic):
function applyDiscount(cart) { ... }  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 9. Follow the project code style

  • camelCase для Ρ„ΡƒΠ½ΠΊΡ†Ρ–ΠΉ:
function calculateTotal() { ... }  
Enter fullscreen mode Exit fullscreen mode
  • PascalCase для класів:
class User { ... }  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 10. Use on... for event handlers

// πŸ‘ Good  
function onClick() { ... }  
function onFormSubmit() { ... }  

// πŸ‘Ž Bad  
function clickHandler() { ... }   
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 11. Mark asynchronous functions (fetch, load, get)

// πŸ‘ Good  
async function fetchUser() { ... }  
async function loadProducts() { ... }   

// πŸ‘Ž Bad  
async function getUser() { ... }  // Is this an HTTP request? 
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 12. Avoid negativity in titles

// πŸ‘ Good  
function isValid(user) { ... }    

// πŸ‘Ž Bad  
function isNotValid(user) { ... }  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 13. Specify the direction of the transformation

function mapUserToDto(user) { ... }  
function formatPriceToUSD(price) { ... }  

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 14. Names shouldn't be misleading

// πŸ‘ Good  
function saveUserToLocal() { ... }       

// πŸ‘Ž Bad  
function saveUser() { ... } // What if it stores in `localStorage`?    
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 15. Avoid duplication in context

// πŸ‘ Good  
userService.getData();        

// πŸ‘Ž Bad  
userService.getUserData();  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 16. Short functions - shorter names

// πŸ‘ Good  
function sum(a, b) { ... }          

// πŸ‘Ž Bad  
function getSumOfTwoNumbers(a, b) { ... }  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 17. Name your arguments clearly.

// πŸ‘ Good  
function validateUser(user) { ... }          

// πŸ‘Ž Bad  
function validate(data) { ... }  
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 18. Name IIFE functions.

// πŸ‘ Good  
(function initApp() { ... })();           

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 19. Facade naming for API

// πŸ‘ Good  
function updateProfile() { ... }             

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 20. The name is a promise!
The function should do exactly what the name says.


Conclusion

Good function names are if:

  • Describes an action or result (not an implementation)

  • Avoids ambiguities

  • Uses conventions (get, is, on, etc.).

Example of ideal naming:

function getUserById(id) { ... }  
function isAdmin(user) { ... }  
function onButtonClick() { ... }  
async function fetchPosts() { ... }  

Enter fullscreen mode Exit fullscreen mode

If you doubt the name, imagine another developer seeing it. Will they understand its purpose?


Read more:

"Naming Conventions in JavaScript (Classes, Components, Events, APIs)" (first part)
"JavaScript Variable Naming: Rules You Can’t Ignore" (second part)

Top comments (0)