| « Prev | Page 1 | Next » |
|
Next lesson: nested loops! :)
NeoThermic |
|
Well, also, they're setting the values to the default values in the array already.
|
|
String[][] reservationHistory = new String[100][20]; reservationHistory[x][0] = null;
Shouldn't he be doing 0-19 rather than 0-18? |
|
It would seem as if. But... the array *initializes* to null anyway... does he even need the loop? [^o)]
|
|
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. :) |
|
I like his use of the [6] and [8] characters [:P]
|
|
furniture can be heavy though. It's more like someone printing out a 20 page report one page at a time... :)
|
|
I think he understands loops (since he is using one) -- but he doesn't understand that you can NEST loops ...
|
|
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. |
|
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.[;)] |
|
Obviously the author evaluated the performance of nested
|
|
Wow. Array.Clear() was just too hard eh? |
|
I hope he understands the concept of Copy-Paste at least.
|
That's funny. Made me laugh anways [Y] |
|
There's also the use of mysterious magic numbers (100, 20).
[st][st][st] (weather here) [st][st][st] |
LOL, those will only compile in VS.NET if you have MSN Messenger 6 or higher installed |
Re: Finally, some better code!
2005-01-05 15:16
•
by
Chris Butler
|
|
Not much to say about this one. Think it speaks for itself
[um][um][um] [um][um][um] |
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-|] |
|
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... |
|
Given that the programmer did not use reservationHistory.length for the upper bound of the for 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 (reservationHistory[x].length), so instead he unrolled the loop and avoided the problem. Does anyone know if the compiler is smart enough to realise that this code (poorly) attempts an initialization of memory that new did already, and optimizes it into oblivion??? |
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.
|
|
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. |
|
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: public String[][] getReservationHistory(int reservationId) { 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. |
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...). |
|
Can anyone explain to me why people post their own, serious, solutions to the WTF posted here?
WTF is the point? |
|
That's what the parallel printer ports are for :-P
|
|
To show how 733t and hardcore we all are. |
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 :) |
I can just imagine: Bob: "My computer won't connect to the internet" 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. |
Re: Finally, some better code!
2005-01-06 10:23
•
by
JamesCurran
|
Some people can't program. Others can't tell jokes....
|
|
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. |
|
"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. |
|
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.
|
|
The solution here is to remove the loop completely, they're all null anyway.
|
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! |
Other than saying "WTF", wtf else would one write about?! Also, criticizing is inherently better when you can provide a better solution. |
|
Oh come on. The guy was working on efficiency! Loop unrolling! ;)
Also why isn't the 20th string not initialized to null? |
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. |
|
|
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. |
With a site name like this, even seriously off-topic posts are sometimes quite valid...
|
|
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]; |
| « Prev | Page 1 | Next » |