我有一个函数可以完成一些(可能很长)工作(defn workwork [x] ...),还有一些其他函数可以提前检查调用是否会成功(defn workwork-precondition-1 [x] ...)。
workwork每次调用时都应评估前置条件函数(例如使用:pre)。前置条件函数也应该在单个函数中收集(和:ed)并直接供客户端代码使用(例如禁用按钮)。
在 Clojure 中解决此问题同时避免代码重复的惯用方法是什么?
特别是,有没有办法在不运行函数体的情况下评估函数的前提条件?
有没有办法为前置和后置条件指定自定义错误/失败消息,类似于Predicate_Failurefor 谓词?我似乎无法在官方文档中找到任何内容。TIA。
design-by-contract predicate ada preconditions post-conditions
假设您有一个具有一些前置条件和后置条件的方法.是否可以为每个未完成的前置条件创建异常类?例如:未完成pre1意味着抛出notPre1Exception实例.
想象一个默认属性:
class Positive {
public int Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想补充一个先决条件:set价值只能是正数.是否可以在不添加成员变量样板的情况下执行此操作?
public int Value { get;
set {
if(value < 0) throw new ArgumentOutOfBoundsException();
// continue doing 'the default thing'
// instead of `value_=value`, mirrored by a change in the
// get, and adding the `int value_` member variable
}
};
Run Code Online (Sandbox Code Playgroud) 我曾经在我的代码中插入java1.4的断言构造并发现它非常实用,因为它允许我在调试时启用插入的断言并在编译时禁用它们.
我的问题是:
是否可以对Guava库中的现代Preconditions.checkArgument(..)等做同样的事情?
这一点很重要.我们可能在代码中有大量的番石榴前置条件检查,但是大多数是用于调试目的,并且当这些前提条件的数量很快变大时可能会影响性能.
谢谢你的想法.
我仍然是Ada的新手,我认为我误解了Preconditions的使用,因为通过GNAT RM查看似乎检查不会在运行时执行.此外,此处的GNAT RM for Precondition不指定在不满足前提条件时抛出哪个异常.
这是我正在尝试的代码:
procedure Test is
begin
generic
type Element_Type is private;
use System.Storage_Elements;
procedure Byte_Copy (Destination : out Element_Type;
Source : in Element_Type;
Size : in Storage_Count := Element_Type'Size)
with Pre =>
Size <= Destination'Size and
Size <= Source'Size;
procedure Byte_Copy (Destination : out Element_Type;
Source : in Element_Type;
Size : in Storage_Count := Element_Type'Size)
is
subtype Byte_Array is Storage_Array (1 .. Size / System.Storage_Unit);
Write, Read : Byte_Array;
for Write'Address use Destination'Address; …Run Code Online (Sandbox Code Playgroud) 例如,我有以下代码:
public class Calc(){
final int PI = 3.14; //is this an invariant?
private int calc(int a, int b){
return a + b;
//would the parameters be pre-conditions and the return value be a post-condition?
}
}
Run Code Online (Sandbox Code Playgroud)
我只是对这些术语的确切含义感到困惑?上面的代码就是我认为的样子,但是有人能指出我的理论正确方向吗?
假设我拥有世界上最愚蠢的环形缓冲区.
size: constant := 16;
subtype T is integer;
package RingBuffer is
procedure Push(value: T);
function Pop return T;
end;
package body RingBuffer is
buffer: array(0..size) of T;
readptr: integer := 0;
writeptr: integer := 1;
procedure Push(value: T) begin
buffer(writeptr) := value;
writeptr := (writeptr + 1) mod size;
end;
function Pop return T
begin
readptr := (readptr + 1) mod size;
return buffer(readptr);
end;
end;
Run Code Online (Sandbox Code Playgroud)
因为我的代码很糟糕,我想添加前置条件和后置条件,以使我不会滥用这个肯定.所以我改变Push的实现如下:
procedure Push(value: T) with
pre => readptr /= writeptr
is begin
buffer(writeptr) …Run Code Online (Sandbox Code Playgroud) 基于 Liquibase xml 的变更集块:
<preConditions>
<not>
<tableExists tableName="alarm" schemaName="public"/>
</not>
</preConditions>
Run Code Online (Sandbox Code Playgroud)
在“liquibase 格式的 sql”中应该如何?Liquibase 的官方文档只给出基于 xml 的
我有这个功能:
(defn executa-peso-individuo
[estado-individuo transicao-individuo]
(def tipos-transicoes-peso #{:troca-peso :mesmo-peso})
(def tipos-estados-peso #{:d :e})
{:pre [(contains? tipos-transicoes-peso
(:peso transicao-individuo))
(contains? tipos-estados-peso
(:peso estado-individuo))]
...
Run Code Online (Sandbox Code Playgroud)
先决条件不起作用.不知何故,vars tipos-transicoes-pes和tipos-estados-peso在前置条件代码中创建了一个bug.我知道我可以将这些变量放在我的功能之外以使其工作.但我想将这些定义保留在我的功能中.我怎样才能做到这一点?
据我所知,在更改某些对象状态之前,我们使用Guava Preconditions快速失败(这是stackoverflow的一个很好的答案).这很好.但是它会抛出运行时异常,这不是应用程序用户最喜欢的异常(500错误等等......).所以我需要你在设计方面给我一些帮助.
我有一个声明许多方法的接口.每个方法都有必须控制的参数(例如:not null).所以在实现类中我使用如下指令:
Preconditions.checkNotNull(fooObj);
Run Code Online (Sandbox Code Playgroud)
但是,调用此API的程序可能会因运行时异常而崩溃,在本例中为NullPointerException.
那么你如何处理这些未经检查的异常呢?
谢谢.
--------编辑应用层:
数据访问层
API声明交换DTO的方法
使用Guava实现API并检查参数的过程
Web服务取决于流程层