mercredi 30 octobre 2019

Is calling a small function twice (e.g. in if condition and boby) preferable than storing result in local variable?

Which of the two options below is better (or preferable), and why ?

ReturnType *function1(const ParamType *param) {
  const ValueType* value = getSomeValue(param);
  if (value) {
     return value->finalStuff();
  }
  return nullptr;
}

VS

ReturnType *function2(const ParamType *param) {
  if (getSomeValue(param)) {
     return getSomeValue(param)->finalStuff();
  }
  return nullptr;
}

Given getSomeValue like :

ValueType * getSomeValue(const ParamType *param) {
    if (param) {
        return param->some.very.boring.stuff.value;
    }
    return nullptr;
}

Any better option ? Thanks

Aucun commentaire:

Enregistrer un commentaire