Ada 函数头中的可选注释部分

发布于 2024-12-26 07:48:08 字数 1564 浏览 1 评论 0原文

当读者开始阅读函数代码时,他应该已经非常清楚它是做什么的,它是如何做的,以及他可能会遇到什么问题。我正在尝试编写干净、结构化、注释良好且易于理解的代码。我正在阅读《Ada 风格指南》和一些我不太理解的内容,我可以为可选部分写什么(例如:@Error_Handling、@Pre、@Post)。 我想用一个例子来表示这个函数。使用上述准则,可以导出标准函数头:

--  ---------------------------------------------------------------------------
--  @Function: Arithmetic_Mean
--
--  @Description:
--    Function to find the mean of a numeric vector. The program should
--    work on a zero-length vector (with an answer of 0.0).
--  @Usage: (opt)
--  @Parameter:
--    +Num: Given array
--  @Return: Averages/Arithmetic mean or zero
--  @Error_Handling: (opt)
--  @Pre: (opt)
--  @Post (opt)
type Mean_Numbers is array (Natural range <>) of Float;
function Arithmetic_Mean (Num : Mean_Numbers) return Float is
   Sum : Float := 0.0;
begin
   if Num'Length > 0 then
      while Num'First <= Num'Last loop
         Sum := Sum + Num(Num'First );
      end loop;
      return Sum / Float (Num'Length);
   end if;
   return 0.0;
end Arithmetic_Mean;

这是另一个示例:

-------------------------------------------------------------- ... --
--  @Function: Get_Index
--  @Description:
--     Returns the minimum index of Item in A.
--  @Parameters:
--     +A: the array
--     +Item: element searched for
--  @Return:
--     The minimum index of Item in A.
--  @Pre:
--    true
--  @Post:
--     if exists 1 <= I <= UPPER_BOUND: A(I) = Item then
--       result = min {1 <= k <= UPPER_BOUND | a(j) = item }
--    else
--       result = 0

When a reader starts to read the function code, he should already have a very good idea of what it is does, how it does it, and what problems he might meet. I'm trying to write clean, structured, well-commented code that is easy to understand. And I'm reading Ada Style Guide and some things I didn't understand well enough, what can i write for optional sections (for exapmle: @Error_Handling, @Pre, @Post).
I want to represent this Function like an example. Using the above guidelines, a standard function header may be derived:

--  ---------------------------------------------------------------------------
--  @Function: Arithmetic_Mean
--
--  @Description:
--    Function to find the mean of a numeric vector. The program should
--    work on a zero-length vector (with an answer of 0.0).
--  @Usage: (opt)
--  @Parameter:
--    +Num: Given array
--  @Return: Averages/Arithmetic mean or zero
--  @Error_Handling: (opt)
--  @Pre: (opt)
--  @Post (opt)
type Mean_Numbers is array (Natural range <>) of Float;
function Arithmetic_Mean (Num : Mean_Numbers) return Float is
   Sum : Float := 0.0;
begin
   if Num'Length > 0 then
      while Num'First <= Num'Last loop
         Sum := Sum + Num(Num'First );
      end loop;
      return Sum / Float (Num'Length);
   end if;
   return 0.0;
end Arithmetic_Mean;

And here is another example:

-------------------------------------------------------------- ... --
--  @Function: Get_Index
--  @Description:
--     Returns the minimum index of Item in A.
--  @Parameters:
--     +A: the array
--     +Item: element searched for
--  @Return:
--     The minimum index of Item in A.
--  @Pre:
--    true
--  @Post:
--     if exists 1 <= I <= UPPER_BOUND: A(I) = Item then
--       result = min {1 <= k <= UPPER_BOUND | a(j) = item }
--    else
--       result = 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鼻尖触碰 2025-01-02 07:48:08

@Pre@Post 标签应记录模块的 按合同设计。正如您所观察到的,任何 先决条件 必须为 true 才能成功执行,并且任何 postcondition 是您的代码要履行的承诺。 @Error_Handling 标记标识您如何处理其他两个标记的违规行为。

作为一个具体示例,您的 Arithmetic_Mean 实现会默默地忽略空输入数组,返回平均值为零;它传播引发的任何异常。这些是应该记录的行为。

可以带来以下几个好处:

  1. API 程序员可以清楚地说明预期的行为。
  2. API 客户端可以区分可能的错误来源。
  3. 审核者可以验证意图是否与实现相匹配。

另请参阅Ada 简介:按合约进行设计,它说明了 Ada 2012 对执行合同的支持。 Ada 2012 的基本原理 提供 主题和相关方面的概述

The @Pre and @Post tags should document your module's approach to Design by Contract. As you observed, any precondition must be true for successful execution, and any postcondition is a promise to be fulfilled by your code. The @Error_Handling tag identifies how you deal with violations of the other two.

As a concrete example, your implementation of Arithmetic_Mean silently ignores an empty input array, returning a mean of zero; it propagates any exceptions that are raised. These are the behaviors that should be documented.

Several benefits accrue:

  1. The API programmer can clearly state the intended behavior.
  2. The API client can distinguish among possible sources of error.
  3. A reviewer can verify whether the intention matches the implementation.

See also Introduction to Ada: Design by contracts, which illustrates Ada 2012 support for enforcing contracts. The Rationale for Ada 2012 offers an overview of the topic and related aspects.

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