• (unregistered)

    Next lesson: nested loops! :)

    NeoThermic

  • (unregistered)

    Well, also, they're setting the values to the default values in the array already.

  • (unregistered)

    <FONT size=+0><FONT color=#0000ff>String</FONT>[][] reservationHistory = <FONT color=blue>new String</FONT>[100][20];</FONT>

    <FONT size=3>reservationHistory[x][0] = <FONT color=blue>null</FONT>;
    </FONT><FONT size=3>// ...
    reservationHistory[x][18] = <FONT color=blue>null</FONT>; </FONT>

    <FONT size=3></FONT> 

    <FONT size=+0>Shouldn't he be doing 0-19 rather than 0-18?</FONT>

  • (unregistered) in reply to

    It would seem as if. But... the array initializes to null anyway... does he even need the loop? [^o)]

  • (unregistered)

    Sadly, this is somewhat common to see... alot of people don't seem to get loops.

    I've gotten used to it. It's no longer a WTF in my eyes, but rather just code produced by someone that doesn't know better.

    It's kind of like a person that has to move some furniture, yet doesnt realize they can put all of it in the truck at once and instead make several trips back and forth to get one piece of furniture each time. :)  

  • Mike R (cs)

    I like his use of the [6] and [8] characters [:P]

  • (unregistered) in reply to

    furniture can be heavy though. It's more like someone printing out a 20 page report one page at a time... :)

  • Jeff S (cs)

    I think he understands loops (since he is using one) -- but he doesn't understand that you can NEST loops  ...

  • Maurits (cs) in reply to

    Nested loops? pshaw.  Unrolling the inner loop is clearly a performance optimization.

    As to why [x][19] is left uninitialized... um... ah!  He clearly realized that it was unnecessary to initialize element [x][19], because when he creates the array the memory will be automatically initialized for him!  Pure genius.

  • icelava (cs)

    I say he was aiming for the readability of the code - everybody knows adding a nested loop with a y counter variable will only serve to confound the logic.[;)]

  • DanielMoth (cs)

    Obviously the author evaluated the performance of nested loops and decided to inline/unroll one of them… smart!

  • (unregistered)

    <FONT style="BACKGROUND-COLOR: #efefef">Wow. Array.Clear() was just too hard eh?</FONT>

  • (unregistered)

    I hope he understands the concept of Copy-Paste at least.

  • Jacob K (cs) in reply to
    NeoThermic:
    Next lesson: nested loops! :)

    NeoThermic


    That's funny. Made me laugh anways [Y]
  • sas (cs)

    <font face="Georgia">There's also the use of mysterious magic numbers (100, 20).

    [st][st][st] (weather here) </font><font face="Georgia">[st][st][st]</font>
    <font face="Georgia">

    </font>

  • Goudinov (cs) in reply to Mike R
    Mike R:
    I like his use of the Devil and Music characters Stick out tongue
    LOL, those will only compile in VS.NET if you have MSN Messenger 6 or higher installed
  • Chris Butler (cs) in reply to Goudinov

    <FONT style="BACKGROUND-COLOR: #efefef">Not much to say about this one. Think it speaks for itself  </FONT>

    <FONT style="BACKGROUND-COLOR: #efefef"></FONT> 

    [um][um][um]<FONT style="BACKGROUND-COLOR: #efefef">    [um][um][um]<FONT style="BACKGROUND-COLOR: #efefef"> </FONT>

    </FONT>
  • Blue (cs) in reply to
    :
    furniture can be heavy though. It's more like someone printing out a 20 page report one page at a time... :)


    I'm not familiar with any printers that can print more than one page at a time.  At least not ones used in most businesses.


  • Guayo (cs) in reply to Blue

    Blue:

    I'm not familiar with any printers that can print more than one page at a time.  At least not ones used in most businesses.

    You just create a new thread for every individual page to print.

    ...

    Newbies... [8-|]

  • (unregistered)

    As well as the lack of a nested loop, I have my doubts about whether a 2D array is the best data structure to use in this situation. What if there are > 100 history items? And do you specify < 100 items by filling as many elements as necessary and keeping the rest null? Is the second dimension of the array being used to store values for the different fields of each history item?

    Of course, this is all just speculation since I don't have the spec and can't see how the array is filled or used. It just smells a bit off...

  • (unregistered)

    <font face="Verdana" size="2">Given that the programmer did not use</font> <font color="#808080" face="Courier New" size="2">reservationHistory.length</font> <font face="Verdana" size="2">for the upper bound of the</font> <font color="#808080" face="Courier New" size="2">for</font> <font face="Verdana" size="2">loop, I reckon the programmer wanted to write a nested loop but just couldn't figure out or was scared by how you get the length of an array in the second dimension (<font color="#808080" face="Courier New">reservationHistory[x].length</font>), so instead he unrolled the loop and avoided the problem.
    </font>
    <font face="Verdana" size="2">Does anyone know if the compiler is smart enough to realise that this code (poorly) attempts an initialization of memory that <font color="#808080" face="Courier New">new</font> did already, and optimizes it into oblivion???
    </font>

  • Free (cs) in reply to
    :

    <FONT style="BACKGROUND-COLOR: #efefef">Wow. Array.Clear() was just too hard eh?</FONT>

    Uh ya, thats the ticket.

    You gave him loops, now he loops through everything? Sounds like the story of the guy with the hammer and the things that look like nails.

     

     

  • (unregistered)

    Perhaps the guy who wrote this wasn't familiar with Java (which i presume this is, perhaps it's something else but very similar), and thought that you have to nullify new arrays like in C, because in school we're taught: 'some compilers generate processor code (they mean assembler/y) that doesn't literally make the array empty, but instead aims for speed and leaves the reserved memory full with the junk it had before'. Of course in C you would write String[][]* arr[100][20]={''}; or something of that caliber to make it all empty.

    That doesn't explain the unrolled loop and the wrong index (18 instead of 19) though.

  • (unregistered)

    This guys looks like a C programmer... for two reasons:

    1. He's initializing his array (don't need to do that in Java).
    2. He's using a 2 dimensional array as some sort of data struture... not much fun.

    Just in case this fellow gets to read this blog (and I think his peer should point it out to him), this code sould be written as:
    <font color="blue" face="Courier New">public String</font><font face="Courier New">[][] getReservationHistory(</font><font color="blue" face="Courier New">int</font><font face="Courier New"> reservationId) { 
    </font><font color="blue" face="Courier New">String</font><font face="Courier New">[][] reservationHistory = </font><font color="blue" face="Courier New">new String</font><font face="Courier New">[100][20];
    </font><font color="green" face="Courier New">// do stuff with the array...</font><font face="Courier New">
    </font><font color="green" face="Courier New"> <font color="#0000ff">return</font> </font><font face="Courier New">reservationHistory;
    }</font>

    There is no need to initialize the array, it was already done when you called "new".

    Also, think up a better way to represent the structure; arrays are fine but if you need a 2 dimensional array you likely need to refactor.
    Maybe it would work better as an object added to a list that you then return.
     
  • (unregistered) in reply to
    :

    Perhaps the guy who wrote this wasn't familiar with Java (which i presume this is, perhaps it's something else but very similar), and thought that you have to nullify new arrays like in C,



    I was thinking the same thing... Some of the worst java code I've ever seen was written by C programmers... not because they were bad programmers but because java was so close to C that they tried to write it as C.

    Wait... umm... scratch that, the worst code I've ever seen was from a Perl programmer... made the java look like Perl (shudder, cough...).

  • (unregistered) in reply to

    Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?

    WTF is the point?

  • (unregistered) in reply to Blue

    That's what the parallel printer ports are for :-P

  • (unregistered) in reply to

    To show how 733t and hardcore we all are.

  • (unregistered) in reply to

    :
    Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?

    WTF is the point?

     

    We're trying to find logic in the chaos that is the WTF. Why do people solve a Rubik's Cube? That the fuck is the point :)

  • Mike R (cs) in reply to Free

    Free:

    Uh ya, thats the ticket.

    You gave him loops, now he loops through everything? Sounds like the story of the guy with the hammer and the things that look like nails.


    I can just imagine:

    Bob: "My computer won't connect to the internet"
    Jim: "Really? I think I can fix it!"
    Bob: "Ok. Go ahead!"
    Jim: "Okay, Just let me run back to my house real quick to get my new hammer"
    Bob: "A Hammer?!! What for?"
    Jim: "Its the latest in repair technology. I fixed my car with it yesterday. Now it works like a champ!"


    Random Web Board Gripe Time:

    Is it just me or is the posting page just complete garbage. The above post was typed twice. I guess I should know better than to forget to select the post, copy and paste.

    Alex... Have you looked at the source code for this webboard? It may be a candidate for one of your posts. I know its features almost certainly are.  My favorite is:

      * At random, change quoting logic to totally obliterate any text the user types no matter what the user may have the audacity to try.

    Ohh, and the HTML editor's "Insertion Point is 2 characters behind the actual location of the caret" feature is a bit annoying.

  • JamesCurran (cs) in reply to

    :
    Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?

    WTF is the point?

    Some people can't program.

    Others can't tell jokes....

     

  • UncleMidriff (cs)

    When I was in school, I had a professor who drilled into her students' heads the need to initialize eveything, no matter what.  "Initialize, initialize, initialize!", she'd say, "Never trust a variable to contain anything you didn't put in it!"  As a result of that, I still catch myself initializing variables when I don't need to.  Perhaps this person had a similar professor and just hasn't broken the habit yet?

    I've no idea about the loop (or lack thereof) though.

  • (unregistered)

    "Of course in C you would write String[]* arr[100][20]={''}; or something of that caliber to make it all empty."

    If we do a dodgy, and limit max string size (and waste memory), we'd need: char arr[100][20][MAX_LENGTH]={0};

    If we malloc space for the strings of differing sizes, we need a matrix of char*'s. char* arr[100][20]={0};

    If however we need some proper 'String' functionality in C, a matrix of linked lists (which allow random access) would have to be constructed, along with a bunch of string manipulation functions to make working with the linked list less tedious. Not my idea of fun.

    This is why I love C++ sooo much.

    (it's 3am where I am, and I need to amuse myself somehow; leave me alone!)

    UncleMidriff: I got taught to always initialise aswell, although the reason was more todo with possibly buggy code if you go from a language which initialises, to one that doesn't.

  • (unregistered) in reply to Maurits

    The coder could not figure out why he was having a memory issue with an array of this size.  So he just started removing code until it worked.

  • (unregistered) in reply to

    The solution here is to remove the loop completely, they're all null anyway.

  • (unregistered) in reply to

    :
    "Of course in C you would write String[][]* arr[100][20]={''}; or something of that caliber to make it all empty."

    If we do a dodgy, and limit max string size (and waste memory), we'd need:
    char arr[100][20][MAX_LENGTH]={0};

    If we malloc space for the strings of differing sizes, we need a matrix of char*'s.
    char* arr[100][20]={0};

    If however we need some proper 'String' functionality in C, a matrix of linked lists (which allow random access) would have to be constructed, along with a bunch of string manipulation functions to make working with the linked list less tedious. Not my idea of fun.

    This is why I love C++ sooo much. 

     

    Yeah that's what I meant :p It's been too long since I wrote any C that had to do something with strings, so I wrote String[][]* instead of char* hehe. My bad. On a side note. I HATE it when people write char *arr[100][20]={0} instead of char* arr[100][20]={0}  . Notice the position of the *? Both syntaxes are valid I believe, but ARGH!

  • Blue (cs) in reply to
    :
    Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?

    WTF is the point?


    Other than saying "WTF", wtf else would one write about?!

    Also, criticizing is inherently better when you can provide a better solution.


  • (unregistered)

    Oh come on.  The guy was working on efficiency!  Loop unrolling! ;)

    Also why isn't the 20th string not initialized to null?

  • haveworld (cs) in reply to

    :
    Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?

    WTF is the point?

    Please let my quote work!

    Because people like me read this blog and want to know what better code looks like... :(

    Rule of thumb : always preview comments before posting on this board.

  • (unregistered) in reply to haveworld
    haveworld:

    [image]  wrote:
    Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?

    WTF is the point?

    Please let my quote work!

    Because people like me read this blog and want to know what better code looks like... :(

    Rule of thumb : always preview comments before posting on this board.

  • (unregistered)

    Java does not have 2D (or any kind of multi-dimensional) arrays.

    This commonly held fallacy causes great confusion, though not of the type demonstrated in the original post.

  • (unregistered) in reply to

    "On a side note. I HATE it when people write char *arr[100][20]={0} instead of char* arr[100][20]={0}  . Notice the position of the *? Both syntaxes are valid I believe, but ARGH!"

    I've not had to 'point' to anything for a long time, but I think the practice you HATE has something to do with the following:

    What does: <FONT face="Courier New"><FONT color=#0000ff>char* fred1, fred2, fred3;</FONT> </FONT>do?

    What does: <FONT face="Courier New" color=#0000ff>char *fred1, fred2, fred3;</FONT> do?

    (IIRC, many coding standards prohibit both the above, for roughly the same reason.)

    "Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?"

    Some people just come here to read the silly code and either 'get it', or 'not get it', while others are either open-minded or stupid enough to want to enter into a discussion. Maybe even learn a thing or two from others mistakes and experience. Idiots.

    Personally, once I'd seen about 3 or 4 WTFs I found that although they are sometimes amusing in their own right, the discussion was often far more interesting and entertaining (and often more 'WTF?') then the actual topic. That's why I come back every day.

    And for me, it's kind-of a running joke how many of the 'serious solutions' don't quite hold up either...

    <FONT size=1>With a site name like this, even seriously off-topic posts are sometimes quite valid...</FONT>

     

     

  • (unregistered)

    If this is java (does C have a String object?) then it is flat wrong and will throw NullPointerExceptions. It needs a

    reservationHistory[x] = new String[20];

Leave a comment on “Finally, some better code!”

Log In or post as a guest

Replying to comment #:

« Return to Article