逻辑编程帮助 - II

发布于 2024-09-17 18:48:57 字数 1086 浏览 3 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

琉璃梦幻 2024-09-24 18:48:57

几个问题:

  • C真的是Add,还是update?
  • 您真的需要将 inputA 和 inputB 的处理与 infoA 和 infoB 联系起来吗?

一般来说,我会通过以下方式重构它:将

  • 添加/删除决策拆分为单独的 process_input 函数,该函数仅处理一对输入/信息对象。
  • 将添加/删除活动拆分为它们自己的函数,这些函数将处理数据的基本空处理。

逻辑的唯一简化是调用 add() 或 remove() 的决定基于输入数据是否为空。这就是真正需要的一切。 add() 和remove() 函数执行自己的数据验证,以确保操作对于提供的数据有效。

此重构演示了两种重要的编程技术:

  • DRY(不要重复自己)原则...我们不应该多次检查对象上的empty()。
  • SRP(单一责任原则)——每个操作应该只做一件事。
    • 输入处理程序负责将输入数据与现有信息数据配对,然后调用 process_input。
    • process_input函数只负责决定是否需要执行add()或remove()操作。
    • add() 和remove() 函数各自负责自己不同的操作,可以独立于其余代码进行测试

(请原谅我的php...它有点生疏)

function remove($data)
{
    if (empty($data))
        return;

    // Do remove operation
}

function add($data)
{
    if (empty($data))
        return;

    // Do add operation
}

function process_input($input, $info)
{
    if (empty($input))
        remove($info);
    else
        add($input);
}

process_input($inputA, $infoA);
process_input($inputB, $infoB);

Several questions:

  • Is C really Add, or is it update?
  • Do you really need to tie procesing of inputA and inputB with infoA and infoB?

In general, I would refactor this by:

  • Splitting the add/remove decision into a separate process_input function which handled just one pair of input/info objects.
  • Spliting add/remove activities into their own functions which would handle basic empty handling of the data.

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:

  • The DRY (don't-repeat-yourself) principle... we shouldn't be checking empty() on an object more than once.
  • SRP (single-responsibility-principle) - each operation should only do one thing.
    • The input handler is responsible for pairing the input data with the existing info data and then calling process_input.
    • The process_input function is only responsible for deciding whether we need to do an add() or remove() operation.
    • The add() and remove() functions are each responsible for their own distinct operation which can be tested in isolation of the rest of the code

(Excuse my php... it's a bit rusty)

function remove($data)
{
    if (empty($data))
        return;

    // Do remove operation
}

function add($data)
{
    if (empty($data))
        return;

    // Do add operation
}

function process_input($input, $info)
{
    if (empty($input))
        remove($info);
    else
        add($input);
}

process_input($inputA, $infoA);
process_input($inputB, $infoB);
拥有 2024-09-24 18:48:57

我认为展开条件并使用 else 或 else-if 语句对于代码的可读性和理解有很大帮助。虽然您提出的答案减少了代码行数,但它将很难理解和调试,特别是如果您将其传递给其他人。

这是我的看法,

// check if to update in DB or not

$value = db_adapter::get_var($sql);
if (empty($value))
{
  if (!empty($field_A) && !empty($field_B))
    add($field_A, $field_B);  

} else 
// you can roll the following block up into a function
{


  if (empty($field_A) && empty($field_B))
    remove($field_A, $field_B);
   else {
      if ($field_A != $value)
       update($field_A);  
     else if ($field_B != $value)
       update($field_B);
   }

}

其他每个都可以汇总到一个函数中;我没有这样做,因为我想不出任何逻辑来调用 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

// check if to update in DB or not

$value = db_adapter::get_var($sql);
if (empty($value))
{
  if (!empty($field_A) && !empty($field_B))
    add($field_A, $field_B);  

} else 
// you can roll the following block up into a function
{


  if (empty($field_A) && empty($field_B))
    remove($field_A, $field_B);
   else {
      if ($field_A != $value)
       update($field_A);  
     else if ($field_B != $value)
       update($field_B);
   }

}

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?)

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