- 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
private static final Integer[] DIRECTIONS = {null,null,null,null,null};
Admin
private static final Integer[] COMMENTS = { frist };
Admin
There are places in the world where the number 4 is considered unlucky, in fact. It's a sort of collision between language and culture, given that in those places the word for "four" and the word for "death" sound similar. It's considered catastrophically impolite to give people four of something as a gift.
Admin
Perhaps it's an attempt to avoid off-by-one errors. Add one to each value and you get the 8 directions on the numpad. 4 becomes 5 which is in the middle.
Admin
Came here to say exactly that!
Or they started at 0 - because 0-based indexing - but just suffered a brain-f*rt, missed a number, but reached number 8 and thought "well, 8 directions, that looks right".
Admin
My guess is that the directions are as the numbers are laid out on a number pad, but from 0 instead of 1. Then 4 is in the center, which is no direction. 0, 1, and 2 for down-left, down, and down right. 3 and 5 for left and right. 6, 7, and 8 for up-left, up, up-right.
Don't know how these comments handle newline, but I'll give it a try:
789 3.5 012
Admin
My theory why it has 8 and not 4 is because if you number elements of a 3x3 matrix like this:
0 1 2 3 4 5 4 7 8
then 4 corresponds to the center, which is not a direction.
Addendum 2020-02-10 06:52: The second 4 should be a 6, and it was meant to be a 3x3 table, not one line.
Admin
Why 4? I'm sure it's nothing personal. The developer surely just wanted to remove a random number from the list. https://xkcd.com/221/
Admin
I have a feeling it may have to do with the number pad. 0 = 1, 1 = 2, etc. 4 is excluded because it's the #5 which doesn't indicate a direction.
Admin
I agree with Maia Everett; this looks like a 0-based 3x3 matrix with the center (i.e. 4) being left out. Maybe a WTF to fix a prior WTF.
Admin
TRWTF is Remy being ignorant again. It's a very common technique in the old days (especially game dev) to represent a direction as integers 0 to 8 like this:
Then disregard 4 since it's not a valid direction, and you get this line of code. This is used because you can get the x and y differences very easily: modulo 3 and division by 3 respectively, instead of having lots of unnecessary switch statements.
I mean, if you can accept using bitmasks and bitwise operations to operate on flags, why not this too? It's not even a WTF. It's just not knowing how it's been done.
Admin
Which is exactly the WTF. There should be a comment in the code that lives with the code to describe why the code is doing this. Even if it is a single line that might reference a more detailed explanation in a larger document. The fact that there is no comment is the WTF here.
Admin
The solution, obviously, is to replace it with an array of strings, "Left", "Up", etc. Then use the string array to get the length.
Admin
If missing comment is enough to qualify the entry here then we are getting pretty low on wtf quality. Seriously, it took me about 2 seconds to solve this captcha, erm, I ment mystery.
Admin
Saying this is a WTF and requires a comment is like saying doing power of 2 check by
n & (n - 1) == 0
is a WTF because it's not accompanied by a comment. It shows the poster's idiocy and ignornace more than then actual quality. I thought TDWTF is for showcasing perversion, not things layman don't understand?Admin
You'd think if you wanted to express it's use as a grid with 4 being the centre cell and missing you'd write it out as a grid.
This would have made it clear that the 4 is the centre cell in the representation and thus it's missing because staying put isn't a direction.
Alternatively you could use an Enum for it and thus your directions are just the options for your enum which could also be easily named for the sake of making it easy to follow.
Admin
Whether missing a comment here is a WTF rather depends on the age and context of this code fragment. Doesn't have to have been a game, btw, could be anything where you need to be pushing the screen or cursor about ... CAD, GIS, image editor to name but a few.
Reminds me of having to explain the metaphor behind circular "radio buttons" (aka "option selectors") to a bunch of people five years younger than me and thus never exposed to those funny old car radios with clunky preset buttons. It would never occur to me to that needing explaining to anyone 20 years ago, but I guess if some of my old code using that type of UI element (or probably many other forgotten things) rocked up then we'll see Remy gleefully write articles about it.
Clearly we have run out of real WTFs to scoff at.
Admin
Because 4 represents the time, in which one can only move forward.
This developer is clearly a genius, having figured out dimensions 5-8. Safety not guaranteed.
Admin
Number pad theory doesn't make it less of a WTF. Leaving the length usage aside, I can't think of a purpose these numbers in an array can serve.
Admin
This code snippet is the prime example of why "MY CODE IS SELF DOCUMENTING!!!!111" is just a hoax. We can only guess here what was meant by the implementation. Even the original developer doesn't have a clue anymore, it seems. So, a simple comment like "//represents a 3x3 matrix, 4 is the center and therefore excluded" would immediatly end all the guessing work and tell us the WHY (and not the WHAT). Which is what comments should do: Tell us, why the code does what it does. Not tell us what the code does.
Admin
Every algorithm is self-documenting in the eyes of something inside the domain who knows the standard techniques used in the field. For everyone else it always looks like obscure pieces of "magic". Nobody owes an uneducated layman like you (or Remy for this regard) an explanation. Depending on the context making comments might actually be TRWTF, if programmers are expected to know the domain knowledge before allowed to touch it.
And no, "tell us the WHY and not the WHAT" is the real hoax there. If you don't understand a damn about the code base, everything belongs to the WHY category and not WHAT category, but nobody will treat you seriously if you just bitch about the code base at that state. A good WTF doesn't just mock things because the poster don't understand why: that's for the low quality filler WTFs when Remy is on drugs again. Good WTFs are even when you know WHY and WHAT it's doing, it's still absurd: the twisted logic and contorted path that composes the result.
Complaints like yours are ridiculous and stupid: just because you don't know a specific field doesn't make something a WTF. Have you considered to git gud before mocking things you don't understand? The entire programmer ecosystem will thank you for that.
Admin
The real real WTF is you. Please do me a favor and catch this: <°)(><
Admin
Oh, so that's why they're called radio buttons?
Regarding the original post, I would say magic numbers are a code smell, even if their meaning is obvious to someone in the know. Java has powerful enum classes; this array could have easily been reorganized into an enum with the number becoming an instance field of the enum constant, i.e.
Admin
Seems plenty of people easily worked out what this was meant to be doing without comment or context, just based on experience. That doesn't include me, but I never wrote this sort of thing and I don't expect to have my hand held. We'd never get a chunk of code finished if we had to document/comment for people unfamiliar with the domain. Function names, variable names and patterns should be nice and obvious, and comments then only need to explain assumptions and weird stuff.
Perhaps the first two comments in any module should be;
// It is assumed you are a competent programmer who knows what to do with this language // If you are not familiar with the terminology and methods in whatever industry sector get some help/read a book/ do a course or you will get confused and make a fool of yourself
Admin
oh no, the original developer was just a chinese guy. And in China, 4 is a bad number, avoid it ! You'll find many tall buildings with out a 4th floor, and never ask for rooms 4, 44, or 444 in a hotel...
Admin
This developer obviously knew of the futility of the ninth position
http://churchofrobotron.com/#ninth (audio warning)
I am definitely not a robot.
Admin
But why are you doing a power of 2 check?
Admin
I was thinking this also. I recall, when working at HP, that they had difficulty marketing the LaserJet 4 to certain regions of Asia for this cultural point.
Admin
Add another vote for this being a zero-indexed number pad. It's a naming failure unless the context makes it clear what's going on but this doesn't rise to the level of a WTF.
Admin
I agree with the others on the code smell of the hard-coded numbers. I'm not sure if they should be an enum (available since Java 5) or a final int for each direction. It kinda depends on when this code was written and how extensively they are used in math.
Admin
If anything the WTF is that programming history has been forgotten.
This was a pretty standard method of using the number pad and people from the right generation and industry would have seen it and recognised it. Sure there's no comments, but you don't comment every line of code, and you certainly don't do it for the ones where you expect anyone who is working on that type of code to know about.
Admin
Is it really a WTF to use Integer rather than int without a good reason?
Admin
Fail... not first and spelled incorrectly... speed WITH purpose :-)
Admin
Usually, yes. Depends on what you're doing. If you're fine with paying for constructing the object and having an extra level of indirection, go nuts.
The fact that autoboxing hides those costs doesn't mean it's free.
Admin
Somewhere in the code there are probably offset arrays like:
private static final Integer[] XMOVE = { -1, 0, 1, -1, 1, -1, 0, 1 };
private static final Integer[] YMOVE = { -1, -1, -1, 0, 0, 1, 1, 1 };
that DIRECTIONS indexes into. Though honestly I'd rather just leave the center point in with X and Y moves of 0, 0 and get rid of DIRECTIONS ( i == i ). Maybe they were using it for a drunkard's walk and didn't want the non-moving option.
Addendum 2020-02-10 14:12: Which of course, is why something like this needs a comment, even a really quick on. It's being CLEVAR (i.e., 'clever' but not self-obviously so)
Admin
For all those (expendable) developers who think comments should be added to industry standard practices for their benefit. I have two words . . .
Job Security
Admin
Yeah, that's the one I can't see an obvious answer for, unless it's something to do with the java object/event they use to pick up key-presses? Not done much Java myself. A bit trivial though for a WTF, as you say, but this code probably predates enums in java, and maybe even predates verbose commenting and variable names. And now its context has gone we'll never know where it came from ... maybe it was the nav controls for Elite n the Beeb? Imagine trying to read that code base!
Admin
This code doesn't attempt to be self-documenting.
For what it's worth, I used to be a self-documenting coder, and I'd find this to be pretty cryptic, even though I did recognize the number pad pattern right off. These days, I'm a self-documenting coder who still puts in a suffusion of comments, because I've had people asking me what's going on with code that used some 40 character variable names and subroutine names to spell out exactly what was going on.
Admin
The unfortunate thing about this hunk of code is that we don't know what it does from this small example. It is the job of the original programmer to inform ALL of us, including the original programmer. I look back on code I did just 6 months ago, and wonder what was going on. I thank my lucky stars I wrote something informative when I committed things.
The lesson here folks is: Write comments for yourself, for in a VERY small time interval, you will forget EVERYTHING that brought you to the conclusion that is your code.
Admin
Language has idioms and jargons. As many posters pointed out, this line would be obvious to any dev who spent some time on pc gaming. In this particular community, no one needs to comment this line.
Admin
Given that most of us seem to have recognised what's going on without any need for a comment, it's obviously not that obscure. A comment would obviously be helpful, but clearly not required.
Admin
An esolang I use has a command that queries its canvas in all 8 directions from the current position. However, the movement command uses the directions in reverse order starting in a different direction... maybe I should ask the author to harmonise the two.
Admin
Martin's comment is exactly what I thought. This article is a real stretch anyway. Also, would it kill Remy to catch his blatant Markdown syntax error?
Admin
I would like to congratulate P for knowing that clever little trick to determine if x is a power of 2. I happen to know it, only because I've had to read the documentation for the java.util.Random.nextInt(int) method. I've got to say, while it is clever, it is not self-documenting. If I hadn't seen a comment on what it was doing, it would not have been obvious to me. Having read the comment, I can see exactly what it's doing, and am happy to accept that it works without there being some edge case where it doesn't, even though I haven't spent the effort proving that to myself.
Here's the thing though P. Not every programmer, including the good ones, know this trick. Not every programmer needs to know this trick. Some people can go an entire career without needing to know if a given number is a power of two. I'm pretty sure, on the balance of probabilities, that you did not come up with this trick, and only know about it because you learnt it somewhere else. So lets not develop a superiority complex because something that is now obvious to you is not immediately obvious to everyone else.
I do maths for a living, working on code with other mathematicians, and if I use a clever little trick like that, you bet there will be a comment immediately above saying "Check if x is a power of 2". I won't explain why it works, I'll just leave it at that, but the comment will be there.
Admin
But maybe the original author thought it was self-documenting. I mean, I certainly have been there: Thinking "Nah, I don't need comments, it's obvious what I do here!" only to come back to the project 2 months later and having no clue what I was trying to accomplish.
Admin
I don't know where I first saw that test; it may have been here:
https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
Although less efficient in terms of assembly instructions, I actually prefer this variant:
(n & -n) == n
(it has the same problem with zero of course).