Sun's guidelines for Java suggest that you declare the variables at the
top of the block, even though the language doesn't require it. Java
also doesn't support inline. It helps me to think of "setting up stack
frames," rather than "declaring variables," even when I realize that the
function I'm writing will probably be inlined automatically, and
therefore not require its own stack frame at all.
I agree with your implied point that one probably shouldn't be defining
variables before one is ready to initialize them. However, if you find
yourself declaring variables in the middle of a block, it may be
worthwhile to factor that portion of the block into a separate function,
anyway.
I don't often like what I find out about Java, but that guideline is
not
too bad in my opinion. I find it useful to be able to define
variables
before initializing them in marshalling code that is computer-
generated.
For this input:
msg_manager
(short, bool, bool)
}
I get this output:
// Generated by the C++ Middleware Writer version 1.8
#include <Buffer.hh>
struct msg_manager
{
inline
msg_manager() {}
inline
~msg_manager() {}
inline
void
Send(Buffer* buf, const short abt1, const bool abt2, const bool abt3)
{
unsigned char boolRep;
buf->Receive(&abt1, sizeof(short));
boolRep = abt2;
buf->Receive(&boolRep, sizeof(boolRep));
boolRep = abt3;
buf->Receive(&boolRep, sizeof(boolRep));
buf->SendStoredData();
}
inline
void
Receive(Buffer* buf, short& abt1, bool& abt2, bool& abt3)
{
unsigned char boolRep;
buf->PersistentRead(&abt1, sizeof(short));
buf->PersistentRead(&boolRep, sizeof(boolRep));
(boolRep != 0) ? abt2 = true : abt2 = false;
buf->PersistentRead(&boolRep, sizeof(boolRep));
(boolRep != 0) ? abt3 = true : abt3 = false;
}
};
I find it preferable to keep track of whether any bools are involved
and
only define boolRep if needed. The alternative would be to have
multiple
boolRep variables defined -- boolRep1 and boolRep2. One reason to
favor
the above approach is the amount of stack space needed is not
dependent
on how many bools are used. We do something similar for strings with
a
variable that holds string lengths.
I should add that the output above is not currently available on line.
The on line version is 1.7 and this newer version that supports both
exceptions and return codes will hopefully be available on the site
before the end of the month.
Brian Wood
Ebenezer Enterprises
www.webEbenezer.net