Thursday, 19 March 2020

Should small functions used only by a module be declared inside or outside that module? Does it matter?

Imagine I have the following ES6 syntax:export default async (username, password) => { const res = await authenticate(username, password); if(!res) { logFailedAttempt(username); } }; Those two methods - authenticate() and logFailedAttempt() - can be declared either inside out outside the export default.I am used to breaking my code up into small functions like this. It seems to be less common in the NodeJS world but to me it produces much better, more readable code.Does it matter which way I do it? Is there a best practice? So I could have either:export default async (username, password) => { const authenticate = async (username, password) => { // } const logFailedAttempt = (username) => { // } // Call the methods here } ...or...const authenticate = async (username, password) => { // } const logFailedAttempt = (username) => { // } export default async (username, password) => { // Call the methods here }

Submitted March 19, 2020 at 04:22PM by niwork-account

No comments:

Post a Comment