Reassignment if vs if-else?

Is there a difference between reassigning like:

data {
  int is_fancier_model;
...
}
...
transformed parameters {
  mu = beta_0;
  if (is_fancier_model) {
     mu = beta_0 + beta_1*x;
  }
} 

vs using if-else like:

data {
  int is_fancier_model;
...
}
...
transformed parameters {
  if (is_fancier_model) {
     mu = beta_0 + beta_1*x;
  } else {
     mu = beta_0;
  }
} 

This might not be what you’re asking, but the better option is to conditionally aggregate:

transformed parameters {
  vector[N] mu = beta_0;
  if (is_fancier_model) {
     mu += beta_1 * x;
  }
} 

Depending on your C++ optimization setting (e.g. -O2, etc) the code most likely ends up being executed identically in the end. I just did a quick test and all three styles above (when used in pure C++, but the Stan translation is fairly direct here) yield the same assembly code at O2

Does Stanc3 still initialise variables first with NaNif a declare-and-define is used? Or would the variable be initialised with the assigned value?

On my phone so can’t check myself

If you use stanc’s optimizations it should remove the NaN initialization.

This means the style which doesn’t use else would have benefits if the type of mu is a vector or matrix, since gcc or clang’s optimizer most likely wouldn’t delete the object creation.

Neat, thanks!