More efficient way to write if-conditionals with repetitive variable

c, conditional-statements, if-statement, performance, variables

Solution

I recommend using intermediate variables and breaking up the logic into its own function. Generally, whenever I find conditional logic like this, it shows up again and again in my project, so refactoring it saves time in the long run.

Type processInput(const Type input)
{
  auto result = input;
  if ( input < b )
  {
    result = c;
  }
  return result;
}

int main()
{
  const auto input = huge_term;
  const auto result = processInput(input);
}

Problem

I'm looking for a more efficient way to write these kinds of if-conditionals: ``` a = huge_term >= b ? huge_term : c ``` or ``` (a = huge_term) >= b ? a : a = c ``` The second one is quite shorter but the variable `a` appears 3 times. I need the result to be stored in a variable. How would you write it?

Original source