0

对于以下哪种替代方案(此处以 C 语言为例)是否存在共识?

  1. 所有参数的一个断言:

    int f(int m, int n)
    {
       assert((m >= 0) && (m <= mmax) && (n >= 0) && (n <= nmax));
       ...
    }
    
  2. 每个参数一个断言:

    int f(int m, int n)
    {
        assert((m >= 0) && (m <= mmax));
        assert((n >= 0) && (n <= nmax));
        ...
    }
    
  3. 具有原子条件的断言:

    int f(int m, int n)
    {
        assert(m >= 0);
        assert(m <= mmax);
        assert(n >= 0);
        assert(n <= nmax);
        ...
    }
    
4

1 回答 1

1

我个人更喜欢第三个,不仅是为了可读性,也是为了将来的可维护性和调试。想象一下,在编写代码一段时间后,其中一个断言突然开始失败。对于前两个中的任何一个,当其中一个断言失败时,您不确切知道哪个条件为假。

但是对于第三个,绝对没有歧义。

于 2013-01-28T15:06:42.270 回答