When talking about HTTPUrlConnection on his blog Tim Bray gives us the following snippet for checking the HTTP status code
// better check it first
if (http_status / 100 != 2) {
// redirects, server errors, lions and tigers and bears! Oh my!
}
Is http_status / 100 != 2 better or faster than http_status != 200
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.
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.
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.
Is http_status / 100 != 2 better or faster than 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.
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
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:
JavaDoc: Response.Status.Family
Spring's HttpStatus provides methods like is2xxSuccessful()
, is4xxClientError()
etc which can be used to check if the HttpStatus
belongs to any particular family of HTTP status codes.
So if you want to handle the condition if the status code is NOT belonging to 2xx, you can do the below:
if (!HttpStatus.valueOf(http_status).is2xxSuccessful()) {
// redirects, server errors, lions and tigers and bears! Oh my!
}
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").
That said, doing a divide is horrible, and completely obtuse. I would always use the explicit comparison, because then the intent is clear.
One pro in favor of Tim Bray's division method to detect non-200 level messages is that it is easier to unit test.
This method below would need to be tested three different times; 2xx, 1xx, and > 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.
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