• NULLPTR (unregistered)

    private static final Integer[] DIRECTIONS = {null,null,null,null,null};

  • bvs23bkv33 (unregistered)

    private static final Integer[] COMMENTS = { frist };

  • (nodebb)

    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.

  • ian (unregistered)

    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.

  • PJ (unregistered) in reply to Steve_The_Cynic

    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".

  • Martin (unregistered)

    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

  • Maia Everett (github)

    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.

  • Mike5 (unregistered)

    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/

  • (nodebb)

    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.

  • guilty 1 (unregistered)

    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.

  • P (unregistered)

    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:

    012
    345
    678
    

    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.

  • Kattman (unregistered) in reply to P

    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.

  • (nodebb)

    The solution, obviously, is to replace it with an array of strings, "Left", "Up", etc. Then use the string array to get the length.

  • not a robot (unregistered) in reply to Kattman

    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.

  • P (unregistered) in reply to Kattman

    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?

  • scragar (unregistered)

    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.

     private static final Integer[] DIRECTIONS = {
          0 1 2
          3    5
          6 7 8
     };
    

    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.

    public enum CompassDirection {
        NORTHWEST, NORTH, NORTHEAST,
        WEST, EAST,
        SOUTHWEST, SOUTH, SOUTHEAST
    }
    
  • MiserableOldGit (unregistered)

    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.

  • Scott (unregistered)

    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.

  • (nodebb)

    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.

  • 🤷 (unregistered)

    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.

  • P (unregistered) in reply to 🤷

    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.

  • 🤷 (unregistered) in reply to P

    The real real WTF is you. Please do me a favor and catch this: <°)(><

  • Maia Everett (github) in reply to MiserableOldGit

    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.

    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.

    public enum Direction {
        NORTHWEST(0),
        NORTH(1),
        ...
    }
    
  • MiserableOldGit (unregistered) in reply to P

    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

  • (nodebb)

    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...

  • Turtle (unregistered)

    This developer obviously knew of the futility of the ninth position

    http://churchofrobotron.com/#ninth (audio warning)

    I am definitely not a robot.

  • tbo (unregistered) in reply to P

    But why are you doing a power of 2 check?

  • Andrew (unregistered) in reply to Steve_The_Cynic

    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.

  • (nodebb)

    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.

  • Just Me (unregistered)

    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.

  • Your Mammas name (unregistered)

    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.

  • (nodebb)

    Is it really a WTF to use Integer rather than int without a good reason?

  • Anon E Mouse (unregistered) in reply to bvs23bkv33

    Fail... not first and spelled incorrectly... speed WITH purpose :-)

  • JJ (unregistered) in reply to jinpa

    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.

  • sizer99 (google)

    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)

  • BobE (unregistered)

    For all those (expendable) developers who think comments should be added to industry standard practices for their benefit. I have two words . . .

    Job Security

  • MiserableOldGit (unregistered) in reply to jinpa
    Is it really a WTF to use Integer rather than int without a good reason?

    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!

  • Some Ed (unregistered) in reply to 🤷

    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.

  • (nodebb)

    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.

  • Sascha (unregistered)

    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.

  • SG (unregistered) in reply to Kattman

    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.

  • (nodebb)

    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.

  • Ulysses (unregistered)

    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?

  • Chris (unregistered)

    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.

  • 🤷 (unregistered) in reply to Some Ed

    This code doesn't attempt to be self-documenting.

    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.

  • (nodebb) in reply to Chris

    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).

Leave a comment on “Without Directions”

Log In or post as a guest

Replying to comment #:

« Return to Article