# Preferred example style in documentation?

**URL:** <https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648>\
**Category:** Request Feedback\
**Created:** [November 29, 2018, 8:11pm UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648 "2018-11-29T20:11:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianmackenzie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ianmackenzie/32/86_2.png) [@ianmackenzie](https://discourse.elm-lang.org/u/ianmackenzie)\
**Post date:** [November 29, 2018, 8:11pm UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/1 "2018-11-29T20:11:26Z")

</div>

I’m at a point where I am going to have to update a ton of documentation examples for my `elm-geometry` package, and I’m wondering if this is an opportunity to tweak the example style to something more concise/readable. I’d love to get some feedback on what kinds of documentation examples are most useful!

## Current style

Currently I’ve been trying to make examples complete and standalone (that you could basically just type into the REPL and have work), for example

> [@](#):
>
> Rotate a point around an axis by a given angle (in radians).
> 
> ```auto
> point =
> Point3d.fromCoordinates ( 3, 1, 0 )
> 
> Point3d.rotateAround Axis3d.x (degrees 45) point
> --> Point3d.fromCoordinates ( 3, 0.7071, 0.7071 )
> 
> ```

However, this gets a bit unwieldy with more complex data structures - for example in my `Arc3d` module I have a example arc defined near the top of the docs:

> [@](#):
>
> ```auto
> exampleArc =
> Point3d.fromCoordinates ( 1, 1, 0 )
> |> Arc3d.sweptAround Axis3d.z (degrees 90)
> 
> ```

This then gets used to provide ‘complete’ examples for various functions:

> [@](#):
>
> Rotate an arc around a given axis by a given angle (in radians).
> 
> ```auto
> Arc3d.rotateAround Axis3d.x (degrees 90) exampleArc
> --> Arc3d.sweptAround (Axis3d.reverse Axis3d.y)
> --> (degrees 90)
> --> (Point3d.fromCoordinates ( 1, 0, 1 ))
> 
> ```

It’s annoying to have to go back and look at the definition of `exampleArc` each time, and the output is a fair bit of text to parse/interpret.

## Proposed new style

I’m thinking of relaxing the ‘standalone’ requirement in favor of something shorter and more conversational, like:

> [@](#):
>
> Rotate a point around an axis by a given angle (in radians). To rotate a point a quarter turn around the Z axis, you might write
> 
> ```auto
> rotatedPoint =
> Point3d.rotateAround Axis3d.z (degrees 90) originalPoint
> 
> ```

(In this case I wouldn’t actually define `originalPoint` or show the resulting coordinates of `rotatedPoint`). Similarly,

> [@](#):
>
> Rotate an arc around a given axis by a given angle (in radians). To rotate an arc 180 degrees around the X axis, you might write
> 
> ```auto
> rotatedArc =
> Arc3d.rotateAround Axis3d.x (turns 0.5) originalArc
> 
> ```

## Thoughts?

Is it useful to see fully-worked examples with actual numeric values in the docs, or is it enough just to have a description and example of the syntax, and fall back to `elm repl` or Ellie if you want to see the details? Are there other styles that are even better?

---

<div class="post-metadata">

**Author:** ![Libbum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/libbum/32/1311_2.png) [@Libbum](https://discourse.elm-lang.org/u/Libbum)\
**Post date:** [November 30, 2018, 9:49am UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/2 "2018-11-30T09:49:31Z")

</div>

I would say that a simple argument against this is that a package author knows exactly what will happen when one of these examples is fired, whereas a user does not.

In your examples it may be extremely clear to you that `originalPoint` is of type `Point3d`, but that’s now implicit. The old example told me that, and if I were to use the new example only, maybe I would have tried `( 3, 1, 0 )` or `[3, 1, 0]` or `{ x = 3, y = 1, z = 0 }` or something else before getting notified of an error at compile time. Perhaps you also just don’t explain the function in a way that everyone understands, but at the very least you give them an expected result they can play with. Not everyone is good a writing documentation, so I usually find playing with known results is the way I come to understand a new library rather than reading function docs.

**Edit:** Additionally, I really like your comment styles, even the multiline examples.

Initially, I was using

```elm
greedy [4, 5, 7, 6, 8] == ( [6, 7], [4, 5, 8] )

```

as a style choice, but this is only OK for extremely simple examples.

```elm
greedy [4, 5, 7, 6, 8]
--> ( [6, 7], [4, 5, 8] )

```

is far better and extensible in my opinion, so I changed them a few months ago because of `elm-geometry`'s docs.

I guess what you’re trying to do is simplify your examples so that they don’t look too intimidating. To me, they’re already terse enough and explain a complex library well—oversimplification will just lead to confusion.

---

<div class="post-metadata">

**Author:** ![ianmackenzie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ianmackenzie/32/86_2.png) [@ianmackenzie](https://discourse.elm-lang.org/u/ianmackenzie)\
**Post date:** [November 30, 2018, 2:38pm UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/3 "2018-11-30T14:38:04Z")

</div>

Thanks for the thoughtful comments! General simplification is part of it, but another big part is that writing out things like `Point3d.fromCoordinates` will soon become even _more_ verbose. I’m in the middle of updating `elm-geometry` to be based on `elm-units`, so

```auto
Point3d.fromCoordinates ( 3, 1, 0 )

```

will soon need to be

```auto
Point3d.fromCoordinates ( meters 3, meters 1, meters 0 )

```

or perhaps (if you’re being scrupulous about using qualified imports) even

```auto
Point3d.fromCoordinates ( Length.meters 3, Length.meters 1, Length.meters 0 )

```

In examples where everything has to be built up from scratch each time, this will add a lot of verbosity.

I wonder if perhaps there’s a compromise where I have an extended “full” example in the intro to each module that shows examples of a few different functions, something like

```auto
import Point3d
import Length exposing (meters, centimeters)

point =
    Point3d.fromCoordinates ( meters 3, meters 1, meters 0 )

Point3d.distanceFrom Point3d.origin point
--> meters 3.162

displacement =
    Vector3d.fromComponents ( centimeters 5, centimeters 0, centimeters 10 )

point |> Point3d.translateBy displacement
--> Point3d.fromCoordinates ( meters 3.05, meters 1, meters 0.1 )

```

and then have individual function examples in the ‘compact’ style.

---

<div class="post-metadata">

**Author:** ![Libbum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/libbum/32/1311_2.png) [@Libbum](https://discourse.elm-lang.org/u/Libbum)\
**Post date:** [November 30, 2018, 3:04pm UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/4 "2018-11-30T15:04:55Z")

</div>

Ah, I see what you’re getting at.

If you define a small set of helpers that you use all the way through the docs, then yeah—that will be a pretty good compromise.

Particularly with the way `elm-geometry` is set-up, you have a bunch of primitives (`Point`, `Axis`, `Frame` _etc_) that are then operated on (`midpoint`, `centroid` _etc_). Having a good understanding of how the primitives are constructed initially, then how they are operated on, seems to be the natural steps a user would want to absorb the package.

---

<div class="post-metadata">

**Author:** ![ianmackenzie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ianmackenzie/32/86_2.png) [@ianmackenzie](https://discourse.elm-lang.org/u/ianmackenzie)\
**Post date:** [November 30, 2018, 10:27pm UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/5 "2018-11-30T22:27:40Z")

</div>

I don’t think I’d actually want to _reuse_ anything from the initial ‘full’ example in the rest of the docs - I find it annoying that to understand a particular function’s example you sometimes have to scroll back to the top of the page to see what it’s referring to, so I’d like to avoid that if possible.

I was hoping that an initial extended example would accomplish the goals of

- having something you can play around with in a REPL
- forming a rough mental model of the module (`Point3d` is an opaque type, there are functions to construct/query/transform points, etc.)

such that individual function examples could then be brief while still being understandable. (If you’ve seen a fully-worked example of `rotateAround`, then you probably don’t also need to see a fully-worked example of `translateBy` or `mirrorAcross`, since they work very similarly.)

---

<div class="post-metadata">

**Author:** ![Libbum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/libbum/32/1311_2.png) [@Libbum](https://discourse.elm-lang.org/u/Libbum)\
**Post date:** [December 1, 2018, 7:07am UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/6 "2018-12-01T07:07:12Z")

</div>

That’s what I was trying to get at for the most part, so I think we’re on the same page with your idea. I’d be happy to take a look again when you’ve fleshed out the plan a bit more and see if it still makes sense to me then. Perhaps ping me on github? `@Libbum`

---

<div class="post-metadata">

**Author:** ![mgold](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mgold/32/284_2.png) [@mgold](https://discourse.elm-lang.org/u/mgold)\
**Post date:** [December 2, 2018, 7:59am UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/7 "2018-12-02T07:59:03Z")

</div>

I’m not sure if everyone knows that the `-->` syntax is used by [elm-verify-examples](https://github.com/stoeffel/elm-verify-examples) to create tests that confirm that your docs snippets actually evaluate to what you claim they do. It’s a very useful tool and I recommend using it.

In my opinion, as long as you can make elm-verify-examples happy, you’re doing ok. If there are multiple variations that it accepts, use your best judgement and pick the one that is most clear.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [December 12, 2018, 7:59am UTC](https://discourse.elm-lang.org/t/preferred-example-style-in-documentation/2648/8 "2018-12-12T07:59:11Z")

</div>

Closed on December 12, 2018, 7:59am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
