I like the way you’re thinking! In fact, we’ve been working on some training materials related to the overall topic of “Building Maintainable Code.” Stay tuned for more, and thanks for mentioning the topic.
In the meantime, I’d even go a step further with this idea of leveraging pointers and loops, to eliminate your whole case statement – you just need a couple of tables instead. Then when you go to expand this in the future, you’ll just need to add an element to each of these tables initialized at the top (this is the same method our very important: I/O Enabler uses).
I also renamed your “temp” variable, and cut out a couple of extra lines you had in calculating your fan_runtime so your original 60 lines in now 25.
// We're assuming the ptTotalizers & ptTimesTables were initialized to null
// Now set up the lists to loop through
ptTotalizers = &ex_fan1_run;
ptTotalizers = &ex_fan3_run;
ptTotalizers = &ex_fan4_run;
ptTotalizers = &ex_fan2_run;
ptTotalizers = &chiller_chwp1_run;
ptTotalizers = &chiller_chwp2_run;
ptTimesTable = &pnb_exf1_runtime;
ptTimesTable = &pnb_exf3_runtime;
ptTimesTable = &pnb_exf4_runtime;
ptTimesTable = &pnb_exf2_runtime;
ptTimesTable = &pnb_chiller_chwp1_runtime;
ptTimesTable = &pnb_chiller_chwp2_runtime;
// Initialize our looping pointers, loop through the totalizers, Get & Restart, then
// update the "grand total" (in hours) for each
on_totalizer_ptr1 = ptTotalizers;
float_ptr = ptTimesTable;
totalizer_index = 0;
while ( not (on_totalizer_ptr1 == null) )
fan_runtime = GetRestartOnTimeTotalizer(*on_totalizer_ptr1);
// Convert seconds to hours (60 seconds x 60 minutes) and add to the total
fan_runtime = fan_runtime/(60*60); // in hours
*float_ptr = *float_ptr + fan_runtime;
// Increment the counter & what we're pointing to
on_totalizer_ptr = ptTotalizers[totalizer_index];
float_ptr = ptTimesTable[totalizer_index];
Better? Make sense?
Thanks for sharing and do share more!