This works, but does any have a suggestion for a more simplified / elegant way to write this?:
if @mediaAddQueue[''+mid]['mediaType'] is 'photo' and
@mediaAddQueue[''+mid]['econ_status'] is 'loaded' and
@mediaAddQueue[''+mid]['medium_status'] is 'loaded' and
@mediaAddQueue[''+mid]['thumb_status'] is 'loaded' and
not @mediaAddQueue[''+mid]['targetEventRecord']?
@addMedia @mediaAddQueue[''+mid]['targetEventRecord'], mid, @mediaAddQueue[''+mid]['mediaType']
else if @mediaAddQueue[''+mid]['mediaType'] is 'video' and
@mediaAddQueue[''+mid]['video_status'] is 'loaded' and
not @mediaAddQueue[''+mid]['targetEventRecord']?
@addMedia @mediaAddQueue[''+mid]['targetEventRecord'], mid, @mediaAddQueue[''+mid]['mediaType']
Sure! First, notice that
@mediaAddQueue[''+mid]is repeated all over the place, you can replace that with a variable. Also, there’s no need to access properties likesomething['prop']if the property has a valid identifier as a name; you can dosomething.prop. Changing those two things already cleans the code quite a bit:Then, notice that the code inside both the
ifand theelse ifis the same. I think it would be great if you could give some meaningful name to the conditions so that the code becomes more self-documented and DRY. I don’t know much about the context of this code, so i’ll be guessing the variable names; try to use something that explains their meaning as clearly as possible: