• (cs)

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

  • (cs)

    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!

  • Gnictigezoink (unregistered)

    add( wicked, awesome )

  • steveth (unregistered)

    WTF = (Which + HowMany ^ WhyGodWhy) * StopTheInsanity

  • Anon (unregistered) in reply to Alex Papadimoulis

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

  • rammadman (unregistered)

    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.

  • (cs) in reply to Alex Papadimoulis
    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.
  • Last Bubble (unregistered)

    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.

  • eddiedatabaseboston (unregistered) in reply to Benanov
    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:

    <font face="Courier New">which = which + howMuch

    </font>
  • AndrewVos (unregistered) in reply to Anon

    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.

  • Obi Wan (unregistered)
    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!?!?!
  • Paul (unregistered) in reply to Obi Wan

    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?

     

  • benryves (unregistered) in reply to Obi Wan

    Which is passed by reference, so the result is written back to the first argument passed.

  • is it that hard? (unregistered) in reply to Paul

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

  • Mike (unregistered)

    Bah.  What they should have done is overloaded the += operator to do this.

  • Geoff (unregistered) in reply to Alex Papadimoulis
    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?

  • Antony (unregistered) in reply to Obi Wan

    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.

  • (cs) in reply to Paul
    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
  • (cs)

    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)

  • foxyshadis (unregistered) in reply to Anon
    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?
  • a possible explanation: (unregistered) in reply to Antony

    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.

  • Guybrush Threepwood (unregistered)

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

  • Thorgull (unregistered)

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

  • foxyshadis (unregistered) in reply to Obi Wan
    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.
  • (cs)

    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.

  • (cs)

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

  • Buh Foon (unregistered) in reply to Carnildo

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

  • (cs) in reply to a possible explanation:
    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

  • Shizzle (unregistered) in reply to Antony
    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

    <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.
  • narcisist (unregistered) in reply to Paul
    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))

  • Shizzle (unregistered) in reply to Jeff S
    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!
  • Jimmy Jimmy (unregistered) in reply to benryves

    The real wtf is the lack of understanding passing by reference and value by the comment posters.

  • (cs) in reply to foxyshadis
    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)
  • narcisist (unregistered) in reply to narcisist

    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.

  • Loren Pechtel (unregistered) in reply to Jeff S
    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
  • (cs)

    Theory #1:
    Real programmers abstract everything!

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

  • Anonymous (unregistered) in reply to Jeff S
    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
  • a possible explanation: (unregistered) in reply to Jeff S
    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)
  • DaveK (unregistered) in reply to Alex Papadimoulis
    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....


  • Anon (unregistered) in reply to Obi Wan
    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).
  • Got enough wtfs of my own (unregistered) in reply to Disgruntled DBA

    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!

  • (cs) in reply to Jeff S

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

  • jovnas (unregistered) in reply to is it that hard?
    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...)
  • (cs) in reply to jovnas

    doh...
    ment "sense" not "since"...

  • (cs) in reply to Ben Adams
    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
  • Onanymous (unregistered) in reply to jovnas
    jovnas:
    doh...
    ment "sense" not "since"...


    Yeah, I was wondering why it didn't make any since...
  • (cs) in reply to Carnildo
    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.

  • (cs) in reply to Geoff
    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.
  • ratatask (unregistered) in reply to Obi Wan
    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!?!?!


    Please consider what the ByRef means vs ByVal
    This function does something - it's usefulness can ofcourse be discussed.

  • (cs) in reply to a possible explanation:
    Anonymous:
    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)


    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. 

Leave a comment on “Which is on HowMany?”

Log In or post as a guest

Replying to comment #:

« Return to Article