Is Groovy's assert a good idea for production code, unlike Java's assert?

后端 未结 1 1905
生来不讨喜
生来不讨喜 2020-12-31 02:16

In Java it\'s known that using the assert keyword is usually a bad idea, as its behavior is dependant on the runtime enviornment (it doesn\'t do anything by def

相关标签:
1条回答
  • 2020-12-31 02:48

    Groovy assert is always executed in production code, and I recommended to use in production. I see the following as being roughly equivalent, but the Groovy version is more compact

    Groovy

    assert file.exists(), "$file does not exist"
    

    Java

    if (!file.exists()) {
        throw new SomeRuntimeException(file + " does not exist");
    }
    
    0 讨论(0)
提交回复
热议问题