逻辑编程帮助 - II
A
if ( ( empty($infoA) || empty($infoB) ) && ( !empty($inputA) && !empty($inputB) ) )
{
//add
}
B
if ( ( !empty($infoA) || !empty($infoB) ) && ( empty($inputA) && empty($inputB) ) )
{
//remove
}
C
if ( ( !empty($infoA) || !empty($infoB) ) && ( ($inputA != $infoA) || ($inputB != $infoB) ) )
{
//add
}
因此,为了不重复添加,我们可以:
if (A || C)
{
//add
}
elseif(B)
{
//remove
}
在您的选项上有更好的逻辑吗?
语境: 我相信这应该做什么无关紧要。我的意思是,这是一个逻辑问题。 :s 不知道在这里写什么...:(
这是一个表单:一些输入将来自数据库,其他输入来自输入字段。 我们在这里做一些比较。
A上下文: 如果来自数据库的值为空,并且输入字段A和B不为空,则添加到数据库。
B 上下文: 如果来自数据库的值不为空,并且输入字段 A 和 B 为空,请从数据库中删除。
C 上下文: 如果来自数据库的值不为空,并且输入字段A!等于 infoA或输入字段B不等于数据库值infoB,那么, 添加到数据库。
请指教。 MEM
A
if ( ( empty($infoA) || empty($infoB) ) && ( !empty($inputA) && !empty($inputB) ) )
{
//add
}
B
if ( ( !empty($infoA) || !empty($infoB) ) && ( empty($inputA) && empty($inputB) ) )
{
//remove
}
C
if ( ( !empty($infoA) || !empty($infoB) ) && ( ($inputA != $infoA) || ($inputB != $infoB) ) )
{
//add
}
So, in order to not repeat the add, we can:
if (A || C)
{
//add
}
elseif(B)
{
//remove
}
Any better logic to be applied here on your option?
Context:
I believe it's irrelevant what this should do. I mean, it's a logical question. :s
Not sure what to write here... :(
This is for a form: some inputs will come from the database, others from input fields.
We are doing some comparisons here.
A context:
If the value that comes from the database is empty, and the input fields A and B are NOT empty, do add to database.
B context:
If the value that comes from the database is NOT empty, and the input fields A and B are empty, do remove from the database.
C context:
If the values that comes from the database are NOT empty, AND the input fieldA !equal infoA or input fieldB NOT equal database value infoB then, do add to the database.
Please advice.
MEM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几个问题:
一般来说,我会通过以下方式重构它:将
逻辑的唯一简化是调用 add() 或 remove() 的决定仅基于输入数据是否为空。这就是真正需要的一切。 add() 和remove() 函数执行自己的数据验证,以确保操作对于提供的数据有效。
此重构演示了两种重要的编程技术:
(请原谅我的php...它有点生疏)
Several questions:
In general, I would refactor this by:
The only simplification of the logic is that the decision for calling add() or remove() is only based on whether or not the input data is empty or not. That's all that's really needed. The add() and remove() functions do their own data validation to make sure the operation is valid for the supplied data.
This refactoring demonstrates two important programming techniques:
(Excuse my php... it's a bit rusty)
我认为展开条件并使用 else 或 else-if 语句对于代码的可读性和理解有很大帮助。虽然您提出的答案减少了代码行数,但它将很难理解和调试,特别是如果您将其传递给其他人。
这是我的看法,
其他每个都可以汇总到一个函数中;我没有这样做,因为我想不出任何逻辑来调用 else 块中的代码块(处理可能删除更新?确定更新或删除?)
I think unrolling the conditions and using else or else-if statements helps a lot, for readability and understanding of code. While your proposed answer cuts down on lines of code, it is going to be hard to understand and debug, especially if you pass it on to someone else.
Here's my take
Each of the else can be rolled up into a function; I didn't because I can't think of anything logical to call the block of code in else block (process-maybe-delete-update? determine-update-or-delete?)