有什么心理技巧可以快速推断出具有大量条件逻辑的程序中所需的 ifs/else 吗?

发布于 2024-09-25 10:40:11 字数 377 浏览 0 评论 0原文

通常在编程中,一个非常常见的要求是某些功能需要大量条件逻辑,但不足以保证规则引擎。

例如,测试一个数字是否可以被 x 整除,但也可以被某物的倍数、某物的因数、某物的平方根等。正如您可以想象的,沿着这些思路的东西很容易涉及很多 ifs/elses。

虽然可以使用更现代的编程技术来减少混乱,但如何快速地以计算的方式推断出所需的 if/elses?

例如,在一个为汽车保险潜在客户推断必要报价的程序中(顺便说一句,规则引擎除外),会有年龄、位置、驾驶积分、收集这些积分的年龄等条件逻辑。是否有任何条件逻辑?快速推导出多余条件分支的心理技巧?难道只是普通的经验,没有什么特殊的心法吗?这很重要,因为结对编程有很多噪音,因此很难真正思考问题,甚至很难有足够的时间来实现这个想法。

谢谢

Often in programming, it is a very common requirement that some piece of functionality will require a lot of conditional logic, but not quite enough to warrant a rules engine.

For example, testing a number is divisible by x but also a multiple of something, a factor of something else, a square root of something, etc. As you can imagine, something along these lines will easily involve a lot of ifs/elses.

While it is possible to reduce the clutter with more modern programming techniques, how do you quickly and, in a calculated fashon, deduce the required ifs/elses?

For example, in a program to deduce the necessary quote for a car insurance prospective customer (rules engines aside btw), there would be conditional logic for age, location, driving points, what age those points are collected at, etc. Is there any mental technique to quickly deduce the redundant conditional branches? Is it just plain experience and no special mental technique? This is important because pair programming there is a lot of noise and thus difficult to actually think something through or even get enough time to implement the idea.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相守太难 2024-10-02 10:40:11

我建议,尝试在自己的头脑中做这类事情是自找麻烦,而尝试与合作伙伴一起做会让事情变得更糟。有时你必须坐下来思考,甚至​​在纸上做一些笔记。如果您不喜欢命题逻辑,请尝试决策表

I would suggest that trying to do this sort of thing in your head is asking for trouble, and trying to do it with a partner is going to make it much worse. Sometimes you have to sit and think and even make some notes on paper. If you don't like propositional logic, try decision tables.

行至春深 2024-10-02 10:40:11

我会添加简单、可读、简短的方法,例如:

IsMinor(..)  
IsRecordClean(..)

然后结合使用它们来创建具有有意义名称的新方法,例如:(

IsMeetingPreReqs(..)   //which checks several "simple" conditions
IsValidForInsurance(..)   

对于这些示例,我很抱歉,我在这里的英语很挣扎,但你明白了..)

IMO 将使您的代码更加清晰,从而减少因干扰而感到困惑的机会。
本身不是精神上的,但有点..

I would add simple, readable, short methods, such as:

IsMinor(..)  
IsRecordClean(..)

And then use them in conjunction to create new methods with meaningful names, such as:

IsMeetingPreReqs(..)   //which checks several "simple" conditions
IsValidForInsurance(..)   

(Sorry for the examples, I'm struggling with my English here, but you get the point..)

IMO that will make your code much clearer, and thus reduce the chances of being confused by distractions.
Not mental per se, but kinda..

帥小哥 2024-10-02 10:40:11

我想说你应该使用命题逻辑。
建议:
q = 年龄大于 18
p = 位置在 10 英里以内
r = 行驶点数小于 3
s = 收集积分时年龄小于 18

你可以说...
<代码>
(^ 是 AND)
如果 (q ^ p ^ r ^ s) {
//你有资格什么的!
} 否则{
//离开这里
}

I would say you should use propositional logic.
Suggest that:
q = age is greater than 18
p = location is within 10 miles
r = driving points are less than 3
s = age is less than 18 when points are collected

you could say...

(^ is AND)
if (q ^ p ^ r ^ s) {
//you are eligible or something!
} else {
//get outta here
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文