Wouldn't it be nice if one could declare variable or constants in a formula in Google Sheet? Define something once and reuse it multiple times within one formula.
= declare(VAR=(if(A="";B;A))&&right(VAR,len(VAR))
(And directly in the sheet, not via apps script.)