/!\ I do JS at home mostly for fun and I'm still at school so I never used JS on a production context.
I find it useful to have a clear error when I use a function improperly. So I usually just add if blocks throwing errors if something is wrong. I guess in a production code, you could ass some special comments around and have their contents removed for the production version. Another solution a to have a function checkArguments to which you pass the arguments object and an array / object / whatever describing what the arguments object should contain. And in production, you would replace the declaration of that function by a function doing nothing. But there is a drawback with those ways of doing it: things you would expect to work probably won't. e.g., if a function can take an array, I could expect it so take an arguments object or any other array-llike object. I would also expect functions taking strings to take buffers. So another way of doing it is to simply transform the input into what you expect. +o for number parseInt( o, 10 ) for integer o+'' for string Object( o ) for object [ ].slice.call( o ) for Array !!o for boolean On Sat, Oct 29, 2011 at 11:14 PM, Michael Haufe (TNO) <[email protected]> wrote: > On Oct 29, 2:41 pm, Jordan Harrison <[email protected]> wrote: >> I'm not sure when and where it is best to check function parameters >> for the correct type and valid input. > > This question is almost always the source of a flame-war... > >> What kind of function parameter type checking do you do? Do you check >> in every function? Just the core library functions? What do you think >> about the overhead costs of checking? What kind of checking patterns >> do you use to write less code or make the process of writing it easier? > > My personal approach is a bit unpopular. There was a related thread > about a year ago you may like: > > https://groups.google.com/group/jsmentors/browse_thread/thread/e41c08dd7b50ae4/201681b463f68d6c > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
