Hacker News new | ask | show | jobs
by wycy 1595 days ago
Do you have an example context in which you'd use these? I've never thought to do this and am curious about the possibilities and use cases.
3 comments

Sure, last time I used them I documented some file format I needed to reverse engineer.

Here an excerpt. Of course, it's not _necessary_ to do it like that. It's just flavor.

    ┏━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┳┅┅
    ┃ Chunk 1 Header │ Chunk 1 Body ┃ Chunk 2 Header │ Chunk 2 Body ┃
    ┗━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┻┅┅

    Chunk Header:
    ┌───────────┬─────────────────┐
    │ Magic Nr. │ Chunk Body Size │
    │   4 Byte  │          4 Byte │
    └───────────┴─────────────────┘
Biggest problem is with mobile / low horizontal resolution displays. I don't mind scrolling left and right, but in some cases the wrapping is forced (as on the HN phone app I'm using, Materialistic) and it becomes a mess. I've longed for some kind of markup system that allows for textual data to be formatted in tabular fashion irrespective of the character size of the contents, like ummmmmm HTML
It's fabulous in cases where there's a "big important business logic" or a "big important test" with tough to eliminate complexity, where you feel a diagram is so important that it's worth putting in comments beside said code. I do recommend that you be careful with this though; in places where it's not common to put ASCII art diagrams in code you'll probably receive pushback (it is afterall very large and distracting compared to said code). Be ready to save an in-code diagram for the 1-to-3 places in the business where they'll be a godsend.
It's also extremely useful for bit-mapped registers in embedded code.

EG the TI bq25155[1] battery charge controller PCHRGCTRL register (page 52) is divided into 3 fields. It can be quite helpful to show the layout before code to set the range.

   /*
    * ┌───────────────┬──────────┬─────────┐
    * │ ICHARGE_RANGE │ RESERVED │ IPRECHG │
    * │      7        │   6-5    |   4-0   |
    * └───────────────┴──────────┴─────────┘
    * This function sets the precharge current
    *  and fast-charge current step size the
    *  nearest 1.25mA (<= 318.75mA) or 
    *  2.5mA (<= 500mA).
    */
etc, etc.

[1] https://www.ti.com/lit/ds/symlink/bq25155.pdf

Basically I use the same kind of philosophy as RFCs, and document things using ascii art where some kind of diagram makes sense. For example, see the TCP RFC https://datatracker.ietf.org/doc/html/rfc793 where they use it for packet layout, flow diagrams, block diagrams, etc. It makes the whole thing self contained, you can copy paste diagrams to almost anywhere, easy to modify. Disadvantage is in some more complex diagrams it can get noisy
RFCs are now allowed to contain SVGs though.