DEV Community

Discussion on: Writing code for your future self

Collapse
 
qm3ster profile image
Mihail Malo

And like in my other comment, I would suggest the early return pattern that has been refactored out of validateUsername be reintroduced.
Not just for the sake of short circuiting, which is important for performance if some of the later checks make additional network calls, but also because this pattern is always helpful as it doesn't force the developer to keep as many concepts in their head. (No more references to binding? Can forget concept.)

async function validateUsername(username) {
  if (!validateUsernameLength(username)) return false
  if (!validateAlphanumeric(username)) return false
  if (await checkUsernameExists(username)) return false
  return true
}