Class attribute access

Hi guys,

hope this is the right place to ask. I have a Class with an attribute that is used a lot. I could do this attribute public, but i want it readonly. So i did following:

MyController.h

class MyController
{
private:
	// Timing
	uint16_t nowTMinute;

public:
	MyController();

	uint16_t GetTMinute();
};

MyController.cpp

uint16_t MyController::GetTMinute() {
    return nowTMinute;
}

Now for example i have following code each cylce:
Main.cpp

// Check for rise or dawn
if (Controller.GetTMinute() <= Config.Data.LowLights[ident].on_tmin + (Config.Data.LowLights[ident].rise_minutes)) {
	// Rise
	Controller.SetPWM(pwmPort, calcPwm((Controller.GetTMinute() - Config.Data.LowLights[ident].on_tmin), 1));
}
else if (Controller.GetTMinute() >= Config.Data.LowLights[ident].off_tmin - (Config.Data.LowLights[ident].dawn_minutes)) {
	Controller.SetPWM(pwmPort, calcPwm((Config.Data.LowLights[ident].off_tmin - Controller.GetTMinute()), 0));
}
else { // MaxPower
	Controller.SetPWM(pwmPort, (Config.Data.LowLights[ident].max_power));
}

Here i call the function a lot. Would be the performance much better if i did the Attribute public?
Or would it be better to declare a temp variable for the if statement like that:

uint8_t  tmin = Controller.GetTMinute() ;
// Check for rise or dawn
if (tmin  <= Config.Data.LowLights[ident].on_tmin + (Config.Data.LowLights[ident].rise_minutes)) {
	// Rise
	Controller.SetPWM(pwmPort, calcPwm((tmin  - Config.Data.LowLights[ident].on_tmin), 1));
}
else if (tmin  >= Config.Data.LowLights[ident].off_tmin - (Config.Data.LowLights[ident].dawn_minutes)) {
	Controller.SetPWM(pwmPort, calcPwm((Config.Data.LowLights[ident].off_tmin - tmin ), 0));
}
else { // MaxPower
	Controller.SetPWM(pwmPort, (Config.Data.LowLights[ident].max_power));
}

But then i declare an extra variable in the ram for "nothing" . Hope you can understand my question :slight_smile:

Greetings,
Raphael

I think your second option is better. The code is easier to read.
Should tmin be uint16_t?
Whether it is faster will depend on compiler optimization.
Making it public is also a valid option on small micro controllers. You are the only user, so you are the one that can prevent changing that variable from outside... you can give it a name to remember yourself (variableXreadOnly)..
I think the public/private thing is more important with larger programs and more than one programmer.

not MUCH better. a little bit better. a tiny bit that you will NOT notice EVER. so don't do it for performance reasons.

yes, u will gain the TINY LITTLE BIT of CPU time for not calling the method on the object multiple times.

but u will lose a TINNY LITTLE BIT of CPU time for creating the var. and u will lose a TINY LITTLE BIT of memory to store the new var.

should you do it? for performance reasons: DON'T. it does not matter.

for readability reasons: maybe.

do focus on what's the most readable, what's easiest to understand. the kind of (micro)optimization you're asking -> does not matter. even on such small devices as arduino hardware. in 90% of cases anyway.

1 Like

BTW, you have something like this:

valueFromController = controller.getValue();

controller.setOtherValue(valueFromController, configValue);

That's a common (anty)pattern. You probably could (should) do something like this:

controller.setOtherValue(configValue);

The keyword/rule/pattern to search for is: Tell, don't ask.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.