Java 检查“http_status / 100 != 2”比“http_status != 200”更好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9759826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is checking "http_status / 100 != 2" better than "http_status != 200"
提问by Ravi Vyas
When talking about HTTPUrlConnection on his blogTim Bray gives us the following snippet for checking the HTTP status code
在他的博客上谈论 HTTPUrlConnection 时,Tim Bray 为我们提供了以下用于检查 HTTP 状态代码的片段
// better check it first
if (http_status / 100 != 2) {
// redirects, server errors, lions and tigers and bears! Oh my!
}
Is http_status / 100 != 2better or faster than http_status != 200
http_status / 100 != 2是否比http_status != 200更好或更快
采纳答案by John Feminella
The reason that's done is because status codes are integers, so this expression will be an integer division.
这样做的原因是因为状态代码是整数,所以这个表达式将是一个整数除法。
The integer division means that all successful HTTP status codes(i.e., those from 200-299) will make the expression false, not just 200.
整数除法意味着所有成功的 HTTP 状态代码(即 200-299 的那些)将使表达式为假,而不仅仅是 200。
Not to nitpick on Tim Bray, but if I was writing this myself and wanted to convey my intent clearly, then for readability purposes I'd probably want to see something more like !statusCode.isSuccessful
. If you didn't know that HTTP 2xx meant successful status codes, it wouldn't be obvious what the intent of the integer division was.
不是对 Tim Bray 挑剔,但如果我自己写这篇文章并想清楚地表达我的意图,那么出于可读性目的,我可能希望看到更像!statusCode.isSuccessful
. 如果您不知道 HTTP 2xx 意味着成功的状态代码,那么整数除法的意图是不明显的。
Of course, integer division is probably more performant than making a bunch of hypothetical StatusCode objects and then doing isSuccessful
method dispatch on them. And performance is probably a key goal for a network library class.
当然,整数除法可能比创建一堆假设的 StatusCode 对象然后isSuccessful
对它们进行方法分派更高效。性能可能是网络库类的关键目标。
Is http_status / 100 != 2 better or faster than http_status != 200?
http_status / 100 != 2 比 http_status != 200 更好还是更快?
It won't be faster (two operations vs. one), but whether it's "better" is an apples-to-oranges comparison since those two operations have different behavior.
它不会更快(两个操作对一个),但它是否“更好”是一个苹果对橘子的比较,因为这两个操作具有不同的行为。
回答by alex
Assuming http_status
is an integer (so the division returns an integer), it's not better or faster, but different.
假设http_status
是一个整数(所以除法返回一个整数),它不是更好或更快,而是不同。
It will allow any 2nn
status code to trigger that condition. A 2nn
status code...
它将允许任何2nn
状态代码触发该条件。一个2nn
状态代码...
...indicates the action requested by the client was received, understood, accepted and processed successfully.
...表示客户端请求的操作已成功接收、理解、接受和处理。
来源。
回答by Oliver Charlesworth
http_status / 100 != 2
is not the same as http_status != 200
. It's essentially equivalent to (http_status < 200 || http_status > 299)
(remember that anything in that range constitutes "success").
http_status / 100 != 2
不一样http_status != 200
。它本质上等同于(http_status < 200 || http_status > 299)
(请记住,该范围内的任何内容都构成“成功”)。
That said, doing a divide is horrible, and completely obtuse. I would always use the explicit comparison, because then the intent is clear.
也就是说,做除法是可怕的,而且完全是迟钝的。我总是使用显式比较,因为这样意图就很明确了。
回答by Jose Martinez
One pro in favor of Tim Bray's division method to detect non-200 level messages is that it is easier to unit test.
支持 Tim Bray 检测非 200 级消息的除法方法的一位专业人士是,它更容易进行单元测试。
This method below would need to be tested three different times; 2xx, 1xx, and > 299.
下面的这个方法需要测试三个不同的时间;2xx、1xx 和 > 299。
(http_status < 200 || http_status > 299)
This method requires only two.
这种方法只需要两个。
http_status / 100 != 2
This is not to say that it is always better to use the division method versus the compare, but it is one point worth making. In a project I am working on, where the speed diff between these two methods is not an issue, I prefer Tim Bray's division method because it leads to one less test case to have to test. We have strict guidelines for code coverage.
这并不是说使用除法比使用比较总是更好,但这是值得提出的一点。在我正在进行的一个项目中,这两种方法之间的速度差异不是问题,我更喜欢 Tim Bray 的除法方法,因为它可以减少一个需要测试的测试用例。我们对代码覆盖率有严格的指导方针。
回答by Reinaldo
I've seen many codes with hard coded validation, and had problems with this aproach frequently.
我见过很多带有硬编码验证的代码,并且经常遇到这种方法的问题。
When I do refactoring on this kind of code, the aproach I use the most is implementing the verification with a class from javax-ws: javax.ws.rs.core.Response.Status.Family
当我对这种代码进行重构时,我最常用的方法是使用 javax-ws 中的一个类来实现验证: javax.ws.rs.core.Response.Status.Family
something like this:
像这样:
if(Response.Status.Family.familyOf(responseCode).equals(Response.Status.Family.SUCCESSFUL)){
//do your thing
}
You can also check for other kinds of status:
您还可以检查其他类型的状态:
- INFORMATIONAL- 1xx
- SUCCESSFUL- 2xx
- REDIRECTION- 3xx
- CLIENT_ERROR- 4xx
- SERVER_ERROR- 5xx
- 信息- 1xx
- 成功- 2xx
- 重定向- 3xx
- CLIENT_ERROR- 4xx
- SERVER_ERROR- 5xx
JavaDoc: Response.Status.Family
JavaDoc:Response.Status.Family
回答by pyb
Please note that RFC 4918introduced the status code 207 Multi Status
. This was intended for WebDAV but some developers may find it handy when the request relates to several resources (for instance: deleting a bunch of orders).
请注意RFC 4918引入了状态代码207 Multi Status
。这是为 WebDAV 设计的,但是当请求涉及多个资源(例如:删除一堆订单)时,一些开发人员可能会发现它很方便。
While starting with the number 2, a 207
response is expected to provide information about the status of the individual resources(such as orders). It is possible that one of those statuses would be an error.
而开始以数字2,一个207
响应预计将提供有关各个资源的状态的信息(例如,订单)。这些状态之一可能是错误。
I know this is far fetched, in doubt better check the API you're consuming.
我知道这很牵强,有疑问最好检查您正在使用的 API。
回答by Madhu Bhat
Spring's HttpStatusprovides methods like is2xxSuccessful()
, is4xxClientError()
etc which can be used to check if the HttpStatus
belongs to any particular family of HTTP status codes.
Spring 的HttpStatus提供了诸如等方法is2xxSuccessful()
,is4xxClientError()
可用于检查 是否HttpStatus
属于任何特定的 HTTP 状态代码系列。
So if you want to handle the condition if the status code is NOT belonging to 2xx, you can do the below:
因此,如果您想处理状态代码不属于 2xx 的情况,您可以执行以下操作:
if (!HttpStatus.valueOf(http_status).is2xxSuccessful()) {
// redirects, server errors, lions and tigers and bears! Oh my!
}