开发者

Any mental technique to quickly deduce the required ifs/elses in a program with A LOT of conditional logic?

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), t开发者_如何学JAVAhere 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


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.


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..


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
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜