0

This question already has an answer here:

I find myself writing a lot of functions whereby the first few lines "verify" the arguments, that is, they typecast, check falseness, range, etc. before doing any work. The main reasoning for this is to prevent idiots—human or otherwise—from passing invalid or illogical arguments to the function. An example in JS would be this:

function mix(red, grn, blu, anotherColor) {
  // typecast, falseness/presence
  this.red = +red || 0
  this.grn = +red || 0
  this.blu = +red || 0

  // args must be within [0, 255]
  this.red = Math.max(0, Math.min(red, 255))
  this.grn = Math.max(0, Math.min(grn, 255))
  this.blu = Math.max(0, Math.min(blu, 255))

  // if anotherColor is falsy/undefined
  anotherColor = anotherColor || {red:0, grn:0, blu:0}

  // do the actual work here
}

Another example is when a recursive function calls itself.

function menulist(pages, depth) {
  // do some stuff

  // when done, recurse
  if (depth > 0) {
    // verify pages.sub exists
    pages.sub = pages.sub || []

    return menulist(pages.sub, depth-1)
  }
}

On the other hand I've heard the opinion that this "verification" shouldn't be done within the function itself, rather, it should be delegated to some other piece of the software. In other words, the function should be "pure" and minimal, assuming the arguments passed will be correct. (If this is the case, where should the verification process be delegated?)

Which opinion is considered best practice in the Javascript community, and/or in the programming community as a whole?

marked as duplicate by Ixrec, gnat, Jules, yannis May 29 '16 at 16:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    I can't think of anything I would say here that isn't in the accepted answer on gnat's question, and I'm pretty sure there's nothing unique to Javascript that would change it, so I'm thinking duplicate. – Ixrec May 29 '16 at 12:39
  • 1
    I agree that this question is a duplicate. The linked question is even more general than this one, since it does not restrict t JavaScript. – Giorgio May 29 '16 at 13:06
1

What you have done isn't validation. You've pretty much just said "Guess what the user wanted". Validating it would throw an error if it was outside the range, not randomly coerce it to inside the range.

It's perfectly fine for a function to validate its arguments. It's also fine to do what you've done, which is to effectively default them. But those two things are not the same.

1

Step 1: You write a spec what your function does. Step 2: You implement it according to the spec.

Your spec may say that inappropriate arguments lead to undefined behaviour, or lead to an error being reported, or get fixed. Each is fine. Just write a spec and follow it.

  • This answer really doesn't have any useful information in it. – whatsisname May 29 '16 at 22:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.