-2

I have been reading documentation but when I see method or function I see something like this.

str.count(sub[, start[, end]])

emitter.emit(eventName[, ...args])

I couldn't figure out what bracket for and what is require parameters and what is optional.

this code is from python documentation I see brackets and other things also.

what I want is:

  1. what is the name of this pattern.
  2. how to understand it

Because I see this pattern a lot of time and actually I like it but don't understand it and I want to use it to document my future code.

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
  • We're going need context in order to even start answering this question. Please either leak the documentation that contains or what this function does. – xinkecf35 Apr 27 '20 at 19:12
  • 2
    For python, look at https://stackoverflow.com/questions/1718903/what-do-square-brackets-mean-in-function-class-documentation – Eric Truett Apr 27 '20 at 19:16

3 Answers3

1

Overview

Simply, everything inside the square brackets shows an optional parameter. When you have square brackets inside other square brackets, it means that the second square brackets are only valid if the first square brackets were used.

First documentation

str.count(sub[, start[, end]])

The documentation shows this: str.count(sub[, start[, end]]). Each variable inside ( ) (so sub, start and end) are parameters.

Lets start off with a simpler example. Imagine instead the documentation only allowed for two parameters like so: str.count(sub, start). The problem here is that start is optional! The user only has to include it if they want to start the search from somewhere other than the start of the string.

The solution to this, is to always include optional parameters inside [ ]. So this means our new documentation is: str.count(sub[, start]).

The reason the comma is inside the [ ] is because you can almost think of "removing" everything inside the [ ]. If the comma was outside, like this: str.count(sub, [start]) and you removed everything inside the [ ], then it would look like: str.count(sub, ) which we can see is wrong.

First documentation, further explanation

So that's the reason behind the funny comma [ ] thing that you've seen. But then the question of "why are there things like [, [ ]]???" remains.

The answer: because some optional parameters depend on others. In this case, the end parameter can only be used if the start parameter has been used. If we tried our previous rule and kept both start and end inside the same [ ], then we get: str.count(sub[, start, end]).

This is wrong, because it suggests that we can only use both optional parameters or none of the parameters, since if we removed the [ ] we get: str.count(sub). There is no inbetween here.

To tell our users this, we put the second [ ] inside the first, so that the second optional parameter is only usable if the first has been used. So if we want to only use the start optional parameter, we keep the first [ ], and remove the second [ ] (which are inside the first), meaning that str.count(sub[, start[, end]]) becomes str.count(sub, start). We have only removed the inner-most [ ].

Using this new system of [, [ ]], it also means that we can't just use the end parameter, since to remove start we have to remove the outside [ ], but this also removes the inside [ ] which then removes end.

First documentation, further further explanation

"But what if we want to be able to use any combination of the two?" You don't. If we did something like: str.count(sub[, start][, end]) this means we can have any combination of start and end.

This breaks things because if we only use the end parameter, there's no way of telling if its start or end! If we use str.count(sub[, start][, end]), we don't know if str.count(sub, 5) means that we want to start at index 5, or end at index 5.

Second documentation

emitter.emit(eventName[, ...args])

This example is almost exactly the same as before, except the ... is actually an operator in JavaScript.

Using our new knowledge, we can tell that emitter.emit has one required parameter eventName (as its not inside [ ]), and one optional parameter [, ...args].

The ... simply means: "get all extra parameters provided after this point, and put them into an array".

So for an example:

function hello(name, ...extraNames) {
    console.log(extraNames);
    console.log('hi ' + name);
    for (let i=0; i<extraNames.length; i++) {
        console.log('hi extra name ' + extraNames[i]);
    }
}

hello('dan')
// []
// hi dan
hello('dan', 'frank', 'mark')
// ['frank', 'mark']
// hi dan
// hi extra name frank
// hi extra name mark

And the reason that the , ...args is inside [ ] is because those extra parameters (known as rest parameters) are always optional. They are always for catching extra parameters passed, and therefore also have to come last in the function definition.

The equivalent of the ... parameter in Python are *args (arguments), used like so:

def hello(name, *extraNames):
    print(extraNames)
    print('hi ' + name)
    for extraName in extraNames:
        print('hi extra name ' + extraName)

hello('dan')
# []
# hi dan
hello('dan', 'frank', 'mark')
# ['frank', 'mark']
# hi dan
# hi extra name frank
# hi extra name mark

Also look into **kwargs (key word arguments) which allow you to have any number of "key value pairs" given as a dictionary such as this:

def hello(**kwargs):
    print(kwargs)
    for key in kwargs:
        print(key + ' equals ' + kwargs[key])

hello('one'='apple', 'two'='orange')
# {'one'='apple', 'two'='orange'}
# one equals apple
# two equals orange

JavaScript doesn't have an equivalent to this, but you can always pass an object of values as your "kwargs".

Hope this helps!

dwb
  • 2,136
  • 13
  • 27
0

For a string, returns the number of disjoint occurrences of the specified substring in it.

sub - A substring whose number of occurrences should be calculated.
start = 0 - The position in the line from which to start calculating the number of occurrences of the substring.
end = None - The position in the line at which to finish calculating the number of occurrences of the substring.

eventName <string> | <symbol>
... args <Any>
Returns: <boolean>

Synchronously calls each of the listeners registered for the eventName named event in the order in which they were registered, passing the provided arguments to each.

Returns true if the event had listeners, false otherwise.

Jay
  • 2,553
  • 3
  • 17
  • 37
0
    str.count(sub[, start[, end]])

    emitter.emit(eventName[, ...args])

[] means a list of optional parameters. Contents in the brackets are the parameters

for the first example:

  1. sub is requred parameter
  2. start and end are optional parameters. Where you can use that start on its own, and skip the end. If you want to use the end however, you need to provide the start. This is the meaning of the nested brackets- both start and end are optional, but end is optional to start while start is required for end

in the second example:

  1. eventName i required
  2. ...args is optional. The ... notation in front of it means it is an array.

For a function described as eventName[,...args], you would be allowed to pass anything you want, it will be forwarded to the handler as array.

tstoev
  • 1,415
  • 11
  • 12