Re: We do not use C++ exceptions
Jeff Schwab <jeff@schwabcenter.com> writes
(but possibly has used another indentation):
/* Postcondition: The result is evenly divisible by 2. */
int times_two(int i) {
int const j = i + i;
assert(j % 2 == 0);
return j;
}
I see that in your post, there is a TAB character in front of
>return<, but spaces in front of >assert<. This might disturb
the visual appearance on some implementations of Usenet
message display systems. Possibly, this is a result of the
moderation process.
In the quotation above, I have replaced this TAB it with
spaces,
I remember that recently a post of mine was modified. I sent in:
|{ int i( 12 );
| int & r( i );
| ...
The article <references-20090123194355@ram.dialup.fu-berlin.de>
however appeared with:
|{ int i( 12 );
| int & r( i );
| ...
What wanted to write, when I hit the >reply< button, is this:
Jeff Schwab <jeff@schwabcenter.com> writes
(but possibly has used another indentation):
/* Postcondition: The result is evenly divisible by 2. */
int times_two(int i) {
int const j = i + i;
assert(j % 2 == 0);
return j;
}
Whenever all return statements of a function have the form
>return <variable>;<, I will name that variable >result<.
/* Postcondition: The result is evenly divisible by 2. */
int times_two(int i) {
int const result = i + i;
assert(result % 2 == 0);
return result;
}
I like your use of >const< and even would extend it to >i<:
/* Postcondition: The result is evenly divisible by 2. */
int times_two(int const i) {
int const result = i + i;
assert(result % 2 == 0);
return result;
}
Which conditions or assertions might one add?
/* Precondition: Twice the argument value can still be
represented as an int value.
Postcondition: The result is evenly divisible by 2. */
int times_two(int const i) {
assert i <= ::std::numeric_limits<int>::max() / 2;
int const result = i + i;
assert(result % 2 == 0);
return result;
}
C++ might still give a result if the precondition is not
fulfilled, but then the name of the function might be
misleading, because the result is not twice the value.
In order to be more precise, I would need an actual
specification of the intended behavior (including the result)
of this function.
(If one finds any TAB characters in the body of this post or
if the first character of the lines within the function body
of >times_two< does not appear in the same position as the
final brace, then the body of my post was altered after I have
sent it.)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]