如您所说,前置条件被定义为在后续代码执行之前必须始终为真的条件。
这意味着在执行其他代码之前在函数开头检查条件的任何内容都被视为前提条件。
示例:
//We will do something cool here
//With an integer input
int doSomethingCool(final int input)
{
//Wait, what if input is null or less than 100?
if(null == input || input < 100)
{
//Return a -1 to signify an issue
return -1;
}
//The cool bit of multiplying by 50. So cool.
final int results = input * 50;
//Return the results;
return results;
}
在示例中,函数 input 在执行任何其他操作之前被检查。只要条件满足,剩下的代码就会执行。
不好的例子:
//We will do something cool here
//With an integer input
int doSomethingCool(final int input)
{
//Want to make sure input is not null and larger than 100
if(null != input && input > 100)
{
//The cool bit of multiplying by 50. So cool.
final int results = input * 50;
//Return the results;
return results;
}
//Return a -1 to signify an issue because the
//preconditions were not met for some reason
return -1;
}
在示例中,前提条件是检查input 是否不是null 并且大于100。这是一个不好的前提条件,因为它可能会引入不必要的ifs 嵌套和循环。
前提条件应该进行检查,并且仅在检查失败时返回。前提条件下不应该做任何工作。
根据 Liskov 替换原则,如果类型 S 是类型 T 的子类型,则类型 T 可以替换为类型 S。如果S 类型覆盖doSomethingCool 并更改前提条件,则它是违规的,因为T 类型是基本定义并定义了必须满足的预期条件。
现在为您解答
是的,简单的条件仍然算作先决条件。只要它们位于使用该变量的所有其他代码的前面,条件就是程序所需的条件。此外,如果函数在子类型中并且正在覆盖父类,那么它不应更改前提条件。
但是,不要将您需要在前置条件中运行的代码括起来。那是不好的做法。如果 value 需要大于 100,请检查 value < 100 并在 if 检查中设置返回值。