避免过多的IF [英] Avoiding too many IFs

查看:61
本文介绍了避免过多的IF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

对不起,这个问题的主题,但真的很难考虑更好的主题.

我有时在编程中遇到这样的情况:我有太多的变量,当我有14个变量时,它真的很不舒服,我必须使用if语句将每个人彼此进行比较.

即使我处理了这么多变量或有另一种方法,我也必须使用if吗?


感谢您的回答,对不起我的英语.

Hello,

Sorry for the subject of this question but its really hard to think about better subject.

I''m in programming sometimes in situation where I have too many variables and its really uncomfortable when I have for example 14 variables and I have to compare everyone with each other using if statement.

Do I have to use if''s even if I work with so many variables or there is another way?


Thanks for any answer and sorry my English.

推荐答案

正如我对问题的评论一样,这个问题很有趣.避免 if 的程序设计是一个很大的话题,引起了很多概念上的思考.

您可能会发现一些有关相关主题的文章很有趣:
http://programmingwithoutifs.blogspot.com/ [ ^ ],
http://www.ewernli.com/noif [ http://aigamedev.com/open/review/schematic-table-conditionals/ [ ^ ].

根据Wikipedia关于条件编程的文章,动态调度可以被视为替代的执行选择方法.请参阅:
http://en.wikipedia.org/wiki/Conditional_%28programming%29 [ ^ ],
http://en.wikipedia.org/wiki/Dynamic_dispatch [动态方法分派器 [
As I put in my comment to the question, this question is interesting. The programming avoiding ifs is a whole big topic which evoked a lot of thinking on the conceptual level.

You may find some of the articles on related topics interesting:
http://programmingwithoutifs.blogspot.com/[^],
http://www.ewernli.com/noif[^],
http://aigamedev.com/open/review/schematic-table-conditionals/[^].

According to the Wikipedia article on conditional programming, dynamic dispatch can be considered as an alternative execution choice approach. Please see:
http://en.wikipedia.org/wiki/Conditional_%28programming%29[^],
http://en.wikipedia.org/wiki/Dynamic_dispatch[^].

However, this is pretty hard to understand. I would suggest my own CodeProject article, where I put forward my approach to the dynamic dispatch which helps to avoid if and switch statements:
Dynamic Method Dispatcher[^].

This work includes detailed motivation, full source code and detailed usage description and samples. You may also find it all interesting.

—SA


我认为这实际上取决于您要尝试运行的条件类型.
如果所有变量的条件相同(null,小于等),则将所有变量添加到列表中,并对列表中的所有项目运行if可能会更好.

但是,如果您有几种不同的条件要针对它们运行,则可以创建一个func列表和一个对(变量+测试)列表,并运行类似
的循环 我列表中的Foreach项目
测试中的Foreach项目...

现在您要在基础架构中进行更多工作……对于像这样的一般性问题,我认为没有一个答案能完全解决问题.但是,当您确定最适合的细节时,他们一定会很有趣




反正我的2美分
I think it really depends on the type of conditions you are attempting to run.
If they it’s the same condition for all variables, ( null, less than etc ) then adding all the variables in to a list and running the if against all items in the list might work nicer.

If however you have several different conditions you’d like to run against them, you could create a list of func and a list of pairs ( variable + tests ) and run a loop like
Foreach item in my list
Foreach item in the test…

Now you’re creating more work in the infrastructure…… I don’t think there is 1 answer fits all when it comes to general questions like this. But they are sure fun to deal with when you have specifics to figure out which best fits



my 2 cents anyway


这篇关于避免过多的IF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆