views:

542

answers:

10

Are ASCII diagrams within source code worth the time they take to create?

I could create a bitmap diagram much faster, but images are much more difficult to in line in a source file (until VS2010).

For the record, I'm not talking about decorative ASCII art.

Here's an example of a diagram I recently created for my code that I probably could have constructed in half the time in MS Paint.

          Scenario A:

                          v
 (U)____________(N)_______<--(P)                   Legend:
          '     /             |                    J = ...
          '    /              |                    P = ...
          '   /d              |                    U = ...
          '  /                |                    v = ...
          ' /                 |                    d = ...
          '/                  |                    N = ...
         (J)                  |
          |                   |
          |___________________|
+5  A: 

Your time would be better utilized writing clear and concise code with descriptive variable and function names that does not require comments to understand. Neither comments nor design documents compile. As a result, they quickly fall out of sync with the code and become misleading.

Tom Cabanski
However, with graphics and geometric algorithms, a picture really does say more than a thousand words (or lines of code).
Thomas
Some things are very difficult to express clearly in words, let alone concisely. For example, non-trivial algorithms and graphical routines are often better explained graphically.
Kramii
In real production code that is actually pretty rare. It is usually expected that the people working on the code are familiar with the problem domain. It is not the responsibility of comments or design docs to teach them the problem domain. For example, I would expect the reader of code that handles 2D graphic transforms in an imposition library to understand how 2D graphic transforms work. I would not put together a document explaining it as part of my code or design.
Tom Cabanski
Even if everyone on the team is familiar with the domain, we can't expect everyone to be able to read code like `const double c = P*P - 2.0*P*J + J*J - d*d` and know what is going on. Geometric algorithms need geometric explanations. The easiest way to do that is through a visual aid.
Jesse Stimpson
+1  A: 

Try this: nice library for putting these together dynamically. http://code.google.com/p/clojure-textflow/

Matt Andrews
That only covers a very limited subset of diagrams that you might want to include in source code though.
Paul R
There are other tools that might help, e.g. http://www.jave.de/
Kramii
@Kramii, very cool app. Thanks for the tip
Jesse Stimpson
+1  A: 

Then use MS Paint (or whatever) and include the diagram in your source control.

Kramii
+1  A: 

Design documentation belongs in a design document. Why not have a project folder with subfolders for drawings, manuals, source, example data files, test cases, wish lists, change logs, etc. I'm not saying that each document type needs a separate directory, but things should be organized logically.

Time to open the external file is not an issue next to the time wasted to make the ASCII art, and how quickly it falls apart when someone uses a different editor or font.

Ron
+1  A: 

Know your audience. Will your audience (other developers in the future) be able to access the bitmap simply and easily with the tools at their disposal? Is the diagram useful/helpful?

An ASCII diagram in the comments of the code can almost always be viewed easily. (Yes, there may be some issues if the extended ASCII characters were used, or if the developer is using a non-fixed width font.)

Sparky
if you code with a non-fixed font you're weird to say the least :P
Lo'oris
@Lo'oris, I'd have to agree. However, it still may be common for code to be viewed with a non-fixed width font. Our version of ClearCase's comparison tool uses a non-fixed font by default. This makes everything ugly as sin.
Jesse Stimpson
A: 

I agree with most other posters: anything which is not code (and a bit of comments, but not too verbose) should be somewhere else, be it a wiki or a document.

I'd prefer to avoid Word due to various mishaps I suffered in the past but this could be personal preference. Just one more thing: try to have good names for stuff and use them as a link between your code and your docs. So if you have a class (or a routine, or a module) who deals with scenarios like the one you showed as an example, please call it SquareBisector or something like this, and have its methods be scenarioA(Point a, Point b), scenarioB(Point a, Line l1) and so on, and then write the documents explaining them in more high-level terms, with plenty of diagrams, in a document using a consistent terminology.

Please don't call your methods "bisectWithTwoPoints(Point firstPoint, Point secondPoint) in the code and "Scenario A" in the documentation...

p.marino
+6  A: 

Have you looked into the various ASCII art converters out there? That way you can draw quickly in paint or whatever and then have it export ASCII art.

jamone
+6  A: 

If you use a tool to generate documentation from your code, such as Doxygen, there should be a way to reference an image file directly and have it appear in the generated docs. For Doxygen, the relevant command is \image. This combines the benefits of having an actual image diagram with ease of reference from the source (no need to fire up a heavy program like Word), and also with your auto-generated docs.

rmeador
+1  A: 

You could get everyone on your team to Use VS2010 and just embed actual images into the source file.

Eclipse
Yes I'm familiar with that awesomeness. Sadly this isn't an option for us right now, but we're working toward it.
Jesse Stimpson
+2  A: 

Sometimes an ASCII diagram really is worth a thousand words. But not very often. I won't go search all my source files, but I'll guess one diagram per 20,000 lines of code might be about right (or at least not off by more than a factor of two).

Anybody who suggests putting the code in one place and the diagram in another is just begging for the two to become inconsistent. Better to have no diagram or a crappy ASCII diagram than a separate Word document that is wrong.

Norman Ramsey