R
Richard Heathfield
<snip>
In a long reply, pete said a great deal on which I have no particular
comment to make, but one point certainly bears further discussion:
Absolutely right. But sometimes they *do*. I want to move straight into
the general case here - and do I hear a sigh of relief from MM?
Code fragments come in all manner of shapes and sizes, and are provided
for all sorts of reasons. I think the smallest (visible) code fragment
I've ever written is <code>.</code> - when describing the structure
member operator. Clearly, that fragment is not intended to be compiled
separately!
Generally, if a code fragment is written inline in a paragraph, it is
unreasonable to expect it to compile. For example, I have recently
written the following in an HTML page: "The declarator, <code>int
main(void)</code>, tells the compiler that <code>main</code> is a
function that returns a value of type <code>int</code>." I do not
expect people to expect that int main(void) will compile all by itself!
When a particular line (or short group of lines) of code is being
discussed, again it is unreasonable to expect it to compile:
"But we can use a <code>for</code> loop to make the job a lot easier:
</p>
<pre><code>
for(i = 0; i < 64; i++)
{
putchar('-');
}
</code></pre>
<p>
This is of course the equivalent of:
</p>
<pre><code>
i = 0;
while(i < 64)
{
putchar('-');
i++;
}
</code></pre>"
If an entire function is given, however, then it /is/ reasonable to
expect it to compile, provided that any relevant information supplied
earlier in the discussion is made available to the compiler (e.g.
headers, declarations for called functions, and so on). The obvious
exception is when we get something like:
int foo(void)
{
auto i = 42; /* perhaps this is what we're trying to talk about */
.
.
.
return i;
}
Here, the three lines of dot tell an eloquent story - "stuff goes here"
- and we recognise that this is a device for generalising a function
that, in its present form, is not intended to be compiled.
All of the above are suitable for expositors, didacts, and exegetes to
employ during their expositions, didacticisms, and exegeses.
When one is seeking help, however ("my program is broken, please help,
here's the code"), it is normally essential to provide a complete,
minimal, compilable program - together with enough information to allow
easy reconstruction of test data.
It is also necessary for authors - not just book authors but also online
tutorial writers - to provide complete, compilable, compilED, and
TESTED versions of the code in their books. If this is not done via
some kind of side-channel (Web site, CD in the back of the book, or
whatever), then it must be done in the book itself. If the side-channel
is employed, the abridged book code should be annotated with an
explanation of where the full version can be found (or a general remark
covering this point should be given near the front of the book).
Did I miss anything obvious?
In a long reply, pete said a great deal on which I have no particular
comment to make, but one point certainly bears further discussion:
It depends on whether or not you want to consider the
published function definition as a code fragment.
Code fragments don't always have to be compilable.
Absolutely right. But sometimes they *do*. I want to move straight into
the general case here - and do I hear a sigh of relief from MM?
Code fragments come in all manner of shapes and sizes, and are provided
for all sorts of reasons. I think the smallest (visible) code fragment
I've ever written is <code>.</code> - when describing the structure
member operator. Clearly, that fragment is not intended to be compiled
separately!
Generally, if a code fragment is written inline in a paragraph, it is
unreasonable to expect it to compile. For example, I have recently
written the following in an HTML page: "The declarator, <code>int
main(void)</code>, tells the compiler that <code>main</code> is a
function that returns a value of type <code>int</code>." I do not
expect people to expect that int main(void) will compile all by itself!
When a particular line (or short group of lines) of code is being
discussed, again it is unreasonable to expect it to compile:
"But we can use a <code>for</code> loop to make the job a lot easier:
</p>
<pre><code>
for(i = 0; i < 64; i++)
{
putchar('-');
}
</code></pre>
<p>
This is of course the equivalent of:
</p>
<pre><code>
i = 0;
while(i < 64)
{
putchar('-');
i++;
}
</code></pre>"
If an entire function is given, however, then it /is/ reasonable to
expect it to compile, provided that any relevant information supplied
earlier in the discussion is made available to the compiler (e.g.
headers, declarations for called functions, and so on). The obvious
exception is when we get something like:
int foo(void)
{
auto i = 42; /* perhaps this is what we're trying to talk about */
.
.
.
return i;
}
Here, the three lines of dot tell an eloquent story - "stuff goes here"
- and we recognise that this is a device for generalising a function
that, in its present form, is not intended to be compiled.
All of the above are suitable for expositors, didacts, and exegetes to
employ during their expositions, didacticisms, and exegeses.
When one is seeking help, however ("my program is broken, please help,
here's the code"), it is normally essential to provide a complete,
minimal, compilable program - together with enough information to allow
easy reconstruction of test data.
It is also necessary for authors - not just book authors but also online
tutorial writers - to provide complete, compilable, compilED, and
TESTED versions of the code in their books. If this is not done via
some kind of side-channel (Web site, CD in the back of the book, or
whatever), then it must be done in the book itself. If the side-channel
is employed, the abridged book code should be annotated with an
explanation of where the full version can be found (or a general remark
covering this point should be given near the front of the book).
Did I miss anything obvious?