Is Java assert broken?

后端 未结 12 780
走了就别回头了
走了就别回头了 2020-12-29 23:33

While poking around the questions, I recently discovered the assert keyword in Java. At first, I was excited. Something useful I didn\'t already know! A more

12条回答
  •  自闭症患者
    2020-12-30 00:27

    This doesn't directly answer your question about assert, but I'd recommend checking out the Preconditions class in guava/google-collections. It allows you to write nice stuff like this (using static imports):

    // throw NPE if listOfStuff is null
    this.listOfStuff = checkNotNull(listOfStuff);
    
    // same, but the NPE will have "listOfStuff" as its message
    this.listOfStuff = checkNotNull(listOfStuff, "listOfStuff"); 
    

    It seems like something like this might be what you want (and it can't be turned off).

提交回复
热议问题