Jump to content

Thoughts on Class Data default values


Recommended Posts

I've been tangling a little with some classes designed for data serialization and also with just trying to serialize some classes.

One of the things I'm not wild about is the way that default values in a natively serialized object (using the binary or native XML tools) are omitted from the data. I feel like this breaks one of the goals of serializing the data, which is to separate the serialized data from the implementation of the object.

I'm considering making it a convention to try to use default values in class private data that are null or otherwise illegal (in context).

For new objects, this would require writing a constructor that sets the intended defaults.

For deserialized objects, this would require writing a method to validate an object against a class invariant. I'll claim this is a good habit.

Does anyone do it like this? Any drawbacks I'm missing? Any comments or ideas are appreciated.

Thanks,

-B

Edited by blawson
Link to comment

I've been tangling a little with some classes designed for data serialization and also with just trying to serialize some classes.

One of the things I'm not wild about is the way that default values in a natively serialized object (using the binary or native XML tools) are omitted from the data. I feel like this breaks one of the goals of serializing the data, which is to separate the serialized data from the implementation of the object.

I'm considering making it a convention to try to use default values in class private data that are null or otherwise illegal (in context).

For new objects, this would require writing a constructor that sets the intended defaults.

For deserialized objects, this would require writing a method to validate an object against a class invariant. I'll claim this is a good habit.

Does anyone do it like this? Any drawbacks I'm missing? Any comments or ideas are appreciated.

Thanks,

-B

When you say deserialized objects, are you talking about references to objects? (SEQ/DVR/LCOD or whatever implentation?)

in most implementations getting a reference to an object is a private operation anyway, what do you need to validate for?

Link to comment

When you say deserialized objects, are you talking about references to objects? (SEQ/DVR/LCOD or whatever implentation?)

in most implementations getting a reference to an object is a private operation anyway, what do you need to validate for?

Serialization is converting a data structure to a form in which it can be saved or transmitted (a "flat" form like flattening data in LabVIEW). Nothing to do with references to objects.

Link to comment

One of the things I'm not wild about is the way that default values in a natively serialized object are omitted from the data. I feel like this breaks one of the goals of serializing the data, which is to separate the serialized data from the implementation of the object.

To quote the teflon president, "I feel your pain."

This is one of those design decisions R&D had to make that doesn't have a clear "correct" solution. Consider what happens if you save an object to disk with default values, update the .ctl with new default values, and load the saved object? Should the loaded object contain the old default values or new default values? The answer is, it depends. NI had to choose something, so they implemented it so the object loads using the new default values.

I'm considering making it a convention to try to use default values in class private data that are null or otherwise illegal (in context).

Not all data types lend themselves to the default=null convention. Numbers are particularly problematic. Suppose you have a Sum class that adds two numbers and maintains the sum internally. When you load a Sum object from disk where sum=0, is that valid or not? Maybe it's the result of adding -3 and +3? If your serialized objects have data that could be "invalid," it's safer as a general practice to include a private "IsValid" flag.

Link to comment

To quote the teflon president, "I feel your pain."

This is one of those design decisions R&D had to make that doesn't have a clear "correct" solution. Consider what happens if you save an object to disk with default values, update the .ctl with new default values, and load the saved object? Should the loaded object contain the old default values or new default values? The answer is, it depends. NI had to choose something, so they implemented it so the object loads using the new default values.

Not all data types lend themselves to the default=null convention. Numbers are particularly problematic. Suppose you have a Sum class that adds two numbers and maintains the sum internally. When you load a Sum object from disk where sum=0, is that valid or not? Maybe it's the result of adding -3 and +3? If your serialized objects have data that could be "invalid," it's safer as a general practice to include a private "IsValid" flag.

Or in other words, a data type doesn't have an acceptable null if all possible values are acceptable. Booleans would be the easiest example.

I just want to find the middle ground where I can store the state of an object in a human friendly form without writing it out custom for every single class, and without it mysteriously changing if values are sometimes default and sometimes not.

Edited by blawson
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.