如何避免if ... else和switch case

Koo*_*bin 9 vb.net algorithm programming-languages

我一直在编程很多时间.通常我用PHP,ASP.net,Java,JavaScript等语言编程.在所有语言中,我必须使用很多if else语句.就像值= 10然后......如果我查看我的代码然后我发现很多if条件.所以我想尽量减少它们但不确定.

有一点是使用了有些最小化的类但仍然更多......

像任务,猫,秒和类型:

if task = 'add' then
   if cat = "animal" then
      if sec = "man" then
          if type = "male" then
                 'do the following stuffs
          else
                 'do the following stuffs
          end if
      elseif sec = "horse" then
          if type = "run"
                 'do the following stuffs
          else
                 'do the following stuffs
          end if
      elseif....
      end if
   elseif cat = "plant" then
     if sec = "land" then
          if type="tree" then
                 'do the following stuffs
          elseif type = "grass" then..
                 'do the following stuffs
          elseif...
          end if
    elseif sec = "water" then
    ...
...
Run Code Online (Sandbox Code Playgroud)

...

更多n继续n继续

所以我想知道如何最小化它们并编写一些有效的代码?

很抱歉最近通知任务,cat,sec和type可能有很多值.我的if语句嵌套嵌套.

更多解释我的代码也看起来像:

http://thedailywtf.com/Articles/Coding-Like-the-Tour-de-France.aspx

Mit*_*eat 14

许多if..else陈述通常是多态性未被使用的症状.