Question

In Java or Scala if I have an argument configuration: Conf, I can look for Conf class/trait/case class and see its construct so I know which arguments to pass.

Recently I started dealing with JavaScript, I see function like this:

function init(conf) {
  some body // as external developer to init I should not mess 
            // with the internals here I just use init, proper design.
 }

What I'm curious to know, how is it possible for me to know what to send exactly in conf? documentation? examples? sniff around init implementation? all look like bad alternatives for me, not well formed, depending on developer writing or not documentation providing or not examples. isn't there a more formal strict way for me to know what conf means?

I must say that on my day to day work with statically typed languages I don't need almost no documentation I just look at the types functions receive and I know what I need to pass in most cases.

Was it helpful?

Solution

You don't. That's the problem. When types are not declared on a method's arguments, and there isn't a comment on the code saying what it's expecting to receive, there are only two ways to figure out what it expects you to pass. Either examine the function itself and see what it's doing with the input, or look at documentation. (Or sample code, which is really another form of documentation.)

Unfortunately, looking at the code, even if it's "a small, well written function," may not be as easy as David Arno suggests in his answer. Many small, simple functions consist of nothing more than passing on your arguments to one or more function calls, rearranging things to make them fit more conveniently. Then you end up with more and more code to dig through. Depending on what you're doing, you could have to go a dozen layers down before you reach the code that actually does something. (This is annoyingly common in database access code, for example.) And if any of the methods along the way call methods of unknown types, your job becomes even more confusing.

TLDR: In non-trivial code, if you don't have good, correct documentation, it's often very difficult to know what to pass to an untyped argument. This is one of the big reasons why you so rarely see large, complex projects written in dynamic languages.

OTHER TIPS

This isn't a problem unique to JavaScript, or even just dynamic languages. For example, you might have the equivalent Java code:

void init(Configuration conf) {
    ...
}

and then discover that Configuration is an final class with no public constructors and you're left searching around for how to obtain an instance of that class.

Typically, looking at the code is your first course of action, assuming the source is available. The source is the only official, up-to-date, guaranteed-to-be-right documentation for what the function does. If it's a small, well written function, working out what conf needs to be should be an easy task.

If the library/framework in question has accompanying documentation, you may be able to use that to understand how to use the function.

Finally, specific to JavaScript, if you want static typing, use TypeScript, which is a free, open-source statically typed extension to JavaScript.

Dynamically typed or not, it's best to consult documentation and/or examples.

Even if you know what type a parameter is, it's often quicker and safer to base your code off of an example, with more detailed documentation on hand, than it is to assume you know what you're doing based on the parameter type.

Otherwise, you're just making assumptions and experimenting anyway. And if that's your preference -- well, good luck, first of all. But, if you really want to work off of assumptions (or perhaps are working with an undocumented, minified, black box library), you may as well start with the fewest assumptions possible: Give it a null and see what error(s) you get back.

Trial amd error.

Actually, I doubt there's a resource without a proper documentation in it. If you really can't find what goes where, then you could type the function's name without parentheses in the chrome live console, and then, if the functiom is not a native function, then it prints out the source of the function.:

Licensed under: CC-BY-SA with attribution
scroll top