what to choose between require and assert in scala

前端 未结 5 1978
谎友^
谎友^ 2020-12-23 13:37

Both require and assert are used to perform certain checks during runtime to verify certain conditions.

So what is the basic difference bet

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 14:00

    You can see here for a detailed discussion within Scala language.

    I can add that, the key to distinguish between require and assert is to understand these two. These two are both tools of software quality but from different toolboxes of different paradigms. In summary assert is a Software testing tool which takes a corrective approach, whereas require is a design by contract tool which takes a preventive approach.

    Both require and assert are means of controlling validity of state. Historically there were 2 distinct paradigms for dealing with invalid states. The first one which is mainstream collectively called software testing discipline methodologies and tools. The other, called design by contract. These are two paradigms which are not comparable.

    Software testing ensures a code versatile enough to be capable of error prone actions, were not misused. Design by contract controls code from having such capability. In other words Software testing is corrective, and design by contract is preventive.

    • assert is used to write unit tests, i.e. if a method passes all tests each written by an assert expression, the code is qualified as error free. So assert seats besides operational code, and is an independent body.
    • require is embedded within code and part of it to assure nothing harmful can happen.

提交回复
热议问题