Re: Re: SourceComment: Removing code duplication in BinaryWriter

Dear Peter

In bold ;-)

Peter Dimov wrote:

Dear Atul,

I looked into your patch and I see two issues with using the template function.

The major one is that the template function would allow one to write something like:

Ping packet;
Writer.write (ping);

We can specialize, in such cases …
The second issue is that when one sees code like:

writer.write (payloadLength);

one does not know how many bytes are actually written.

That could be desirable, as a encapsulation principle… I can write an object and I can read it back…

One just has to look at the type of the object to know how many bytes get written.

 Furthermore the object should write its innards ( like the streaming idiom …. ;-) so it is better encapsulated. 
So if someone is inspecting the function writeHeader() it will not be obvious if the binary format is correct.

It will almost always be correct as the compiler writes it out - and when it is not ( like the above  Ping Packet, then Unit Testing will point it out ;-)
One of the philosophies of the Python community ( http://en.wikipedia.org/wiki/Python_philosophy is “explicit is better than implicit” and I think that is especially true in our specific case.

I am not too sure this case qualifies for applying that rule ;-) 

The explicit keyword is more in keeping with the above principle, IMHO ;-)
IMHO for BinaryWriter it would be more suitable to apply the same kind of refactoring that you already performed in BinaryReader.

Yes ;-) There are some more thoughts I will pen in a separate posting ;-)

Let me know what you think… ( Even if we end up not doing this refactoring, it helps us explore the
design space ;-)

— cheers atul
Best regards,

Peter

atul wrote:Dear all

BinaryWriter has a number of methods that differ only in type.

writeUInt32, writeInt32, writeUInt16 etc..

One C++ idiom is to let the compiler write these versions with template member functions….

Here is the patch that does this and as a result a lot of repetitive functions get eliminated…

The advantage is if we change some class member from char to int, this change is automatically propogated…. So we do not have to go manually and change things..

Let me know….

— cheers atul

Would you like to post a relpy?


This post is a reply to:
Re: SourceComment: Removing code duplication in BinaryWriter
Dear Atul, I looked into your patch and I see two issues with using the template function. The major one is that the template function would allow one to write something like: Ping (more...)

Follow-ups:
Re: Re: Re: SourceComment: Removing code duplication in BinaryWriter
Dear Atul, I see you are thinking of BinaryReader/BinaryWriter as kind of stream classes. When I created them my intention was NOT that they become another QDataStream. Let me summarize my (more...)