views:

114

answers:

3

I'm specifically referring to JavaScript anonymous function but this could be relevant to other languages. I like to use JSDoc notations in my scripts because I know other people will be hacking at it sooner or later. When i have pretty complex anonymous function how do people document it so that it gets picked up by Eclipse and other IDE's that understand JSDoc or JavaDoc notations?

/**
 * Blah Blah blah
 *
 * @param Object Blah blah blah
 * @return Blah Blah Blah
 * @type Object
 */
function foo(this) {
......
this.bar = function () { ... complex code .....};
......
return obj;
}

Thanks

A: 

By making it not anonymous.

I find the best way to document something is to not to. What I mean I I use good variable/function names and clear code as my document.

So in this case I would just create a static function that creates that anonymous and thus I can give that static function a good name.it

Pyrolistical
Don't talk in riddles. It makes you sound stupid.
Coronatus
Are you saying Yoda is stupid. How dare you! :-)
Clutch
While self documenting code is always a good approach and should be followed, it should not be construed as complete documentation. There are many occasions where further documentation is warranted and desirable. Nothing worse than running across code that the author "thinks" is perfectly self-explanatory by itself, but only the author thinks that.
Bill
@Bill but if the author of the code things that what do you think his comments are going to be? useless and duplicated work
Pyrolistical
@Pyrolistical I am not talking about bad documenters,I'm talking about COMPLETE documentation. Just because the author (who is intimately familiar with the code he wrote) thinks the code is self-documenting, and not in need of further clarification, does not mean this is the case for others. There is a reason all languages have documentation blocks. I'd rather have too much documentation, even "duplicated effort" than too little.
Bill
The code should document `How`, the comments should document `Why`.
Justin Ethier
+10  A: 

Ideally an anonymous function should be short and perform a straightforward task. So... the outer function that contains it should provide sufficient documentation.

If that is not the case you should likely extract the anonymous function out into a named function, and then properly document it.

Justin Ethier
+1  A: 

This is not really an anonymous function. It has a name. The name is "bar", or, more specifically, "foo.bar". What this really is, to my way of thinking, is a function literal. To my way of thinking, a truly anonymous function is a function that really does not have a name, like an argument to another function:

var intervalId = setTimeout(function() { // statements }, 1000);

Now, according to David Flanagan in Javascript: The Definitive Guide, an anonymous function is one that is "created with the Function() constructor." He hedges his bets there, though, because he goes on further to state that such functions are only "sometimes" called anonymous.

Why am I raising this issue here? Because I think if you have a name for something, it can't really be called anonymous. And I think the term "anonymous" with respect to Javascript functions is ambiguous at best, and ought to at least be clarified by someone somewhere.

Robusto