When to use assert in client & common GWT code

我是研究僧i 提交于 2019-12-03 12:49:04

Google's FAQ says

Only use assertions for debugging purposes, not production logic because assertions will only work under GWT's development mode. By default, they are compiled away by the GWT compiler so do not have any effect in production mode unless you explicitly enable them.

This isn't any different from the answers given to the questions you linked to. Whether the Java code is being compiled in the usual way by javac or being compiled to JavaScript by GWT, "assert" means "if this isn't true I have a bug." In contrast, code of the form

if (condition) throw new Exception(msg);

means "if this is true then we have an unexpected situation that the program will have to handle."

As for the members of the team that don't see the point of assert, explain that they're supposed to have a bunch of unit tests that run with assertions enabled. If the tests have good code coverage and none of them cause the assertion to fail, then the assumption indicated by the assert statement is demonstrated to hold.

The GWT compiler removes them by default, but you can leave them in if you like. If you think assertions are useful in compiled code, add the -ea command line argument when invoking com.google.gwt.dev.Compiler. The compiler will then turn the Java asserts into JavaScript.

Google Web Toolkit 2.3.0
    Compiler [-logLevel level] [-workDir dir] [-gen dir] [-style style] [-ea] [-XdisableClassMetadata] [-XdisableCastChecking] [-validateOnly] [-draftCompile] [-optimize level] [-compileReport] [-strict] [-localWorkers count] [-war dir] [-deploy dir] [-extra dir] module[s] 

     ...
      -ea Debugging: causes the compiled output to check assert statements
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!