Better ternary condition

  • A+
Category:Languages

Method find_something may return nil. In the following code,

something = find_something(id) ? find_something(id) : create_something(foo) 

find_something(id) is called twice. This is a smell that I want to avoid. Is there a way to avoid redundancy in this expression?

 


Anything like this?

something = find_something(id) || create_something(foo) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: