Top 20 NuGet big-endian Packages
4 stars based on
This is not binarywriter endian speculation: The request makes sense, simplifies the code, and is good all round — but it breaks substitutability and documented APIs. The substitutability principle is in some ways just common sense. It says in paraphrase that if your code works for some base type T, it should be able to work with subtype of T, Binarywriter endian.
This principle is at binarywriter endian heart of inheritance and polymorphism — I should be able to use a Stream without knowing the details of what its underlying storage is, for example.
Let q x be a property provable about objects x of type T. Then q y should be true for objects y of type S where S is a subtype of T. This is simply an equivalent to System. BinaryReaderbut it lets you specify the endianness to use when converting values. They have no inheritance relationship. The implementation is fairly straightforward — indeed, it means we can drop a bunch of methods from EndianBinaryReader and let BinaryReader handle it instead.
This is particularly handy for text, which is fiddly to get right. I currently have the code in another branch, and it works fine. More on that later. The second problem is that it binarywriter endian the documentation for BinaryReader. For example, the docs for ReadInt32 state: How much detail does it make sense to specify here? How much leeway is there for classes overriding Binarywriter endian Programming binarywriter endian often about nuance, even if we might wish that not to be the case.
Coming back to BinaryReader, I think unless I can be persuaded otherwise that binarywriter endian benefits from going against the documentation and strict substitutability outweigh the downsides. So, am I miles off track? Binarywriter endian is for a class library, after all — should I be more punctilious about playing by the rules? Or is binarywriter endian the more important principle here? Well, to me at least part of the question is unanswered in your article.
Is that really that huge binarywriter endian advantage? Binarywriter endian is that a benefit here? But, there should also be a strong win in doing so. Thanks for the comments Peter, and I do take your binarywriter endian.
Jon, I think your case at hand may actually be beside the point. The stream class in binarywriter endian case has CanSeek etc to try and help with the violations. I seem to remember reading an article with Joshua Bloch discussing the design of the collection classes in Java and how some method occasionally had to throw a not implemented exception.
The idea as to avoid combinational explosion on interfaces etc. As with all design there are tradeoffs. You can read from the stream, rewind the stream to the point you started at, read with Binarywriter endian and observe the result. In other words, when the underlying binarywriter endian is seekable you can just with public members determine the endianness.
Yes, I totally agree there are trade-offs. I think this is one of those areas where computer science binarywriter endian and pragmatism sometime clash… I just thought it might be interesting to look at one such case in a small amount of detail: I should be allowed to overload BinaryReader to provide a seamless experience for the downstream library I may not have the ability to change!
Now, whether or not this will cause headaches with a downstream. Just that by not sealing binarywriter endian classes MS has left them open to subclassing, meaning they at binarywriter endian very least invited somebody to subclass. Mistakes in the design of unsealed classes can most certainly force unfortunate trade-offs between OO principles and economic considerations.
The endian-ness of the BinaryReader really has to be specified in the documentation, otherwise it would useless. I binarywriter endian with Peter, there should have been an implementation-agnostic base class analogous to TextReader. Unfortunately that binarywriter endian has sailed.
I wonder if endian-ness had something to do with that? Someone somewhere has written code that depends on the endian-ness of BinaryReader, and someone somewhere is going to use EndianBinaryReader instead because after all it inherits binarywriter endian BinaryReader, so it is substitutable, right? I think the real problem is trying for theortic perfection, given an imperfect starting position. Second, the fact that you make use of some of the internals of BinaryWriter is an irrelevant implementation detail.
The fact binarywriter endian it can then be used in place of BinaryWriter, is a hack by the users of EndianBinaryWriter, not youmade necessary by the lack of IBinaryWriter, binarywriter endian that should have been using in place of BinaryWriter in the first place. Did the UK as well, or do European geeks hold out until the 3rd day of the 14th months? It works similarly to a DataReader, but it is not a subclass.
As you have said, it breaks the substitutability binarywriter endian. It is very easy to fix the model. Either put EndianDataReader alongside DataReader, so it performs a similar function, or turn it into an adapter, which contains a DataReader. Seeing this you can then see the problem with extending any implementation however. If methods are coded to take the interfaces then they make it clear that they delegate responsibility for maintaining the pairing to someone else.
Two very quick comments:. You have to use the same encoding for both of them for it to make sense. I have no problem with that. Will look at the rest tomorrow. Think about it this way: If someone wants little-endian behavior, they use the superclass. If they want big-endian behavior they use your new subclass. The old behavior is not desirable, the new behavior is desirable.
Implementing an entirely new class without inheriting from BinaryReader is wasteful and redundant and unnecessary. And also redundant and wasteful. Here are my thoughts after reading this post: The only way to make it interchangeable with anything is if that thing EndianBinaryReader inherits from it. Making it interchangeable is very recommended. But you should definitely do it, otherwise all code that could accept both would have binarywriter endian be duplicated, which is definitely a code smell.
A code smell generated by your class library. The contract says that it reads an Int32, and binarywriter endian internal least-surprise-o-meter says that it should read that Int32 in whatever endianness it was written in. The endianness of BitConverter is still documented, it is just documented at runtime instead of design time, which I would argue is actually less useful.
Its not binarywriter endian you have the option of changing the endianness to suit your needs. At least with BinaryReader, you know ahead of time which way it goes, and can binarywriter endian decisions appropriately. My point was, leaving the endianness unspecified for the sake of having a looser contract would make the class useless, since you would binarywriter endian know if it matches your stream. On the other hand, I can see Stephen A.
The question is, are libraries which consume BinaryReader likely to rely on its endianness? If they do, and you have a big-endian stream, then there is already a mismatch. Based on that, I am now inclined to say that deriving EndianBinaryReader from BinaryReader is the right thing to do although I still say that explicit documentation is necessary.
Binarywriter endian is the essential properties we need to worry about. Since the docs explicitly state that BinaryReader reads little-endian data, I would say it is an essential property. Name return the same thing on the subclass as it does on the superclass? Apologies if I duplicate someones answer, I think most views have been expressed. People rightly complain about the BinaryReader both declaring an interface non-sealed with virtual members that very well might be implemented in more than one way and implementing a specific behaviour.
BinaryReader, IBinaryReader, with no overrides. Code that is using BinaryReader now can be easily be refactored to use the interface instead, third party libraries that take a BinaryReader should not be trusted to do the right thing if given an binarywriter endian different from the document behaviour, so not binarywriter endian able to pass yours to them is no loss.
The only case not covered as nicely as could possibly be is when you receive a BinaryReader from third party code. If this really bothers you, I guess you should let LittleEndianBinaryReader wrap a BinaryReader instead of deriving from it, with an implicit cast in either direction.
After all, the docs explicitly state that there are X properties and X methods, too! Not to mention that much of what is documented in MSDN are implementation details, not contract requirements. Unfortunately I think this comes down to a judgment call. Binarywriter endian is precisely the case that is most interesting when discussing LSP and inherited behavior. If you toss this case out the window, none of the rest of it really matters. The question is, should a library X define a class which may break the contract of the supertype, but which otherwise has significant benefits, given that the user of X may want to pass an instance of that class to library Y?
Also, are you sure the number of methods and properties are explicitly stated, not implicitly as in: If so, then the docs are of questionable quality indeed! For binarywriter endian, suppose BinaryReader decided to read an Int32 by reading two bytes twice — how would I know that was binarywriter endian and do the appropriate reversal?
A stream is just a sequence of bytes — it only has endianness when you impose some sort of structure on binarywriter endian from above.