- 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
Next lesson: nested loops! :)
NeoThermic
Admin
Well, also, they're setting the values to the default values in the array already.
Admin
<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>
Admin
It would seem as if. But... the array initializes to null anyway... does he even need the loop? [^o)]
Admin
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. :)
Admin
I like his use of the [6] and [8] characters [:P]
Admin
furniture can be heavy though. It's more like someone printing out a 20 page report one page at a time... :)
Admin
I think he understands loops (since he is using one) -- but he doesn't understand that you can NEST loops ...
Admin
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.
Admin
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.[;)]
Admin
Obviously the author evaluated the performance of nested loops and decided to inline/unroll one of them… smart!
Admin
<FONT style="BACKGROUND-COLOR: #efefef">Wow. Array.Clear() was just too hard eh?</FONT>
Admin
I hope he understands the concept of Copy-Paste at least.
Admin
That's funny. Made me laugh anways [Y]
Admin
<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>
Admin
Admin
<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>Admin
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.
Admin
You just create a new thread for every individual page to print.
...
Newbies... [8-|]
Admin
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...
Admin
<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>
Admin
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.
Admin
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.
Admin
This guys looks like a C programmer... for two reasons:
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:
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.
Admin
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...).
Admin
Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?
WTF is the point?
Admin
That's what the parallel printer ports are for :-P
Admin
To show how 733t and hardcore we all are.
Admin
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 :)
Admin
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.
Admin
Some people can't program.
Others can't tell jokes....
Admin
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.
Admin
"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.
Admin
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.
Admin
The solution here is to remove the loop completely, they're all null anyway.
Admin
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!
Admin
Other than saying "WTF", wtf else would one write about?!
Also, criticizing is inherently better when you can provide a better solution.
Admin
Oh come on. The guy was working on efficiency! Loop unrolling! ;)
Also why isn't the 20th string not initialized to null?
Admin
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.
Admin
Admin
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.
Admin
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.)
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>
Admin
If this is java (does C have a String object?) then it is flat wrong and will throw NullPointerExceptions. It needs a