Re: unit testing C++ code from perl



Nick Keighley wrote:

what! Its *good* to have no comments?! Not even a one liner saying what
it's
*for*?

Ahem.

void ReportAssert(char const* description, char const* filename, int const
lineNumber)

Oookay. We have a function called ReportAssert, and it takes a
description. That's probably the complaint string for an error. "The
frob doesn't fit its Wibble socket", or whatever.

Then we have a filename and lineNumber. They are probably just the same as
the filenames and lineNumbers we have seen ever since we were little
kiddie-programmers.

Oh, and we didn't get creative with the names, like fileN or lNumber.
Those would have required comments. Like fileN // this is the filename,
and lNumber // this is the line number.

Let's try another one.

template<typename ValueType>
void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value)
{
char txt[32];
std::sprintf(txt, format, value);
stream << txt;
}

Hmm. That seems to format any primitive value as a string, then insert
it into a string. It probably wraps the "%2.0i" format flags that sprintf()
is famous for. And we are not production code, so we don't need
snprintf() or something more over-flow safe. But any comment I wrote
describing everything going on would be bigger than the function!

If you ask "what about the hard, complex methods?" that's the point -
there are none. All the functions are short, cheap, narrow, and obvious
like these. (There are those who claim unit testing helps generate such
code!)

Treat all comments as a failed opportunity to improve the code's clarity.

BTW I wrote a tutorial for UnitTest++, here:

http://wiki.orbzone.org/TestFirstCiao

Disregard the CIAO stuff. It would need ... lots of comments!

--
Phlip
.



Relevant Pages

  • RE: Hyperlink Multiple Files
    ... filename itself, not a path or anything, just filename like MyFile.xls or ... You'll need to change a couple of the Const values in the code to match the ... open your workbook and right-click on the Excel icon just ... Dim partialPath As String ...
    (microsoft.public.excel.misc)
  • Re: unit testing C++ code from perl
    ... void ReportAssert(char const* description, char const* filename, int const ... That's probably the complaint string for an error. ... Then we have a filename and lineNumber. ... void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value) ...
    (comp.programming)
  • RE: Hyperlink Multiple Files
    ... filename itself, not a path or anything, just filename like MyFile.xls or ... You'll need to change a couple of the Const values in the code to match the ... open your workbook and right-click on the Excel icon just ... Dim partialPath As String ...
    (microsoft.public.excel.misc)
  • Tricky exception handling question
    ... I can't change the error message in a const ... Options::Options (void) throw{ ... void Options::addOpt (char const shopt, char const* lopt, argreq const arg) { ... int const numopts) { ...
    (comp.lang.cpp)
  • Re: A question on variable defination
    ... nmriopen(char *const filename, const int np1, const float step1, ... For the pointer argument things are a little different, ...
    (comp.lang.c)