Question

I was studying some code and found this (here):

void ass_set_margins(ASS_Renderer *priv, int t, int b, int l, int r)
{
    if (priv->settings.left_margin != l || priv->settings.right_margin != r ||
        priv->settings.top_margin != t || priv->settings.bottom_margin != b) {
        priv->settings.left_margin = l;
        priv->settings.right_margin = r;
        priv->settings.top_margin = t;
        priv->settings.bottom_margin = b;
        ass_reconfigure(priv);
    }
}

I am wondering why is the need to check the values first, so that if one of which is different from the passed arguments, then we assign all values with the the passed arguments? What if we passed them all without checking? Less code, less overhead?

Is the overhead for changing state is more than checking it?

Était-ce utile?

La solution

There is more than just assignment:

ass_reconfigure(priv);

So the conditions would have to be checked anyway.

And if you have to perform the branch anyway, why not move assignments that are only necessary under the same conditions in there, too? That certainly saves some lines code and might also save one or two executed instructions.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top