Functions should do one thing

Functions should do one thing

Tags
Clean Code
Javascript
Software Development
Published
Published June 13, 2022
Author
Due Date
☑️ Topic: Functions
 
☑️ Idea: Create a function for each action or concept. Functions should do one and only one thing.
 
☑️ Benefits: Readability, reusability, testability, refactorability.
 
☑️ Guideline: In the function description you shouldn’t need to use “and”, “or” or “if”.
 
// BAD function emailClients(clients) { clients.forEach(client => { const clientRecord = database.lookup(client); if (clientRecord.isActive()) { email(client); } }); } // GOOD function emailActiveClients(clients) { clients.filter(isActiveClient).forEach(email); } function isActiveClient(client) { const clientRecord = database.lookup(client); return clientRecord.isActive(); }