(This is C btw, not C++)
I just came across the following. Regulator_t is a struct holding various parameters.
/* only one of these */
Regulator_t theRegulator;
int main()
{
/* initialise regulator */
set_coil_value( 0.0001f, /* 100uH coil */
set_maximum_coil_current( 1.5f, /* allow 1.5 amps */
set_maximum_output_voltage( 200.0f, /* cap output voltage to 200 volts */
set_switching_voltage( 325.0f, /* voltage from rectifier is 230*sqrt(2) = 325V */
set_output_voltage_divider_ratio( 100.0f, /* 1Meg to 10k is a ratio of 100 */
initialise_regulator( theRegulator ))))));
/* ---SNIP--- */
}
I'm not sure how to feel about this. Someone was obviously trying to be a smartass by defining all "set" functions with the pattern:
Regulator_t* set_whatever( float someValue, Regulator_t* regulator )
{
regulator->someValue = someValue;
/* do other important stuff */
return regulator;
}
On one hand, it's a little confusing because the first section of code is executed "backwards", i.e. initialise_regulator is called first and then the chain is executed from the inside out.
On the other hand, it might actually be easier to read than doing it the traditional way:
initialise_regulator( theRegulator );
set_coil_value( theRegulator, 0.0001f );
set_maximum_coil_current( theRegulator, 1.5f );
/* etc */
Any thoughts?