- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
This is the kind of code where it is not clear if the guy either deserve a raise or to be fired.
Admin
It was hard to write. It should be hard to read.
Admin
Probably both, or at least promoted to a position where they can't do anymore damage.
Admin
We could call this kind of code “dysfunctional programming”.
Admin
It's not "Functional Programming", it's "Job Security Programming".
And just like every other style of programming it never works quite as well as you would hope.
Admin
But it shouldn't be hard to write.
Addendum 2023-08-10 10:28: Yes, I realize I got the (int) cast syntax wrong. How about the WTF that the "Edit" link doesn't actually allow you to edit?
Admin
The day has finally come when I completely agree with Remy on a WTF. Hallelujah!
Admin
There's actually a problem with the compare to 299, and arguably two problems: we can all agree that a 200 status here is 100% OK. But if the server returns, says, 207 WEBDAV MULTI STATUS, is that really a correct status? I'm more likely to get this from an incorrectly configured server than a functioning one. The Windows docs themselves simply use a comparison to 200 in their example of a good error code.
The "arguable" problem is the readability of the compare: I nearly wrote a paragraph on why the compare to 299 was incorrectly excluding 299 as an OK status code. But that was because I read the code wrong several times until I did a triple check on the syntax. Thank goodness I deleted my complaint, and nobody will know I misread a few lines of code :-)
Admin
Couldn't agree more on >= vs <=. I just was not seeing the distinction. It's lkie raeding thsoe senetnces wtih most wrods misppelled execpt for frist and lsat leetter.
Admin
I'm surprised no one has noted the
fist => frist
Admin
RFC 1945 states "The first digit of the Status-Code defines the class of response" and "2xx: Success - The action was successfully received, understood, and accepted." so I can't rely blame the coder for that.
RFC 4918 states that in certain cases of errors, 207 can be returned, which confuses me (but not enough that I'm going to research it further).
Admin
I wished C# would let you write a single condition as 200 <= (int)StatusCode <= 299, and the compiler would break that up for you.
But my brain is breaking on the fact that they wrote some ass-backwards version of 299 >= (int)StatusCode >= 200. Who does that? The only way this makes sense to me is if they write right-to-left in their preferred (written) language, including how they do maths by hand.
Admin
This looks very boorish and pueril. Clearly one of these many programmers with no regards for coworkers who will need to maintain this code later.
Also it seems like this place doesn't have any code reviews and coding standards to curb this behavior upfront.
Admin
Remy, write down the truth tables for => and <= sometimes… the impleqations are endless!
Admin
Actually, it's a getter, not an initialization at all, every time the property is accessed it evaluates the expression.
An initializer would look like this:
Also, I wouldn't so much say this is "functional" programming so much as using lambda syntax.
Anyway, for readability (while maintaining the original behaviour), I would have written the posted code as:
Admin
Got confused on the => <= as well. But...
That's a clear case of it is bad because you're not used to it. If you were used to it, you wouldn't do a double take. Bad because new, not bad because bad.
Admin
We could do
StatusCode is >= 200 and <= 299
if that makes you happyAdmin
HTTP defines all 2xx error codes as success codes; this code is there absolute correct to not only handle 200.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Admin
Your are not really aware how operators work in C#, are you? :D
Besides, a between operator is a very bad idea because we already have on available in the non-programming language SQL. And what is so bad about it, well because it's double inclusive and most operations especially when it comes to floating point numbers would actually require an non-inclusive part to avoid rounding issues. So in other words, for a between you would need at least four operators, which is so silly, no major programming language has bothered to do so to my knowledge.
Admin
You are obviously not a C# developer.
Pattern matching is the recommended way to do this for numerous reasons I won't get into right now; there's tons of tech talk videos from the languages designers themselves online that give a nice overview why and dozens of links to read for the details.
The only thing wrong with this code is actually that the two conditions are part of the pattern and shouldn't be outside the match.
Admin
This is the problem with the increasing sophistication of parsing technology since the 1980s, it's now possible to create languages in which you can write code that requires the consumption of at least two Camberwell carrots to understand.
Admin
I'm only talking about writing e.g. 0 <= x < 10 as syntactic sugar, and the compiler splits that into 0 <= x && x < 10. You could use any combination of comparison operators (<, <=, >=, >), although you'd want either both less-thans or both greater-thans, not a mix of both (0 > x < -2 is just stupid). It just looks that little bit more obvious what the conditional is testing.
I would hate to use if (x between 0 and 10), as that isn't immediately clear whether or not it's inclusive on both ends, which often you don't want. Even if it is clearly specified in the documentation what "between" meant.
Admin
Swift half does it. It defines two kinds of range: a closed range and a half open range. In Swift, the condition can be written as
(200 ... 299).contains(status)
or(200 ..< 300).contains(status)
.Admin
Yes, that is to say, it is a success. Of course, if the server isn't expected to be a WebDAV server, the body of the response may be incomprehensible to the client. But that could happen even with a 200.
Admin
Http success status codes have different meanings. If I expect status 200 and get status 206, as a developer I want to know what’s going on and then decide if the outcome is something that I can use or not.