lundi 27 juillet 2015

Better way to handle many if conditions?

We have many different types of feeds. And one feed has many feed_comments..

And on the basis of feed type I want to return a specific string..

if feed.type == 1
  if nested_comment
    str = "test"
  else if comment
    str = "test1"
  else
    str = "test2"
  end 
else if feed.type == 2
  if nested_comment
    str = "test3"
  else if comment
    str = "test4"
  else
    str = "test5"
  end 
else if feed.type == 3
  if nested_comment
    str = "test6"
  else if comment
    str = "test7"
  else
    str = "test8"
  end 
else if feed.type == 4
  if nested_comment
    str = "test9"
  else if comment
    str = "test10"
  else
    str = "test11"
  end 
end

What is the better way to write the above code?

So, I don't have to change my existing code in future if we have many more conditions here..

Aucun commentaire:

Enregistrer un commentaire