Comment On Which is on HowMany?

D. Lambert was threading his way through a horrible mess of an inherited system and stumbled upon a rather remarkable function. Unlike so many other of the terrible routines within the system -- some spanning several printed pages with nested loops six-deep and variables whose scope has been violated back and forth and forth and back -- this function is subtle. Elegant. It's a tiny monument to twisted intent that should allow one to appreciate the totality of its ruin ... [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: Which is on HowMany?

2006-07-06 13:11 • by Alex Papadimoulis

From D. ...



Ok, so what's wrong with this code? How many of these did you get:
* It was found in an asp.net web page as vb.net code. Vb.net, of course, features the "+=" operator, which replaces this whole function rather neatly.
* The whole "which" and "howmany" naming convention reeks of "apples" and "oranges", not to mention its disdain for mundane pursuits like variable naming conventions.
* The "byval" and "byref" are actually correct here, but I've got $20 that says it was wrong the first time through. -g-
* It's a function, right? (what's it return?)

Re: Which is on HowMany?

2006-07-06 13:16 • by GoatCheez
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!

Re: Which is on HowMany?

2006-07-06 13:21 • by Gnictigezoink
add( wicked, awesome )

Re: Which is on HowMany?

2006-07-06 13:21 • by steveth
WTF = (Which + HowMany ^ WhyGodWhy) * StopTheInsanity

Re: Which is on HowMany?

2006-07-06 13:26 • by Anon
80492 in reply to 80485
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')

Re: Which is on HowMany?

2006-07-06 13:29 • by rammadman

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.

Re: Which is on HowMany?

2006-07-06 13:30 • by Benanov
80494 in reply to 80485
Alex Papadimoulis:

From D. ...



Ok, so what's wrong with this code? How many of these did you get:
*
It was found in an asp.net web page as vb.net code. Vb.net, of course,
features the "+=" operator, which replaces this whole function rather
neatly.
* The whole "which" and "howmany" naming convention reeks of
"apples" and "oranges", not to mention its disdain for mundane pursuits
like variable naming conventions.
* The "byval" and "byref" are actually correct here, but I've got $20 that says it was wrong the first time through. -g-
* It's a function, right? (what's it return?)





    * 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.

Re: Which is on HowMany?

2006-07-06 13:33 • by Last Bubble

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.

Re: Which is on HowMany?

2006-07-06 13:52 • by eddiedatabaseboston
80500 in reply to 80494
Benanov:


    * VB.Net has +=, but does VB6?
Our coder must be the dinosaur from the Microsoft Office adverts (a WTF in itself...)


Ah, yes.  The good old days, when you had to write:

which = which + howMuch

Re: Which is on HowMany?

2006-07-06 13:55 • by AndrewVos
80501 in reply to 80492

Anonymous:
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')


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.

Re: Which is on HowMany?

2006-07-06 13:56 • by Obi Wan
Private Function Add(ByRef Which As Integer, ByVal HowMany As Integer)

Which = Which + HowMany
End Function
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!?!?!

Re: Which is on HowMany?

2006-07-06 14:01 • by Paul
80503 in reply to 80502

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?


 

Re: Which is on HowMany?

2006-07-06 14:02 • by benryves
80504 in reply to 80502
Which is passed by reference, so the result is written back to the first argument passed.

Re: Which is on HowMany?

2006-07-06 14:08 • by is it that hard?
80505 in reply to 80503
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').

Re: Which is on HowMany?

2006-07-06 14:09 • by Mike
Bah.  What they should have done is overloaded the += operator to do this.

Re: Which is on HowMany?

2006-07-06 14:10 • by Geoff
80507 in reply to 80485
D:
It was found in an asp.net web page as vb.net code. Vb.net, of course, features the "+=" operator, which replaces this whole function rather neatly.

Valid point.


D:
The whole "which" and "howmany" naming convention reeks of "apples" and "oranges", not to mention its disdain for mundane pursuits like variable naming conventions.

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


D:
The "byval" and "byref" are actually correct here, but I've got $20 that says it was wrong the first time through. -g-

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...


D:
It's a function, right? (what's it return?)

True, it should probably have been a Sub. As it is, would it even run?

Re: Which is on HowMany?

2006-07-06 14:10 • by Antony
80508 in reply to 80502

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.

Re: Which is on HowMany?

2006-07-06 14:11 • by tster
80509 in reply to 80503
Anonymous:

Am I missing so8mething 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?


 


8

Re: Which is on HowMany?

2006-07-06 14:12 • by Ben Adams

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)

Re: Which is on HowMany?

2006-07-06 14:12 • by foxyshadis
80512 in reply to 80492
Anonymous:
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')

Was this an intential attempt to start a flame war or just ignorance?

Re: Which is on HowMany?

2006-07-06 14:14 • by a possible explanation:
80513 in reply to 80508
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.

Re: Which is on HowMany?

2006-07-06 14:14 • by Guybrush Threepwood
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!


Re: Which is on HowMany?

2006-07-06 14:18 • by Thorgull
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...

Re: Which is on HowMany?

2006-07-06 14:18 • by foxyshadis
80518 in reply to 80502
Anonymous:
Private Function Add(ByRef Which As Integer, ByVal HowMany As Integer)

Which = Which + HowMany
End Function
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!?!?!

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.

Re: Which is on HowMany?

2006-07-06 14:20 • by Carnildo
Just thinking about translating the WTF to other languages, the Perl version looks just as exciting:

sub Add
{
@_[0] = @_[0] + @_[1];
}

1) Yes, Perl also has a += function.
2) Variable naming conventions? Why?
3) In Perl, function parameters are *always* passed by reference.
4) Perl, unlike VB, will automatically return the value of the last statement in a function. So while "Result = Add(MyVal, 3)" isn't sensible, "$result = Add($myVal, 3)" is.

Therefore, Perl is clearly a superior langauge.

Re: Which is on HowMany?

2006-07-06 14:22 • by Disgruntled DBA
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.....

Re: Which is on HowMany?

2006-07-06 14:23 • by Buh Foon
80522 in reply to 80520
"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.

Re: Which is on HowMany?

2006-07-06 14:24 • by Jeff S
80523 in reply to 80513
Anonymous:
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.


You know, even in languages with or without +=, you are allowed to write:

a = a + b

Re: Which is on HowMany?

2006-07-06 14:24 • by Shizzle
80524 in reply to 80508
Anonymous:

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.


Is it really that much of an improvement to use

    Add(i,5);

instead of

    i = i + 5;

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.

Re: Which is on HowMany?

2006-07-06 14:25 • by narcisist
80525 in reply to 80503
Anonymous:

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?



8


pass-by-ref means the variable itself is accessible - 'which' can change.


s/b a proc (no, s/b +=, (no - should die))

Re: Which is on HowMany?

2006-07-06 14:26 • by Shizzle
80526 in reply to 80523
Jeff S:
Anonymous:
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.


You know, even in languages with or without +=, you are allowed to write:

a = a + b


Yeah, and all you have to do is inline this 'function' to see that!

Re: Which is on HowMany?

2006-07-06 14:27 • by Jimmy Jimmy
80527 in reply to 80504
The real wtf is the lack of understanding passing by reference and value by the comment posters.

Re: Which is on HowMany?

2006-07-06 14:30 • by Jeff S
80528 in reply to 80512
Anonymous:
Anonymous:
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')

Was this an intential attempt to start a flame war or just ignorance?

I'm guessing an ignorant attempt to start a flame war ... (i.e., both)

Re: Which is on HowMany?

2006-07-06 14:31 • by narcisist
80529 in reply to 80525

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.

Re: Which is on HowMany?

2006-07-06 14:32 • by Scalar Context!
80530 in reply to 80520

Carnildo:
Just thinking about translating the WTF to other languages, the Perl version looks just as exciting: sub Add { @_[0] = @_[0] + @_[1]; } 1) Yes, Perl also has a += function. 2) Variable naming conventions? Why? 3) In Perl, function parameters are *always* passed by reference. 4) Perl, unlike VB, will automatically return the value of the last statement in a function. So while "Result = Add(MyVal, 3)" isn't sensible, "$result = Add($myVal, 3)" is. Therefore, Perl is clearly a superior langauge.


 


I think you mean:



sub Add {
$_[0] = $_[0] + $_[1];
}

Re: Which is on HowMany?

2006-07-06 14:33 • by Loren Pechtel
80531 in reply to 80523
Jeff S:
Anonymous:
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.


You know, even in languages with or without +=, you are allowed to write:

a = a + b



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

Re: Which is on HowMany?

2006-07-06 14:34 • by VGR
Theory #1:

Real programmers abstract everything!



Theory #2:

Real programmers rewrite every language to their own liking!  isTrue() can't be far behind.



Re: Which is on HowMany?

2006-07-06 14:37 • by Anonymous
80535 in reply to 80523
Jeff S:
You know, even in languages with or without +=, you are allowed to write:

a = a + b



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

Re: Which is on HowMany?

2006-07-06 14:40 • by a possible explanation:
80537 in reply to 80523
Jeff S:
Anonymous:
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.


You know, even in languages with or without +=, you are allowed to write:

a = a + b



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)

Re: Which is on HowMany?

2006-07-06 14:42 • by DaveK
80539 in reply to 80485
Alex Papadimoulis:

From D. ...



Ok, so what's wrong with this code? How many of these did you get:
* It was found in an asp.net web page as vb.net code.





  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....





Re: Which is on HowMany?

2006-07-06 14:43 • by Anon
80540 in reply to 80502
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).

Re: Which is on HowMany?

2006-07-06 14:44 • by Got enough wtfs of my own
80541 in reply to 80521

Disgruntled DBA:
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.....


I nominate this as the best post!

Re: Which is on HowMany?

2006-07-06 14:46 • by rbriem
80542 in reply to 80523

Jeff S:
Anonymous:
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.


You know, even in languages with or without +=, you are allowed to write:

a = a + b


... which also works on non-integer data ...

Re: Which is on HowMany?

2006-07-06 14:49 • by jovnas
80543 in reply to 80505
Anonymous:
What is 8.

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...)

Re: Which is on HowMany?

2006-07-06 14:51 • by jovnas
80544 in reply to 80543
doh...
ment "sense" not "since"...

Re: Which is on HowMany?

2006-07-06 14:57 • by Sunday Ironfoot
80546 in reply to 80510
Ben Adams:

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)



Awesome quote :D

Re: Which is on HowMany?

2006-07-06 14:58 • by Onanymous
80547 in reply to 80544
jovnas:
doh...
ment "sense" not "since"...


Yeah, I was wondering why it didn't make any since...

Re: Which is on HowMany?

2006-07-06 15:05 • by DZ-Jay
80549 in reply to 80520
Carnildo:
Just thinking about translating the WTF to other languages, the Perl version looks just as exciting:

sub Add
{
@_[0] = @_[0] + @_[1];
}

1) Yes, Perl also has a += function.
2) Variable naming conventions? Why?
3) In Perl, function parameters are *always* passed by reference.
4) Perl, unlike VB, will automatically return the value of the last statement in a function. So while "Result = Add(MyVal, 3)" isn't sensible, "$result = Add($myVal, 3)" is.

Therefore, Perl is clearly a superior langauge.


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.

Re: Which is on HowMany?

2006-07-06 15:13 • by John Bigboote
80553 in reply to 80507
Anonymous:


True, it should probably have been a Sub. As it is, would it even run?



Absolutely. Neither VB.NET nor C# requires that you use the return value of a function or even assign it into an LVALUE.

Re: Which is on HowMany?

2006-07-06 15:15 • by bah
80556 in reply to 80520
Carnildo:
sub Add
{
@_[0] = @_[0] + @_[1];
}


Um, not quite.  @_[0] is actually a list slice of @_ containing the first element.  The code above works, but you really meant to write:

  sub Add { $_[0] = $_[0] + $_[1] }
Carnildo:

 Therefore, Perl is clearly a superior langauge.


No argument there...
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment