Hacker News new | ask | show | jobs
by tinkertamper 2051 days ago
I’ve found returning a tuple with my validation functions to be very helpful.

  let [isValid, err] = validate(data)
It’s nice for a few reasons:

1. I know anything returned `err` is safe to display back to the user

2. Using the array (as opposed to an object) can allow me to name the values when I destructure so it can be used multiple times in the same block with useful naming.

3. I can destructure just the first half `[isValid]` if that’s all I care about.

4. More generally it helps keep the code readable. Other control flows might rely on some casting, assumptions, or exceptions but this way presents a nice customizable api for users writing the code, and a readable api for those reviewing the code.

1 comments

You probably already know this but you can destructive just the second half as well.

`cont [, err] = validate(data)`