- 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
From D. ...
Admin
I think the true wtf is that the parameter names make you think that Which will be added to iteself HowMany times. No wait, the true wtf is that the function exists.... no.... wait.... aha! <sarcasm Jeff S!>the TRUE wtf is that it was done in VB!</sarcasm>... wait, no... that's not it either... bah!
Admin
add( wicked, awesome )
Admin
WTF = (Which + HowMany ^ WhyGodWhy) * StopTheInsanity
Admin
It's weird, but my first thought was "why is that a wtf?" but then I remembered that VB sucks and doesn't have first class functions.
VB: A language designed with all the drawbacks of C but none of the power.
(and yes, it'd still be bad in languages with first order functions for not using builtins, but still wouldn't merit a 'wtf')
Admin
First the parameter is pass by value so the result is not return in the calling parameter.
Second nethier add is assigned, old VB, or value returned.
Last the function return type is not declared.
The question is how is result returned to the caller.
Admin
* VB.Net has +=, but does VB6? Our coder must be the dinosaur from the Microsoft Office adverts (a WTF in itself...)
* I bet Which was a selection variable, multiplied by HowMany in a bitmask-like fashion. Then this was refactored to be "readable." Notice how I didn't say bitmask, I said bitmask-like. The horrors are better imagined that way. :)
* I'll see your $20 and raise you $50. :)
* FileNotFound, of course.
Admin
We did something similar at my old company, where there were strict privileges on who was allowed to increment variables:
Private Function Add(
ByRef Which As Integer,
ByVal HowMany As Integer,
ByRef When As Date,
ByVal Why As String,
ByVal Who As String )
When = Now
#If WhoCanAddHowManyToWhichWhenWhy(Which, HowMany, Who, When, Why) Then
Which = Which + HowMany
#End If
End Function
The += operator just wasn't cutting it for us.
Admin
Ah, yes. The good old days, when you had to write:
<font face="Courier New">which = which + howMuch
</font>
Admin
How do people like this find their way here.
You (Alex) should check your web stats and see where the link is coming from. Then make the /. or whatever remove the link.
Admin
Ok, let's seeeeeeeee...
Which is declared at beginning of function (so now it extists)
HowMany is declared at beginning of function (same as Which)
With no values assigned to either, they are both added and stored in Which (therefore it is still null)
Then the function ends and the variables dissolve into nothingness...
Therefore, as the function itself does nothing useful, it is there only for the purpose of making the person proofing it go WTF!?!?!
Admin
Am I missing something here?
Which is passed in by reference. Add HowMany to it inside the function, it stays added to the original variable the reference pointed to.
What = 3
Add(What, 5)
now, What is what?
Admin
Which is passed by reference, so the result is written back to the first argument passed.
Admin
What is 8.
This is coded in VB.NET. in this language, the ByRef keyword used with a function parameter means that it is By Reference. Thus, any changes made inside the function to a parameter passed ByRef are stored in the variable, and propogate back to the variable (in this case 'what').
Admin
Bah. What they should have done is overloaded the += operator to do this.
Admin
Valid point.
I disagree. These parameter names clearly identify the purpose of the parameters. I will concede that if the code it was part of incorporates a parameter-naming schema that it should have followed it, however this would like have merely led to iWhich and iHowMany
Since when is doing something right a WTF? And while it may be true that a bug was caught and fixed, it wouldn't have taken long to realize the mistake...
True, it should probably have been a Sub. As it is, would it even run?
Admin
There are a few things wrong with your analysis of this function:
"Which" is a byRef value. this is the same as a pointer in a C style language. This means that when "Which" has the sum of itself and "HowMany" assigned to it, the original variable is assigned to.
The values for Which and HowMany come from the calling code
eg.
Add(i, 5)
there is no need for a return, nor is one required, as the initial variable (i in my example) has been altered by the add function.
Also, Functions in VB.Net do not require a return value.
The only thing i can see wrong with this function is that the author was unaware that the += operator exists. Sure, he should probably have written it so that it doesn't use a byRef design, but that doesn't make it wrong.
Admin
8
Admin
Another effective [debugging] technique is to explain your code to someone else. This will often cause you to explain the bug to yourself. Sometimes it takes no more than a few sentences, followed by an embarrassed "Never mind, I see what's wrong. Sorry to bother you." This works remarkably well; you can even use non-programmers as listeners. One university computer center kept a teddy bear near the help desk. Students with mysterious bugs were required to explain them to the bear before they could speak to a human counselor. - B. Kernighan & D. Pike (in "The Practice of Programming" pp. 123)
Admin
Was this an intential attempt to start a flame war or just ignorance?
Admin
Since the += operator does not exist in VB6, and it is possible to import VB6 code into .NET, this could simply be a remnant from a convenience function ported from a VB6 app.
Sure it could have been rewritten to use += (in every instance of its usage, and moreover cannot be changed using a simple Find..Replace) it would be a wasted effort to change something that works, even though it is ugly.
Not a WTF.
Admin
<font face="Courier New">Wow, I think this is the first thread I've seen where the responses are funnier and more WTF-worthy than the actual parent post. Thanks Obi-Wan and Rammadman!</font>
Admin
This wtf resume in one and only one word.
Why "Function" ?
(oups it make two words)
Bah, finally, its just a function with hidden-bad-side-effect ... or bad-hidden-side-effect...
Admin
I think you could use a VB primer. Or perhaps a general programming syntax primer, if "a list of variable declarations in parentheses following the function name" does not ring some kind of bell.
Admin
Just thinking about translating the WTF to other languages, the Perl version looks just as exciting:
sub Add { @[0] = @[0] + @_[1]; }
Therefore, Perl is clearly a superior langauge.
Admin
Programmer 1: What's the parameter for the Add function?
Programmer 2: No, what is the parameter for the Minus function.
Programmer 1: I'm not asking you which parameter goes with the Minus function.
Programmer 2: Which parameter is for the Add function.
Programmer 1: I don't know!
Programmer 2: No, no, that's the parameter for the Multiply function, we haven't gotten that far, yet.....
Admin
"Dah...which way did he go? Which way did he go?"
Get it? That was a Looney Tunes reference.
Ah, forget it. You people have no sense of humor.
Admin
You know, even in languages with or without +=, you are allowed to write:
a = a + b
Admin
Is it really that much of an improvement to use
<font face="Courier New"> Add(i,5);</font>
instead of
<font face="Courier New"> i = i + 5;</font>
In the absence of a += operator, I can see how using some sort of twisted logic it might seem useful to prevent some kind of typo, (e.g. i = j + 5, when i = i + 5 was meant) where the same variable doesn't appear on both sides, but that just seems ridiculous to worry about. And not to prematurely optimize, but if you are using Add inside a well-used and tight loop, you're going to want to inline (heh) this function if you need better performance. Of course, you could also argue that most JIT compilers would inline this function anyway. But then again, perhaps, well, maybe, oh well, WTF.
Admin
8
pass-by-ref means the variable itself is accessible - 'which' can change.
s/b a proc (no, s/b +=, (no - should die))
Admin
Yeah, and all you have to do is inline this 'function' to see that!
Admin
The real wtf is the lack of understanding passing by reference and value by the comment posters.
Admin
I'm guessing an ignorant attempt to start a flame war ... (i.e., both)
Admin
You folks are all missing the real purpose of the 'function'.
There was also a whole bunch of code in it to use which as an index into a DB table, query the table, then use howMany to create a cursor-laden subquery, and then simply add the two together to throw off any suspicious developers attempting to see what it does.
Of course, there is some hidden global that returns the Bool.FILE_NOT_FOUND as the default result.
Admin
True but this has a quite legitimate use anyway.
Yes, it's easy enough to write a = a + b. The reason for routines like this is when a isn't a simple type.
Totals[DayOfMonth(Order.Dates.Sold)] = Totals[DayOfMonth(Order.Dates.Sold)] + Order.Value
Should you make the reader look at both sides to see that they really are identical, or should you write
Add(Totals[DayOfMonth(Order.Dates.Sold)], Order.Value)
Of course there's also the approach
Total = Totals[DayOfMonth(Order.Dates.Sold)]
Total = Total + Order.Value
Totals[DayOfMonth(Order.Dates.Sold)] = Total
But to me this is even worse.
To me the version with a function call is the clearest way to write something like this.
Of course it's moot in languages that support += but who says this code started out in such a language?
On the other hand it seems the website doesn't agree with me. Captcha: error
Admin
Theory #1:
Real programmers abstract everything!
Theory #2:
Real programmers rewrite every language to their own liking! isTrue() can't be far behind.
Admin
That can get quite lengthy. Imagine:
Connect2Computer("Bean").Hardware.Disks.Partition[0].GetPersonalData().Dir("Paula").File("Brillant").GetContents () = Connect2Computer("Bean").Hardware.Disks.Partition[0].GetPersonalData().Dir("Paula").File("Brillant").GetContents () + 5
Admin
No offense, but what part of my post makes you assume that I wouldn't know basic variable assignment?
I was offering a possible reason that this was present in VB.NET code that wouldn't be completely far fetched. All I was saying that the use of the function to add wasn't all that much of a WTF (much worse could be done*). The parameter names, maybe, but I just don't see how this one qualifies to be on the front page.
At least they didn't write:
Function Add(ByVal Which, ByVal HowMany)
Add = Which + HowMany
End Function
(I will leave to the reader as to why this would be worse)
Admin
And we're supposed to be able to tell that from the surrounding zero lines of context, or just by ESP? WTF?
It's pretty unreasonable of someone who saw the whole thing to come over all smug just because the rest of us didn't realise that....
Admin
HowMany is declared at beginning of function (same as Which)
The real WTF is (presumably) professional programmers who do not understand pass-by-reference (or apparently parameter passing at all).
Admin
I nominate this as the best post!
Admin
... which also works on non-integer data ...
Admin
whoa... that kind of reminds me of the first university math-class;
Teacher: "First we must have some since of numbers. One is... Errr... Well, one is one! And two..? That's one more!"
((very) loosely translated from swedish...)
Admin
doh...
ment "sense" not "since"...
Admin
Awesome quote :D
Admin
Yeah, I was wondering why it didn't make any since...
Admin
Wrong! In Perl, parameters are passed as in an array that happens to be an alias to the paremeter variables themselves. There's a subtle difference between an alias and a reference, mainly that aliases can represent all instances of a symbol in a symbol table (i.e. aliasing *foo will create aliases for $foo, %foo, and @foo), and a reference represents a pointer to a specific location.
-dZ.
Admin
Absolutely. Neither VB.NET nor C# requires that you use the return value of a function or even assign it into an LVALUE.
Admin
Please consider what the ByRef means vs ByVal
This function does something - it's usefulness can ofcourse be discussed.
Admin
I'll give you that; point taken. I think I skimmed too quickly over your original post and missed the word *convenience*, which was a key to your point.