- 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
People on this site seem to bring up "commenting your code" as a solution, but in general if the code didn't make sense after reading it, reading a code comment generally doesn't make it all "clear."
Anyway, comments in code usually lie. Learn how to use a debugger.
Admin
Totally fixed! Thanks.
Admin
This code isn't really that hard to follow, seems to be setting up a general layout.... the wtf might be the submitter.
Admin
Indeed...
7 is not such a big number. And this would be much clearer...
Rectangle[] rect = new Rectangle[7]; //header rect [0] = new Rectangle(216, 12, 110, 60); //1st row rect [1] = new Rectangle( 4, 75, 105, 60); rect [2] = new Rectangle(110, 75, 105, 60); rect [3] = new Rectangle(216, 75, 105, 60); //2nd row rect [4] = new Rectangle( 4, 135, 105, 60); rect [5] = new Rectangle(110, 135, 105, 60); rect [6] = new Rectangle(216, 135, 105, 60);
for (int i = 0; i<7; i++){ //whatever common code }
Admin
Alternatively, it's a poor attempt at job security. But perhaps more effort was spent on that in the rest of the code (shudder).
Anyway, this one is really not so terrible, and it's easy to imagine a reasonable context around the given piece of code.
Oh, and naturally having magic numbers like that is perfectly reasonably if it's an embedded device with fixed size screen, but I bet somebody has already pointed that out... ;-)
Admin
Did you comment it out and the whole thing break? Sometimes people use an "if" statement for in-order, shorthand evaluation of a bunch of statements. The last return statement is just because the function was declared to return something, and the compiler would complain otherwise. Hopefully it comes with a comment to such effect, though.
Admin
Admin
Dear Emil,
In case you can’t tell, this is a grown-up place. The fact that you don't know how to program simple 2D graphics clearly shows that you’re too young and too stupid to be using computers.
Go away and grow up.
Sincerely, Bert Glanstron
Admin
Ah yes, that old argument...
Let's take the current example. If I'm, say, looking for a section where it sets a color for displaying some doodad or another (further down in the code), I'd be stuck looking at every line to figure out WTF it was doing. If there were some comments and meaningful variable names (and #define'd quantities), I could visually scan (or search/grep) for words of importance. Huge difference in readability and the resulting productivity.
I shouldn't have to comb through every single line to find a section of interest. Proper naming of variables and simple comments go a long way to aid in code overview; it's the difference between actually having to read all of the code in order to find one specific function and being able to judge flow and structure at a glance (more or less). Code should be written in a way that actively promotes its understanding and readability. Simply lacking obscurity isn't enough.
Until the software needs to be updated for a new version of the device, which is mostly the same but has a display with (even slightly) different dimensions. Instead of simply updating your #define'd values, you're stuck trying to replace every occurrence of an integral constant - and you'll likely have to check each one to see if it was really being used for a display size instead of some other literal value.
Admin
+1
Isn't that ironic?
Admin
This is GUI programming. By default GUI programming is ugly.
GUI programming with magic numbers is a sin, but not very great because GUI programming is ugly no matter what way you do it.
Admin
It's obvious he's just making a group of controls (buttons?) 3 columns wide, with 4 and 75 being the margins and 105 and 60 being the width and height.
Admin
Admin
Even if there was a constant settable at runtime through a modified classloader loading its configuration from an XML file retrieved from a database with three-tier authentication by using a stored procedure generated by an external PERL script, along with full internationalization of the constant's name in 160 different languages, still someone here would not be pleased and consider it a WTF that's it's not as configurable as he's like.
Admin
What was 'i' again?
Can we call it loopIterator instead?
<disclaimer>No, this is not a serious post </disclaimer>
Admin
GUI programiing is ugley, because GUI programmers are generally shyte, hence the need for all these drag'n'drop screen designers, which also generate ugly code, thus appeaqring that said shyte programmers haev put the same effort in that they used to, even though they haven't...
interestingly (no chip on my shoulder) any issues are never in the GUI, despite the ugly code (which I suppose makes the case that readability isn't important)
Admin
Everyone should know by now that THREE is the magic number.
Admin
it's bad because there is no separation of concerns. doesn't matter whether you can make some sense of it or not.
Admin
This is what code looks like when you let francophones write it. I remember one junior spending all day with visual studio, trying to create a method with accents in the name.
Admin
TRWTF is that list is spelled lst. I never get why people think that omitting one character is helpful in any way.
Admin
It's Iron Maiden.
Admin
No, really? Never would have guessed.
Admin
I'll ditto those who say this is not that big a deal.
I hate magic numbers as much as anybody. But what is the programmer supposed to do in a case like this. Sure, you could write:
But is it really that cryptic to write "width=105"? Above this he spaces out boxes by an amount equal to width plus 1. Again, okay, you could write "x=LEFT_MARGIN+(i%3)*(CELL_WIDTH+1)". But would that really gain that much?
We might wonder why he chose 105 as the cell width and not some other number. But odds are this was a number that just worked out empirically.
Admin
That's better, but incomplete. You forgot to mention that rect.X and rect.Y should be onScreenRectangle.pixelOffsetFromLeftEdge and onScreenRectangle.pixelOffsetFromTopEdge. Friggen duh! How do you get any work done when you spend so little time on these things?!
Admin
TRWTF is not having a single exit point?
I've seen something similar in production code at my previous job:
That was written by a master in CS to execute either a function or the same function based on the booelan value of an undeclared varialbe. Since I'm a humble bachelor, his way of doing things was always better according to management.
To save you all the trouble, possible TRWTFs are: 1) using undeclared variables, 2) PHP allowing this, 3) PHP, 4) extraneous statements/unnecessary branching, 5) placeholder for a real check slipping into production (without any comment), 6) management. Definitely 6!
Admin
Testing if the array-element is null and then trying to call a function on thtat object doesnt make much sense...
if array(i).nil? array(i).do_something end
Admin
The real WTF is the incompetence of the readers here, who can't understand this glaringly obvious code.
Should be obvious that he or she is creating seven rectangles, the first one (index 0) at the coordinates of the "new" statement, the remaining six in two rows of three columns. It's just coordinates on the screen where he or she wants the rectangles to appear. No need to document that.
Admin
Really. NOOB. Moonchild ...
Admin
isn't necessarily a WTF.
It could be that the requirements have changed and do_this() now needs to be executed all the time, and the developer left the $condition in, just in case it needs to be changed back at a later stage. It's possible that this could be foresight on the original/maintenance programmer's behalf.
Or, maybe the do_this_2() function was broken/breaking other things and not a mission critical function (GUI update or font formatting, or something) and the dev used a temporary replacement to get the project back online.
Admin
Sounds like a WTF to me. He couldn't be bothered to drag his mouse and hit the delete key?
Silly code like that should stay on your dev box if you are "just debugging", lest it be privy to the other developers who have a solemn duty to ridicule your poor choice of programming paradigms.
capthca: acsi - Standard for printing on a dyslexic machine