> Also, tt is better to use constants with meaningful names, instead of
> magic values.
[quoted text clipped - 4 lines]
> ...
> cls.write_4bytes(START_OF_CHICKEN_SECTION, CHICKENS_GO_MOO);
But those values must be known in other modules (embedded system
attached to the serial port), written in C.
> Also, I think write_4bytes is miss named if it only takes two bytes :-)
No, acually 4 bytes are written, a start byte (not specified in the
function call), two given data bytes and a checksum byte (not given,
but calculated)
> Anyway, hope this helps.
> GL
I knew the trick of int-arguments but hoped there would be another
solution. Now I know there isn't.
Thanks
Daniel Pitts - 19 Dec 2006 18:43 GMT
J?rgen Gerstacker wrote:
> > Also, tt is better to use constants with meaningful names, instead of
> > magic values.
[quoted text clipped - 20 lines]
> solution. Now I know there isn't.
> Thanks
I still think the name method is misnamed. Sure it writes four bytes,
but it might be better to describe what that means to someone who
doesn't necessarilly know the ins and outs of the protocol.
sendCommand, maybe.
Also, even if your values have to be known in a different module all
together, it is still good to have named constants in the Java code.
You can have similar #define directives in the C code to clarify THAT
code. There is often very few reasons to have unlabeled numbers just
sitting around in code.
In any case, depending on the use of your write_4bytes method, you
might be better to have a single integer as the parameter. If the byte
values are always paired up (hard coded), then instead of write4b(0xF0,
0x4D), you would write4b(0xF04D) (or 0x4DF0, depending on how you
endianed it)
Oh, and if you happen to have methods like write_5bytes and
write_3bytes, it might be even better to take an array of bytes and
just call it write instead. Assuming you're java isn't embeded (I don't
know the implications if it is embeded), you're overhead for using
arrays should be small.
If you're using java 1.5, you could even use the varargs approach.
public void write(byte...bytes) {
writeFirstByte();
writeAllBytes(bytes);
writeChecksum(bytes);
}
Ohwell, I'm doing a lot of speculating for not knowing your domain. :-)
But I hope I've given you some ideas that might make your design better
in the end.