何时在客户端使用assert&常见的GWT代码 [英] When to use assert in client & common GWT code

查看:153
本文介绍了何时在客户端使用assert&常见的GWT代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在StackOverflow上有几个问题讨论了何时应该使用assert语句与抛出一些异常的问题。 (示例此处这里这里 here ,以及此处



然而,我怀疑assert-versus-throw的传统观点是基于你在JVM中运行的假设,在GWT宇宙中,你的Java获得音译为JavaScript并在浏览器环境下运行,这组折衷感觉不一样:断言在浏览器中运行时总是被编译掉,并且任何能够缩小JavaScript大小的东西都是一种胜利,特别是如果您是web申请必须运行在手机上。然而,断言确实在DevMode中运行,所以它们在开发过程中有实用性。



所以我的问题是:有没有人对一套最佳实践有任何想法支配如何在GWT中使用assert语句的规则?我已经让我的团队成员问我:自断言被编译出来以后,它有意义吗?,并且我想给他们一个很好的答案。



另外,有没有人对GWT的谷歌开发者在这个问题上的哲学有所了解?看看GWT源代码,它们似乎经常使用它。 .google.com / webtoolkit / doc / 1.6 / FAQ_Client.htmlrel =noreferrer>常见问题


使用断言来进行调试,而不是生产逻辑,因为断言只能在GWT的开发模式下工作。默认情况下,它们被GWT编译器编译掉,所以在生产模式下没有任何效果,除非你明确地启用它们。


与提供给您的问题的答案没有任何区别。无论Java代码是通过javac编译还是通过GWT编译为JavaScript,
assert意味着如果这不是真的,我有一个bug。相比之下,形式代码如果(条件)抛出新的异常(味精); / /

  

的意思是如果这是真的,那么我们有一个意想不到的情况,程序将不得不处理。



至于没有看到断言点的团队成员,请解释他们应该有一堆单元测试,这些单元测试在启用断言的情况下运行。如果测试具有良好的代码覆盖率,并且它们都不会导致断言失败,那么assert语句指示的假设就被证明是成立的。

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.

解决方案

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.

这篇关于何时在客户端使用assert&常见的GWT代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆