Assert a good practice or not?

前端 未结 7 1334
礼貌的吻别
礼貌的吻别 2021-01-30 05:02

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

相关标签:
7条回答
  • 2021-01-30 05:39

    Based on Sun's guide on assertions, you should not use assertions for argument checking in public methods.

    Argument checking is typically part of the published specifications (or contract) of a method, and these specifications must be obeyed whether assertions are enabled or disabled.

    0 讨论(0)
  • 2021-01-30 05:41

    In very large and poorly designed/maintained systems, if you're looking to improve predictability in methods that are, say, 6000 lines long and nobody in the company understands them anymore, it can be valuable to use the assert keyword to cause development environments to blow up, revealing bugs. But were you to implement those assertions in production, you might shortcircuit a patch that, though horribly conceived, fixed a problem. You want to fix that bad patch by discovering it in the dev environment, not production. So you would turn asserts on at development time, and turn them off in production.

    Another valid use of the assert keyword at development time is to insert validity checks into algorithms that must execute in sub-millisecond times and are well enough insulated from unpredictable or untested callers. You may not be able to afford to preserve the validity check in production in such a case, though it's still very useful in development. On the other hand, if the source of the parameters you're validating is unpredictable or could become so (if it's determined partly by user input, for example), you can probably never afford to skip the check, even in production, and should take the performance hit as a cost of doing business. (In this last case, you probably wouldn't want to use an assert.) But you should opt for asserts to eliminate a production-time validity check only after profiling tells you you simply can't afford the overhead.

    0 讨论(0)
  • 2021-01-30 05:42

    Yes it is good practice.

    In the Spring case, it is particularly important because the checks are validating property settings, etc that are typically coming from XML wiring files. In other words, they are validating the webapp's configuration. And if you ever do any serious Spring-based development, those validation checks will save you hours of debugging when you make a silly configuration mistake.

    But note that there is a BIG difference between a library class called Assert and the Java assert keyword which is used to define a Java assertion. The latter form of assertions can be turned off at application launch time, and should NOT be used for argument validation checks that you always want to happen. Clearly, the Spring designers think it would be a really bad idea to turn off webapp configuration sanity checks ... and I agree.

    UPDATE

    In Java 7 (and later) the java.util.Objects class provides a requireNonNull convenience method to test if an argument is null and raise an exception. You use it like this:

     SomeType t = ...
     SomeType tChecked = Objects.requireNonNull(t);
    

    or

     SomeType tChecked = Objects.requireNonNull(t, "t should be non-null");
    

    However, note that this method raises NullPointerException rather than IllegalArgumentException.

    0 讨论(0)
  • 2021-01-30 05:43

    Yes it's a good idea. You're enforcing the contracting of the interface or class. If there is a contract violation you want to detect it as soon as possible. The longer you wait the more unpredictable the results can be and the harder it can be to diagnose.

    When you explicitly check like this you should also provide an information message that when viewed in a log file can give useful context to help find the root cause or even just to realize you've made a wrong assumption about what the contract is.

    0 讨论(0)
  • 2021-01-30 05:46

    Those asserts are library-supplied and are not the same as the built-in assert keyword.

    There's a difference here: asserts do not run by default (they must be enabled with the -ea parameter), while the assertions provided by the Assert class cannot be disabled.

    In my opinion (for what it's worth), this is as good a method as any for validating parameters. If you had used built-in assertions as the question title implies, I would have argued against it on the basis that necessary checks should not be removable. But this way is just shorthand for:

    public static ParsedSql parseSqlStatement(String sql) {
        if (sql == null)
            throw new IllegalArgumentException("SQL must not be null");
        ...
    }
    

    ... which is always good practice to do in public methods.

    The built-in style of asserts is more useful for situations where a condition should always be true, or for private methods. The language guide introducing assertions has some good guidelines which are basically what I've just described.

    0 讨论(0)
  • 2021-01-30 05:49

    I'm keeping my assertions in released binaries but with modified behavior: abort is not called but stacktrace is collected.

    More details here: http://blog.aplikacja.info/2011/10/assert-to-abort-or-not-to-abort-thats-the-question/

    0 讨论(0)
提交回复
热议问题