When Poor Design Gets Baked Into a File Format
-
Found this nugget in the constructor of a class that’s supposed to represent a structure in a binary file:
var isNamed = reader.ReadBoolean(); // This byte indicates whether the object has a name.
if (isNamed) // If the object has a name, read the remaining bytes.
{
var nameLength = reader.ReadByte(); // This byte indicates the length of the name.
this.Name = new string(reader.ReadChars(nameLength));
}The property setter for ``Name`` checks the length of the string—and it forbids zero-length strings. :wtf: Wouldn’t a name length value of ``0`` suffice to indicate the absence of a name?
-
Found this nugget in the constructor of a class that’s supposed to represent a structure in a binary file:
var isNamed = reader.ReadBoolean(); // This byte indicates whether the object has a name.
if (isNamed) // If the object has a name, read the remaining bytes.
{
var nameLength = reader.ReadByte(); // This byte indicates the length of the name.
this.Name = new string(reader.ReadChars(nameLength));
}The property setter for ``Name`` checks the length of the string—and it forbids zero-length strings. :wtf: Wouldn’t a name length value of ``0`` suffice to indicate the absence of a name?
The only reason i can think of for this is that Name can be specified but empty, which seems silly. I wonder if there was a design issue further downstream that required support for empty names?
Real programmers use butterflies
-
The only reason i can think of for this is that Name can be specified but empty, which seems silly. I wonder if there was a design issue further downstream that required support for empty names?
Real programmers use butterflies
Or
ReadBoolean
masks a bit and they put the other 7 bits to good use. :laugh:Robust Services Core | Software Techniques for Lemmings | Articles
-
Or
ReadBoolean
masks a bit and they put the other 7 bits to good use. :laugh:Robust Services Core | Software Techniques for Lemmings | Articles
If that were the case I would be skinning someone come code review time.
Real programmers use butterflies
-
If that were the case I would be skinning someone come code review time.
Real programmers use butterflies
But but but...it's a binary file. :((
Robust Services Core | Software Techniques for Lemmings | Articles
-
Found this nugget in the constructor of a class that’s supposed to represent a structure in a binary file:
var isNamed = reader.ReadBoolean(); // This byte indicates whether the object has a name.
if (isNamed) // If the object has a name, read the remaining bytes.
{
var nameLength = reader.ReadByte(); // This byte indicates the length of the name.
this.Name = new string(reader.ReadChars(nameLength));
}The property setter for ``Name`` checks the length of the string—and it forbids zero-length strings. :wtf: Wouldn’t a name length value of ``0`` suffice to indicate the absence of a name?
Database people have been discussing variants of non-existing, empty and zero-length strings for at least 30-40 years. There is a difference between not knowing whether a value for the attribute exists, knowing positively that it is void, and knowing that there is a defined value, but it is of zero length. The strangest passport I ever saw was for the son of friends of mine: It stated birthdate as unknown, sex unknown, name unknown, no photo or fingerprint available. When the mother, US citizen living in Norway, is pregnant with a baby, and plans a trip abroad with the baby a few weeks after the delivery, you must start the process of applying for a passport before those details are known. So for this baby's middle name, before his birth, nothing was known, not even if he would have a middle name at all. If he has one, I do not know it, so I could treat it as a defined name attribute with unknown value. If I learn that he certainly does not have a middle name, it would be a known value of zero length. There may be better examples to illustrate various null/empty/… values; this is just the first one dropping into my mind. There may be cases where there is no semantic difference between optional omitted, included but unknown, included with a known length of zero and included with a positive length value. Yet there are other cases where the semantic difference may be very significant.
-
But, if
nameLength
is zero, this means thatisNamed
istrue
. A man has no name. That's why."Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
Valar Morghulis! :)
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Found this nugget in the constructor of a class that’s supposed to represent a structure in a binary file:
var isNamed = reader.ReadBoolean(); // This byte indicates whether the object has a name.
if (isNamed) // If the object has a name, read the remaining bytes.
{
var nameLength = reader.ReadByte(); // This byte indicates the length of the name.
this.Name = new string(reader.ReadChars(nameLength));
}The property setter for ``Name`` checks the length of the string—and it forbids zero-length strings. :wtf: Wouldn’t a name length value of ``0`` suffice to indicate the absence of a name?
mmmm... while you are arguing for 1 wasted byte at offset 0, did you noticed the wasted 1MB at offset 43512?! ;P
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
mmmm... while you are arguing for 1 wasted byte at offset 0, did you noticed the wasted 1MB at offset 43512?! ;P
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
I’m sure the serialization inefficiencies could be waved away with a layer of arithmetic coding compression using a specialized model trained on the wasted bits of this format, but I worry that this is just basically sweeping technical dust under the rug …and overengineering for such a trivial problem. :(
-
Database people have been discussing variants of non-existing, empty and zero-length strings for at least 30-40 years. There is a difference between not knowing whether a value for the attribute exists, knowing positively that it is void, and knowing that there is a defined value, but it is of zero length. The strangest passport I ever saw was for the son of friends of mine: It stated birthdate as unknown, sex unknown, name unknown, no photo or fingerprint available. When the mother, US citizen living in Norway, is pregnant with a baby, and plans a trip abroad with the baby a few weeks after the delivery, you must start the process of applying for a passport before those details are known. So for this baby's middle name, before his birth, nothing was known, not even if he would have a middle name at all. If he has one, I do not know it, so I could treat it as a defined name attribute with unknown value. If I learn that he certainly does not have a middle name, it would be a known value of zero length. There may be better examples to illustrate various null/empty/… values; this is just the first one dropping into my mind. There may be cases where there is no semantic difference between optional omitted, included but unknown, included with a known length of zero and included with a positive length value. Yet there are other cases where the semantic difference may be very significant.