There are several questions on StackOverflow discussing the question of when one should use an assert statement versus throwing some exception. (Examples here, here, here, here, and here.
However, I have come to suspect that the conventional wisdom of assert-versus-throw is based upon the assumption that you are running within a JVM. In the GWT universe, where your Java gets transliterated to JavaScript and runs in the context of a browser, the set of tradeoffs feels different: asserts are always compiled-away when running in a browser, and anything that keeps the size of your JavaScript smaller is a win, especially if you web application must run on a mobile handset. Asserts do get run in DevMode, however, so they have utility there during development.
So my questions are: has anybody given any thought as to a set of best-practice rules that govern how to use the assert statement in GWT? I have had members of my team ask me "since the assert gets compiled out, does it make sense to have them?", and I'd like to have a good answer for them.
Also, does anybody have any insight into the philosophy that the developers of GWT at Google have on this subject? Looking at the GWT source code, they appear to use it frequently.
Assertions should be used to check something that should never happen, while an exception should be used to check something that might happen. For example, a function might divide by 0, so an exception should be used, but an assertion could be used to check that the harddrive suddenly disappears.
Don't use asserts for normal error handling Very similar to try and catch , asserts littered all over the code distract the programmer that is reading the code from the business logic. Validating user input — A good program always validates user input, but this should never be done with assertions.
The Syntax of the assert Statement. An assert statement consists of the assert keyword, the expression or condition to test, and an optional message. The condition is supposed to always be true. If the assertion condition is true, then nothing happens, and your program continues its normal execution.
exceptions in Java. Developers use assertions to document logically impossible situations and detect errors in their programming logic. At runtime, an enabled assertion alerts a developer to a logic error. The developer refactors the source code to fix the logic error and then recompiles this code.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With