Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I develop a game library in javascript, containing many classes. I hesitate on the behavior that I should follow concerning method parameter validation:

Should I check the validity of parameters passed to each method ?

By example, when a method take only a number between 0 and 100 in parameter, should I check that the value is correct?

I have dozens of classes, each with dozens of methods. For a simple getter, I can have more than half lines code only used for checking parameters. Add checks makes my code less maintainable, more heavy.

Seeing that it's a library, destined to be used by many other programmers, checking parameters can avoid many mistakes and bugs, and would be a appreciated.

So, how do other javascript libraries, handle this and what is the best solution?

share|improve this question

closed as too broad by gnat, Kilian Foth, MichaelT, Michael Kohne, GlenH7 Mar 2 at 16:22

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
downvoters, it would be nice if you will explain, why this question get down votes? –  BAK Mar 12 '13 at 14:04
1  
There seems to be a good answer to this over on stack exchange: stackoverflow.com/questions/1723080/… where the consensus seems to be, yes you should, with a variety of suggestions on the "how". –  Maks Feb 28 at 2:16

2 Answers 2

up vote 2 down vote accepted

Validate anything that you might consider "suspect", i.e. anything that comes from outside your own class. Parameters to public methods? They come from "Outside" so validate them. Parameters to private methods? Technically these are already "Inside" so there's less reason to do it again. (Of course, you might be checking for different criteria at this level, so you might still have Good Reason to do so).

And have a consistent means of handling parameter "problems". Some people like to throw Exceptions, others return null values from their methods. YMMV. Pick what works for you and stick with it.

share|improve this answer

Yes, you should always validate your inputs. Not validating is one of the primary sources of unexpected behaviour.

Without knowing the structure of your code, if you have a bunch of simple methods and one core method you could put your validation code in the main method rather than all of the individual ones.

As you say it is a library, developers will do their own validation based on their business logic. Their business logic may be different from yours.

A quick flick through the jQuery source sees them validating input parameters.

share|improve this answer
    
What are you talking about? jQuery never validates any input. Silent failure is one of the most prominent features of jQuery. jQuery.error is used 3 times in the entire source. –  Esailija Mar 11 '13 at 5:47
    
@Esailija did you read the source? I said "a quick flick" see code.jquery.com/jquery-latest.js look for jQuery.fn = jQuery.prototype = { you will see validation on the parameter selector. –  Sam Mar 11 '13 at 5:52
    
That's not validation, if you do $(null) that code will just make it fail silently. It will not throw error or even warn you that null is not a proper argument. –  Esailija Mar 11 '13 at 5:53
    
Just saying that claiming that jQuery does validation is wrong. jQuery either fails silently or gives you meaningless error deep in their source when you don't pass expected arguments. –  Esailija Mar 11 '13 at 6:02

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