Is there an beautiful way to assert pre-conditions in Java methods?

后端 未结 8 2176
慢半拍i
慢半拍i 2021-01-02 02:08

A lot of my functions have a whole load of validation code just below the declarations:

if ( ! (start < end) ) {
    throw new IllegalStateException( \"S         


        
8条回答
  •  青春惊慌失措
    2021-01-02 02:39

    For input validation, you can also use Apache Commons Validator.

    Note that input validation should always be enabled. Hence it is conceptually very different from assertion checking (as, e.g., in Eiffel), which can be optionally on/off -- see the answer to this related stack overflow question When should I use Apache Commons' Validate.isTrue, and when should I just use the 'assert' keyword?

提交回复
热议问题