JS - Best Practices (Functions)

Functions should do one thing only on one level of abstraction.
Functions should either do something (modify) or answer something (query), but not both.

Avoid long argument list : use a single object parameter and destructuring assignment instead. It also makes handling optional parameters much easier.

Bad :

function getRegisteredUsers (fields, include, fromDate, toDate) {
/* implementation */ }

getRegisteredUsers(['firstName', 'lastName', 'email'], ['invitedUsers'], '2016-09-26', '2016-12-13')

Correct :

function getRegisteredUsers ({ fields, include, fromDate, toDate })
{ /* implementation */ }

getRegisteredUsers({
fields: ['firstName', 'lastName', 'email'],
include: ['invitedUsers'],
fromDate: '2016-09-26',
toDate: '2016-12-13'
})

 
 

js best practices