真实,直到被证实或被证明是错误的?

时间:2021-11-18 11:33:17

I've noticed something about my coding that is slightly undefined. Say we have a two dimension array, a matrix or a table and we are looking through it to check if a property is true for every row or nested dimension.

我注意到我的编码有点不明确。假设我们有一个二维数组,一个矩阵或一个表,我们正在查看它以检查每个行或嵌套维度的属性是否为真。

Say I have a boolean flag that is to be used to check if a property is true or false. My options are to:

假设我有一个布尔标志,用于检查属性是真还是假。我的选择是:

  1. Initialize it to true and check each cell until proven false. This gives it a wrong name until the code is completely executed.
  2. 将其初始化为true并检查每个单元格,直到证明为假。在代码完全执行之前,这会给它一个错误的名称。

  3. Start on false and check each row until proven true. Only if all rows are true will the data be correct. What is the cleanest way to do this, without a counter?
  4. 从false开始并检查每一行,直到证明为真。仅当所有行都为真时,数据才是正确的。没有柜台,最干净的方法是什么?

I've always done 1 without thinking but today it got me wondering. What about 2?

我总是不假思索地做过1但是今天让我感到疑惑。 2怎么样?

6 个解决方案

#1


They actually both amount to the same thing and since you say "check if a property is true for every row or nested dimension", I believe the first method is easier to read and perhaps slightly faster.

它们实际上都是相同的,因为你说“检查属性是否适用于每一行或嵌套维度”,我相信第一种方法更容易阅读,也许稍快一些。

You shouldn't try to read the value of the flag until the code is completely executed anyway, because the check isn't finished. If you're running asynchronous code, you should guard against accessing the value while it is unstable.

在代码完全执行之前,您不应该尝试读取标志的值,因为检查未完成。如果您正在运行异步代码,则应防止在不稳定时访问该值。

Both methods "give a wrong name" until the code is executed. 1 gives false positives and 2 gives false negatives. I'm not sure what you're trying to avoid by saying this - if you can get the "correct" value before fully running your code, you didn't have run your code in the first place.

在执行代码之前,这两种方法都“给出了错误的名称”。 1给出假阳性,2给出假阴性。我不确定你要避免这样做 - 如果你在完全运行代码之前得到“正确”的值,你就没有首先运行你的代码。


How to implement each without a counter (if you don't have a foreach syntax in your language, use the appropriate enumerator->next loop syntax):

如何在没有计数器的情况下实现每个(如果您的语言中没有foreach语法,请使用相应的枚举器 - > next循环语法):

1:

bool flag = true;

foreach(item in array)
{
    if(!check(item))
    {
        flag = false;
        break;
    }
}

2:

bool flag = false;
foreach(item in array)
{
    if(!check(item))
    {
        break;
    } 
    else if(item.IsLast())
    {
        flag = true;
    }
}

#2


Depends on which one dumps you out of the loop first, IMHO.

取决于哪一个首先将你转出循环,恕我直言。

For example, with an OR situation, I'd default to false, and as soon as you get a TRUE, return the result, otherwise return the default as the loop falls through.

例如,对于OR情况,我默认为false,一旦获得TRUE,返回结果,否则在循环通过时返回默认值。

For an AND situation, I'd do the opposite.

对于AND情况,我会做相反的事情。

#3


Go with the first option. An algorithm always has preconditions, postconditions and invariants. If your invariant is "bool x is true iff all rows from 0-currentN have a positve property", then everything is fine.

选择第一个选项。算法总是有前置条件,后置条件和不变量。如果你的不变量是“bool x是真的,如果0-currentN的所有行都有一个positve属性”,那么一切都很好。

Don't make your algorithm more complex just to make the full program-state valid per row-iteration. Refactor the method, extract it, and make it "atomic" with your languages mechanics (Java: synchronized).

为了使每个行迭代的完整程序状态有效,不要使算法更复杂。重构方法,提取它,并使用您的语言机制(Java:synchronized)使其“原子化”。

#4


Personally I just throw the whole loop into a somewhat reusable method/function called isPropertyAlwaysTrue(property, array[][]) and have it return a false directly if it finds that it finds a case where it's not true.

就个人而言,我只是将整个循环抛入一个名为isPropertyAlwaysTrue(property,array [] [])的可重用的方法/函数中,如果它发现它找到的情况不正确,则直接返回false。

The inversion of logic, by the way, does not get you out of there any quicker. For instance, if you want the first non-true case, saying areAnyFalse or areAllTrue will have an inverted output, but will have to test the exact same cases.

顺便说一下,逻辑的反转并没有让你更快地离开那里。例如,如果你想要第一个非真实的情况,说areAnyFalse或areAllTrue将有一个反向输出,但必须测试完全相同的情况。

This is also the case with areAnyTrue and areAllFalse--different words for the exact same algorithm (return as soon as you find a true).

areAnyTrue和areAllFalse也是如此 - 完全相同的算法的不同单词(一旦找到true就返回)。

You cannot compare areAnyFalse with areAnyTrue because they are testing for a completely different situation.

您无法将areAnyFalse与areAnyTrue进行比较,因为它们正在测试完全不同的情况。

#5


Make the property name something like isThisTrue. Then it's answered "yes" or "no" but it's always meaningful.

使属性名称类似于isThisTrue。然后它回答“是”或“否”,但它总是有意义的。

In Ruby and Scheme you can use a question mark in the name: isThisTrue?. In a lot of other languages, there is a convention of puttng "p" for "predicate" on the name -- null-p for a test returning true or false, in LISP.

在Ruby和Scheme中,您可以在名称中使用问号:isThisTrue?。在许多其他语言中,在LISP中,对于名称的“谓词”,存在一个puttng“p”的约定 - null-p,用于返回true或false的测试。

#6


I agree with Will Hartung.

我同意Will Hartung的观点。

If you are worried about (1) then just choose a better name for your boolean. IsNotSomething instead of IsSomething.

如果你担心(1)那么只需为你的布尔值选择一个更好的名字。 IsNotSomething而不是IsSomething。

#1


They actually both amount to the same thing and since you say "check if a property is true for every row or nested dimension", I believe the first method is easier to read and perhaps slightly faster.

它们实际上都是相同的,因为你说“检查属性是否适用于每一行或嵌套维度”,我相信第一种方法更容易阅读,也许稍快一些。

You shouldn't try to read the value of the flag until the code is completely executed anyway, because the check isn't finished. If you're running asynchronous code, you should guard against accessing the value while it is unstable.

在代码完全执行之前,您不应该尝试读取标志的值,因为检查未完成。如果您正在运行异步代码,则应防止在不稳定时访问该值。

Both methods "give a wrong name" until the code is executed. 1 gives false positives and 2 gives false negatives. I'm not sure what you're trying to avoid by saying this - if you can get the "correct" value before fully running your code, you didn't have run your code in the first place.

在执行代码之前,这两种方法都“给出了错误的名称”。 1给出假阳性,2给出假阴性。我不确定你要避免这样做 - 如果你在完全运行代码之前得到“正确”的值,你就没有首先运行你的代码。


How to implement each without a counter (if you don't have a foreach syntax in your language, use the appropriate enumerator->next loop syntax):

如何在没有计数器的情况下实现每个(如果您的语言中没有foreach语法,请使用相应的枚举器 - > next循环语法):

1:

bool flag = true;

foreach(item in array)
{
    if(!check(item))
    {
        flag = false;
        break;
    }
}

2:

bool flag = false;
foreach(item in array)
{
    if(!check(item))
    {
        break;
    } 
    else if(item.IsLast())
    {
        flag = true;
    }
}

#2


Depends on which one dumps you out of the loop first, IMHO.

取决于哪一个首先将你转出循环,恕我直言。

For example, with an OR situation, I'd default to false, and as soon as you get a TRUE, return the result, otherwise return the default as the loop falls through.

例如,对于OR情况,我默认为false,一旦获得TRUE,返回结果,否则在循环通过时返回默认值。

For an AND situation, I'd do the opposite.

对于AND情况,我会做相反的事情。

#3


Go with the first option. An algorithm always has preconditions, postconditions and invariants. If your invariant is "bool x is true iff all rows from 0-currentN have a positve property", then everything is fine.

选择第一个选项。算法总是有前置条件,后置条件和不变量。如果你的不变量是“bool x是真的,如果0-currentN的所有行都有一个positve属性”,那么一切都很好。

Don't make your algorithm more complex just to make the full program-state valid per row-iteration. Refactor the method, extract it, and make it "atomic" with your languages mechanics (Java: synchronized).

为了使每个行迭代的完整程序状态有效,不要使算法更复杂。重构方法,提取它,并使用您的语言机制(Java:synchronized)使其“原子化”。

#4


Personally I just throw the whole loop into a somewhat reusable method/function called isPropertyAlwaysTrue(property, array[][]) and have it return a false directly if it finds that it finds a case where it's not true.

就个人而言,我只是将整个循环抛入一个名为isPropertyAlwaysTrue(property,array [] [])的可重用的方法/函数中,如果它发现它找到的情况不正确,则直接返回false。

The inversion of logic, by the way, does not get you out of there any quicker. For instance, if you want the first non-true case, saying areAnyFalse or areAllTrue will have an inverted output, but will have to test the exact same cases.

顺便说一下,逻辑的反转并没有让你更快地离开那里。例如,如果你想要第一个非真实的情况,说areAnyFalse或areAllTrue将有一个反向输出,但必须测试完全相同的情况。

This is also the case with areAnyTrue and areAllFalse--different words for the exact same algorithm (return as soon as you find a true).

areAnyTrue和areAllFalse也是如此 - 完全相同的算法的不同单词(一旦找到true就返回)。

You cannot compare areAnyFalse with areAnyTrue because they are testing for a completely different situation.

您无法将areAnyFalse与areAnyTrue进行比较,因为它们正在测试完全不同的情况。

#5


Make the property name something like isThisTrue. Then it's answered "yes" or "no" but it's always meaningful.

使属性名称类似于isThisTrue。然后它回答“是”或“否”,但它总是有意义的。

In Ruby and Scheme you can use a question mark in the name: isThisTrue?. In a lot of other languages, there is a convention of puttng "p" for "predicate" on the name -- null-p for a test returning true or false, in LISP.

在Ruby和Scheme中,您可以在名称中使用问号:isThisTrue?。在许多其他语言中,在LISP中,对于名称的“谓词”,存在一个puttng“p”的约定 - null-p,用于返回true或false的测试。

#6


I agree with Will Hartung.

我同意Will Hartung的观点。

If you are worried about (1) then just choose a better name for your boolean. IsNotSomething instead of IsSomething.

如果你担心(1)那么只需为你的布尔值选择一个更好的名字。 IsNotSomething而不是IsSomething。