I have a bunch of Actions in the dApp i'm building. Each action has different data associated with it. At the same time, they have a few common fields. Is there a way to model this to handle the different data associated with it?
Or do I have to make different structs for each action type I have?
Note that If I have 1 struct for all my action types it makes my code cleaner to write. I.e. I can keep the loop and common testing conditions DRY.
If I have multiple structs I have to loop through each different struct so that adds one loop per struct (plus other code that tests common members). Given this concern, is there a suggested way to handle structs with different data?
enum ActionType { A, B, C }
struct Action {
ActionType actionType;
string name;
string description;
uint createdAt;
// should i put optional data fields?
string someFieldOnlyExistsForA;
bool someBoolOnlyExistsForB;
uint someIntOnlyExistsForC;
}
//example what i mean
Action[] actions;
for (uint i = 0; i < actions.length; i++) {
if (actions[i].createdAt < now - 1000) {
if (actions[i].actionType == A) {do something with the string field}
if (actions[i].actionType == B) {do something with the bool field}
if (actions[i].actionType == C) {do something with the int field}
}
}
vs
for (uint i = 0; i < A_actions.length; i++) {
if (Aactions[i].createdAt < now - 1000) {
}
}
for (uint i = 0; i < B_actions.length; i++) {
if (Bactions[i].createdAt < now - 1000) {
}
}
for (uint i = 0; i < C_actions.length; i++) {
if (C_actions[i].createdAt < now - 1000) {
}
}