Question

A while ago I finished the highly recommended "Working Effectively with Legacy Code" by Michael Feathers. In a final confirmation of my increasing senility I am absolutely convinced seeing a code sample in that book where he defined a struct that included a version number as one of its fields.

However, I am completely unable to find again where I may have seen this code sample. Does anyone know if it was indeed in that book?

Otherwise, is it generally a good idea to include a version number in a struct? How/when might it be used?

Was it helpful?

Solution

Normally it is the other way round. Persistent, serialized data structures on external media would be versioned (contain a version number), while the in-memory structure would exist in a single version only, possibly generalized enough to be able to represent multiple persistent formats in a unified model, and typically not contain the version number.

However, this versioning approach implies that if you modify (edit, update) a data structure and save it back, it will typically be encoded into the latest available format, for good or bad. Occasionally this is not desirable. For example, this could break compatibility with a lower software release or lower capability node/process/system that created this piece of data and that may still need to access it. In such circumstances, you might decide to store the format version even in the in-memory struct and stick to it when serializing, to avoid such "implied format upgrades", or to prevent types of updates that the older format cannot represent or should not allow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top