I’m currently designing a somewhat large program that will involve the simulation of math/physics models and collection of data (I have not implemented any code yet). One of the main problems I’m facing is how to pass data around easily without having to have multiple copies of data (that may potentially be outdated and redundant).
Initially I was thinking of having the model objects each implement a function that returns a data structure containing copies of their data, then collecting all those data structures together and passing that around. This would involve a lot of copying however, and the data wouldn’t be mapped to the actual object anymore. (The data structure idea I had would involve using a trie so that the data is nice and hierarchical and lookups can be done easily by string)
So now I was thinking that instead of designing the program to use a data structure to pass data around, the program would simply pass the model, and then data can be extracted with reflection. This would pretty much avoid any redundancy and the data passed will always be up to date.
The problem is that I also need this program to be fast (it has soft realtime constraints), and I’m not sure about the performance impact of reflection. And reflection does seem kinda ugly. But at the same time, people say it’s never a good idea to optimize early, so I’m on the fence here.
So should I design the program to use reflection, and if so, how liberally? Or should I try to avoid reflection as much as I can during design?
8
Without an example, this question is a little bit too broad for my taste, so best answer I can give you is a very generic one. IMHO what you need is a more functional approach:
-
model your data using immutable objects. For immutable objects, there is no need to create any copies, they can be passed by reference without the risk of creating any side effects.
-
using only immutable data structures forces you to organize your program in a way where each function which could deliver potentially out-of-date results is called “on the fly” at the point in time when the data is needed, not earlier.
-
instead of solving problems imperatively by “calculating some data x and y, store it somewhere, reuse parts of it later to calculate a, b, and c”, learn how to model problems in a functional way (write functions for calculating a, b, and c based on the functions for calculating x and y).
-
model your data flow, maybe graphically – make sure you know exactly which components in your system get data from which other components, and where the “asynchronous changes” are introduced. I recommend Flow Design for this.
-
reflection is most probably the wrong choice for your problem (not for performance reasons, but for actually introducing more problems than it will solve for you)
EDIT: a really good example of a physical model which seems similar to your problem is the digital circuit simulator in “Structure and Interpretation of Computer programs”. And this example is indeed based on mutable data (though its also using functional elements). Maybe this approach maybe a blueprint for yours.
4