Here is some perspective on examples on package.elm-lang.org!
The Dict documentation starts with a nice example:
import Dict exposing (Dict)
users : Dict String User
users =
Dict.fromList
[ ("Alice", User "Alice" 28 1.65)
, ("Bob" , User "Bob" 19 1.82)
, ("Chuck", User "Chuck" 33 1.75)
]
type alias User =
{ name : String
, age : Int
, height : Float
}
That looks like typical Elm code to me! Nice.
If you then scroll down a bit, you come to Dict.get:
animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
get "Tom" animals == Just Cat
get "Jerry" animals == Just Mouse
get "Spike" animals == Nothing
This does not look like any typical Elm code in the wild!
In my opinion, examples should show the reader how to write their code. Otherwise they are bad examples.
I have written Elm for a couple of years now, but I do remember struggling with this as a beginner. After a while I learned to go into “documentation mode” where you mentally add for example Dict.
in front of all “loose” functions you see. I still had to watch out for the default imports though.
Looking at examples and seeing patterns is what my brain is really good at, so good examples are important to me. Having to mentally translate the examples I see does not help. @aion on Slack agrees.
Here are some links that mention things that, to me, should apply to documentation examples and recommend them to use qualified imports:
- docs/syntax
- Documentation Format
- package.elm-lang.org/assets/help/design-guidelines.md at a356a2d485bc521cfaeeafc84b17aa6997ee416c · elm/package.elm-lang.org · GitHub
Qualified imports are preferred.
There is an example that shows a typical use of the function.
Write helpful documentation with examples
Providing examples of common uses is extremely helpful. Do it! The standard libraries all make a point to have examples that show how one should be using those functions.
Also, make the documentation for the library itself helpful. Perhaps have an example that shows how to use many functions together, showcasing the API.
(Bold emphasis mine.)
I think there are cases where unqualified use is acceptable. Such as Html
and Html.Attributes
. Or elm/parser. In such cases I think the docs should use unqualified functions as well. Show the recommended use of the package.
@jfmengels mentioned on Slack that he uses qualified functions in docs for his packages, since he finds it clearer.
@ianmackenzie (who writes great documentation for his packages) seems to also use qualified functions in his examples. For instance: Length - elm-units 2.10.0
What do you think?