断言好的做法与否? [英] Assert a good practice or not?

查看:169
本文介绍了断言好的做法与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Assert作为函数参数来强制执行其有效性是一种好习惯。我正在浏览Spring Framework的源代码,我注意到他们使用了 Assert.notNull 。这是一个例子

Is it a good practice to use Assert for function parameters to enforce their validity. I was going through the source code of Spring Framework and I noticed that they use Assert.notNull a lot. Here's an example

   public static ParsedSql parseSqlStatement(String sql) {
    Assert.notNull(sql, "SQL must not be null");}

这是另一个:

public NamedParameterJdbcTemplate(DataSource dataSource) {
            Assert.notNull(dataSource,
                    "The [dataSource] argument cannot be null.");
            this .classicJdbcTemplate = new JdbcTemplate(dataSource);
        }

        public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) {
            Assert.notNull(classicJdbcTemplate,
                    "JdbcTemplate must not be null");
            this .classicJdbcTemplate = classicJdbcTemplate;
      }

仅供参考, Assert.notNull (不是 assert 语句)在util类中定义如下:

FYI, The Assert.notNull (not the assert statement) is defined in a util class as follows:

public abstract class Assert { 
   public static void notNull(Object   object, String   message) {
      if (object == null) {
          throw new IllegalArgumentException  (message);
      }
   }
}


推荐答案

原则上,断言与其他许多报道没有什么不同 - 时间检查。

In principle, assertions are not that different from many other run-time checkings.

例如,Java绑定检查运行时的所有数组访问。这会让事情变慢吗?是。这有益吗?绝对!一旦发生越界违规,就会抛出异常并且程序员会收到任何可能的错误警报!在没有绑定检查数组访问的其他系统中的行为是多么不可预测的! (通常会带来灾难性的后果!)。

For example, Java bound-checks all array accesses at run-time. Does this make things a bit slower? Yes. Is it beneficial? Absolutely! As soon as out-of-bound violation occurs, an exception is thrown and the programmer is alerted to any possible bug! The behavior in other systems where array accesses are not bound-checked are A LOT MORE UNPREDICTABLE! (often with disastrous consequences!).

无论是使用图书馆还是语言支持,断言在精神上都是相似的。有性能成本,但绝对值得。事实上,断言更有价值,因为它是明确的,它传达了更高层次的概念。

Assertions, whether you use library or language support, is similar in spirit. There are performance costs, but it's absolutely worth it. In fact, assertions are even more valuable because it's explicit, and it communicates higher-level concepts.

正确使用,性能成本可以最小化,价值,客户(谁会尽快赶上合同违规)和开发人员(因为合同自我执行自我记录)最大化。

Used properly, the performance cost can be minimized and the value, both for the client (who will catch contract violations sooner rather than later) and the developers (because the contract is self-enforcing and self-documenting), is maximized.

另一种看待它的方法是将断言视为主动评论。没有人认为评论是有用的,但它们是被动的;他们在计算上什么都不做。通过将一些概念描述为断言而不是注释,它们变为活动。他们实际上必须在运行时保持;违规行为将被捕获。

Another way to look at it is to think of assertions as "active comments". There's no arguing that comments are useful, but they're PASSIVE; computationally they do nothing. By formulating some concepts as assertions instead of comments, they become ACTIVE. They actually must hold at run time; violations will be caught.

参见:用断言编程的好处

这篇关于断言好的做法与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆