119

Is there a standard to interpret the syntax of function interfaces in API documentations and if yes, how is it defined?

Here is an example on how to change the color of an item the JavaScript scripting guide for Photoshop for the "fillColor" function:

fillPath
([fillColor]
[, mode]
[, opacity]
[, preserveTransparency] [, feather]
[, wholePath] [, antiAlias])

What is the meaning of the brackets and why are there commas in the brackets? How does this relate to the following example calls?

myPath.fillPath(myNewColor)

myPath.fillPath(mynewColor, {
    mode: RGB,
    opacity: .5
})
miken32
  • 42,008
  • 16
  • 111
  • 154
dbonneville
  • 2,079
  • 4
  • 19
  • 26
  • 6
    Is there an introduction to the API reference document that describes their syntactic conventions? – Greg Hewgill Jun 07 '12 at 04:01
  • 39
    For the person who voted to close: I believe this question has merit, and would "vote not to close" if I could. It's not a question I've seen (or heard) asked before, it addresses a legitimate programming-related problem, and I would personally find the answer useful when I teach people programming-related things. – David Wolever Jun 07 '12 at 04:31
  • 5
    Derek: I think you missed one of the bold sentences in the original post. If you google "how to read api documentation", info in the first 10 results say things like "abstract", "incomplete", "confusing", etc., thus reinforcing the point of my question. – dbonneville Jun 07 '12 at 04:48
  • 2
    Greg: There is no introductions to the Photoshop / Adobe products. They all follow the same format in 2 PDFs per product. The other APIs that I'm thinking of do the same. There is an implicit knowledge that I in many cases don't have and would certainly like to. – dbonneville Jun 07 '12 at 04:50
  • 1
    A useful resource is the object viewer built into Extendscript IDE (hit F1). Clicking on an object will show you what properties and methods it has. Also if it uses other objects as parameters it (usually) links to them so you can see what properties they have too. It's not perfect but it helps. – pdizz Jan 16 '13 at 03:25
  • [Related question with some common syntax conventions.](https://stackoverflow.com/questions/23242493/linux-unix-man-page-syntax-conventions) – Eric Postpischil Nov 04 '18 at 23:51

4 Answers4

103

So why is API documentation written in such a way as to confuse perennial newbs / hackers / DIYers like myself?

It's really not meant to be written that way. I'll agree there seems to be no ease of use across API documentations. However, there is a lot of cross over from older man style syntax conventions, to the modern API/namespace conventions.

Typically, the type of person who works with API, will have some background in development or at the least a 'power user'. These types of users are used to such syntax conventions and it makes more sense for the API document to follow than to try to create new ones.

Is there some mysterious document somewhere that tells people how to read API documentation?

There really is no standard, or RFC, supersekretsyntaxdoc laying around anywhere, however there is a ~30 year old file for UNIX man page synposis format which is widespread use.

Some examples of this (and answering your question) would be :

Underlined words are considered literals, and are typed just as they appear.

Square brackets ( [] ) around an argument indicate that the argument is optional.

Ellipses ... are used to show that the previous argument-prototype may be repeated.

An argument beginning with a minus sign - is often taken to mean some sort of flag argument even if it appears in a position where a file name could appear.

Almost all programming related documentation uses this type of syntax convention, from Python, man pages, javascript libs (Highcharts), etc.


Breaking down your example from Adobe API

fillPath
([fillColor]
[, mode]
[, opacity]
[, preserveTransparency] [, feather]
[, wholePath] [, antiAlias])

We see that fillPath() (a function) takes optional arguments fillColor, mode, opacity, preserveTransparency, feathe, wholePath or antiAlias. Calling fillPath(), you could pass anywhere from none, to all, of those parameters to it. The commas within the optional [] mean that if this parameter is used in addition to others, you need the comma to seperate it. (Common sense sometimes, for sure, but sometimes some languages like VB, explicitly need those commas to properly delineate which parameter is missing!). Since you did not link to the documentation (and I can't find it on Adobe's scripting page) there really is not a way to know which format the Adobe API is expecting. However, there should be an explanation at the top of most documentation explaining the conventions used within.

So, this function could probably be used many ways :

fillPath() //Nothing passed
fillPath(#000000,RGB) // Black, in RGB mode
fillPath(#000000,RGB,50) // Black, in RGB mode, half opacity

//Now it gets tricky, this might ALSO be acceptable:
fillPath(#000000,50) // Black, no mode, half opacity

//OR
fillPath(#000000,,50) // Black, no mode, half opacity

Again, there usually are some standards across all documentations relating to API/programming. However in each doc, there could be subtle differences. As a power user, or developer, you ARE expected to be able to read and understand the documents/frameworks/libraries you're attempting to use.

robd
  • 9,646
  • 5
  • 40
  • 59
PenguinCoder
  • 4,335
  • 1
  • 26
  • 37
  • 6
    UNIX man page synopsis format link is dead -- anyone have a replacement link? @PenguinCoder Update: Based on [http://unix.stackexchange.com/questions/17833/understand-synopsis-in-manpage] (Unix Stack Exchange), It is loosely based on [https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form( EBNF) – steviesh Jun 04 '16 at 17:58
  • 1
    There is an [archived copy](http://web.archive.org/web/20140908051227/http://cm.bell-labs.com/cm/cs/who/dmr/manintro.html) of _man page synposis format_ – CodeManX Jul 17 '20 at 11:53
6

API docs for dynamically typed languages can be not very meaningful if not written carefully, so do not feel too bad about it, even the most seasoned developer can have a hard time trying to understand them.

About brackets and such, there is usually a code conventions section that should explain the exact usage outside literal examples; although EBNF, Regex and Railroad Diagrams are almost ubiquitous, so you should be familiar with them to understand most notations.

fortran
  • 74,053
  • 25
  • 135
  • 175
3

The brackets mean that the property is optional. Be aware though that if you want to set some property at the nTh rank, you have to either declare properties for the leading one or declare them as undefined :

fillPath() //good
fillPath ( someFillColor ) //good
fillPath ( someFillColor, mode ) //good
fillPath ( undefined, mode ) //good
fillPath ( mode ) //bad
fillPath ( undefined ) //really bad
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Loic Aigon
  • 506
  • 3
  • 1
  • 2
    `fillPath (mode)` might be ok. If an optional argument comes before a non-optional one it often means that the function is smart enough to detect if the optional argument was given or not (e.g. by looking at its type) – ThiefMaster Jun 08 '12 at 13:49
  • 1
    MMmm well that's possible but I prefer to rely on something strong than something the script might do for me :D – Loic Aigon Jun 11 '12 at 17:00
1

I had this same question a while back and somebody pointed me to Extended Backus–Naur Form.

It makes sense because programming can be used to create potentially limitless outcomes. The documentation can not display an example for every possible case. A good example of common use I helpful but once you can read the underlying syntax you are able to create your own code.

StevenKW
  • 19
  • 2