• (cs)

    <font face="Arial" size="2">OK, forgive me if I'm being redundant, but the real WTF is the fact that this was even declared as a function at all!

    WHY would I do:</font>
    Add(SomeNumVar, 12)

    <font face="Arial" size="2">when I could just do:</font>
    SomeNumVar += 12

    WTF???

  • oggiejnr (unregistered) in reply to Anonymous

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

    That wouldn't work - GetContents()?? judging by the +5 we're dealing with value types so it should be:
    Connect2Computer("Bean").Hardware.Disks.Partition[0].GetPersonalData().Dir("Paula").File("Brillant").SetContents(Connect2Computer("Bean").Hardware.Disks.Partition[0].GetPersonalData().Dir("Paula").File("Brillant").GetContents () + 5)

    or the simple way with these new fangled properties

    Connect2Computer("Bean").Hardware.Disks.Partition[0].GetPersonalData().Dir("Paula").File("Brillant").Contents += 5

    the property wouldn't work with the the original function anyway - can't pass properties ByVal (at least using ref in C# so its probably the same in VB.NET)

  • (cs)

    You guys are right about ByRef, the result is returned in the first arg, sadly, I see this alot

  • John Hensley (unregistered) in reply to eddiedatabaseboston
    Anonymous:
    Ah, yes.  The good old days, when you had to write:

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

    </font>

    NO YOU FOOL! What if some Aspergerian mathematician stumbles upon your code, substract "which" from both sides, and concludes that 0 = howMuch?

    It has to be "<font face="Courier New">Let </font><font face="Courier New">which = which + howMuch"</font>, because computer science is a branch of mathematics or something.

    (Disclaimer: Any offense to self-described Aspergerians reading this post is purely intentional)

  • Fixme (unregistered) 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?

    Probably both.

  • XIU (unregistered) in reply to John Bigboote
    John Bigboote:
    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.

     

    In VB.NET you don't have to the Add function will always return 0 BUT in C# you HAVE to write a return statement. At least that's what I think Anonymous ment. But you are right that you don't have to assign the return value from a function.

  • (cs) 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?



    Which is what?
  • eddiedatabaseboston (unregistered) in reply to is it that hard?
    Anonymous:
    What is 8.


    It's the number that comes after seven.

  • squirrel (unregistered) in reply to Loren Pechtel
    Anonymous:
      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)

    I think you're attempting to illustrate the different between macros and function, but nonetheless you just wrote

    foo() = foo() + 1;

    (a) You need an lvalue (b) You need to store the result somewhere anyway, so it is no more complicated to write a = a + b (c) It makes no sense to pass foo() by reference and (d) "Add" is a terrible subroutine even if you can make it work.

  • Wessel (unregistered)

    The real WTF here is the lack of Option Strict.

  • Someone (unregistered) in reply to Geoff
    Anonymous:
    True, it should probably have been a Sub. As it is, would it even run?
    In VB6, every function, parameter or variable without a specified type is of type Variant.
  • Marcelo (unregistered)

        You can't compare VB to C/C++/C#/Java, regardless of on what grounds you want to take it.

    One day i used to only know VB and i hated language flame wars because I was on the weaker side. That said, egos hurt, and whatnot;

    Very nice language nevertheless, i think they should use it on universities as a startup language, and them gradually move to C derived languages, steering as far from pascal as humanly possible.

    But I also hate those that think VB Programmers suck because they only know VB. Back in the old days I myself have coded several mission critical systems in VB that literally renders profit for the company running it, and its been doing so for years; they could not care less if it was written in Perl, C++ or x86 Assembly.

    And the bottom line is: You can write a WTF in any language.


    For those guys that have an english grammar post check webservice SOA oriented async trigger; don't even bother.

  • J.T. (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?

     



    The correct question, with apologies to Mickey Katz, is "What's what?"

    The answer is: "If I knew what's what I'd have gone to college!"
  • J.T. (unregistered) in reply to eddiedatabaseboston
    Anonymous:
    Anonymous:
    What is 8.


    It's the number that comes after seven.



    I just bet you were giggling uncontrollably while you typed that, thinking "Gee, I am so clever", didn't ya?

    captcha: stfu.
    "Help! I'm talking and I can't shut up!"
  • JS (unregistered)

    Actually, the fact that it's a function means the "byref" is wrong, and in fact means the whole thing is wrong. It should be one of these two:

    Private Function Add(ByVal Which As Integer, ByVal HowMany As Integer) As Integer
    Add = Which + HowMany
    End Function
    Private Sub Add(ByRef Which As Integer, ByVal HowMany As Integer)
    Which += HowMany
    End Function

  • (cs) in reply to J.T.

    Meh..
    It's not symmetric.
    Imagine the maintenance programmer who has to hack his way around to find why
    Add(10,5) and Add(5,10) return different values.

  • bit (unregistered) in reply to Shizzle

    We all know that you could do i = i + 5. But

    Add(nProdQuantity,3)

    is faster to type and less error-prone than

    nProdQuantity = nProdQuantity + 3

    It's a stupid example, but you get it.   

  • (cs) 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.....

    Programmer 1: When you call the Add function, which parameter gets passed first?
    Programmer 2: Every reference to it. Afterwards, the garbage collector picks it up.
    Programmer 1: Which garbage collector?
    Programmer 2: Yes, what's wrong with that?
    Programmer 1: Let's slow down. First, how many parameters get passed?
    Programmer 2: No, howMany parameters get passed second.
    Programmer 1: I'm not asking you which parameter gets passed second.
    Programmer 2: Which parameter gets passed first
    Programmer 1: How many times do I have to ask you?
    Programmer 2: howMany gets passed second. this.Many times doIHaveToAskYou, he's been a great help in optimizing the performance of that method.
    Programmer 1: which method?
    Programmer 2: doIHaveToAskYou
    Programmer 1: Why do I have to ask you this many times?
    Programmer 2: Optimally.

  • undo (unregistered) in reply to Shizzle

    Delphi and Turbo Pascal both have a function that does the same thing built in:

    inc(x, 5);

    They also have a function for subtracting:

    dec(x, 5);

    Back in the days when I was coding in Delphi (and didn't know C and its +=), I thought this was handy and used it a lot in my code.

  • Anon (unregistered) in reply to byte_lancer
    byte_lancer:
    Meh..
    It's not symmetric.
    Imagine the maintenance programmer who has to hack his way around to find why
    Add(10,5) and Add(5,10) return different values.


    Um, check that one again.
  • (cs) in reply to DZ-Jay
    DZ-Jay:
    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.

    The term "by reference" doesn't have anything to do with references of the kind that are created with \ in Perl.  It means that if the subroutine/function/method/whatever modifies the variable representing the parameter¹, the modification shows up at the call site (assuming the argument was modifiable).  In other words, what happens in Perl if you modify the elements of @_.

    ¹ This isn't the same as modifying the object refered to by the variable - I mean making it refer to a completely different object.  In other words, assigning directly to the variable, rather than a property, array element, etc.

  • (cs) in reply to Anon
    Anonymous:
    byte_lancer:
    Meh..
    It's not symmetric.
    Imagine the maintenance programmer who has to hack his way around to find why
    Add(10,5) and Add(5,10) return different values.


    Um, check that one again.


    Why?
    Add(x,y) will update x while Add(y,x) will update y when all it should do semantically is to provide the sum of x and y.

    captcha : gotcha?
  • (cs) in reply to byte_lancer
    byte_lancer:
    Meh..
    It's not symmetric.
    Imagine the maintenance programmer who has to hack his way around to find why
    Add(10,5) and Add(5,10) return different values.

    You can't call Add(10, 5) since the first param is byref (as has been pointed out many times here). That means it must be an lvalue, and can't be a literal.

    Even if you could, adding 10 to 5 and adding 5 to 10 are close enough (after rounding) as to be the same.

  • waffles (unregistered)

    This is pretty amusing, but I think they should have gone all the way. Why use a '+' when you can wrap it in a Math function that can perform all sorts of math ops based on a variable passed to it?

    I'm envisioning...

    Private Function Add(ByRef Which as Integer, ByVal HowMany As Integer)
        Which = DoMath(Which, HowMany, "+", "I'm adding numbers, Ma!");
    End Function


    Private Function DoMath(ByRef Which as Integer, ByVal HowMany As Integer, ByVal What as String, Optional ByVal Why as String)
        Select Case What
           Case "+"
              Which = Which + HowMany
           Case "-"
              Which = Which - HowMany
           Case "/"
              Which = Which / HowMany
           Case "*"
              Which = Which * HowMany
           Case "Paula"
              Which = bool.FileNotFound
    EndFunction

  • (cs) in reply to John Smallberries

    Yea, I couldnt think of a variable name to put there after seeing that Which and HowMany WTFery.

  • (cs) in reply to John Bigboote

    I think some people are confused about the purpose of this website. It's not "What does That Function do?" :) If something is posted here, it is safe to assume that there's something wrong with it...

  • ich (unregistered) in reply to eddiedatabaseboston
    Anonymous:

    Ah, yes.  The good old days, when you had to write:
    <font face="Courier New">which = which + howMuch
    </font>

    Isn't the good old days more like
    <font face="Courier New">let which = which + howMuch</font>
  • ich once more (unregistered) in reply to ich
    Anonymous:

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


    OH MY GOD... when I just wrote that post... I hope I can guess VB syntax...

    Private Function Let(ByRef Which AS Integer, ByVal What AS Integer)
        Which = What
    END Function

    and then you can emulate the good old days with VB.Net... Let(x,1+2)
  • (cs) in reply to ich

    or better:

    load which
    add howmuch
    mov wtf

  • ohnonymous (unregistered)

    The real WTF is why a good function is being criticized while the "terrible" ones remain a mystery.

  • Avenger (unregistered)

    This is a security hole waiting to happen. Anyone could add anything they want to Which! They should have used this:

    Private Function AddEleven(ByRef Which As Integer) { AddTen(Which); AddOne(Which); }

    ...

    Private Function AddOne(ByRef Which As Integer) { Which = Which + 1; }

  • (cs) in reply to John Bigboote
    John Bigboote:
    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.


    Actually, C# does require a return statement when the return type of the method is not void.
  • rycamor (unregistered) in reply to Last Bubble
    Anonymous:

    WhoCanAddHowManyToWhichWhenWhy(Which, HowMany, Who, When, Why)


    Props for funniest function name I've seen in awhile.
  • Andre (unregistered)
    For the VB.NET deprived, I've posted the first comment with some cliff notes provided by D.

    Uhh, we get weird database queries and we are left to fend for ourselves. We get a simple VB += routine and we need cliff notes??

  • Cowbert (unregistered)

    You can pull the same stunt using PHP:


    function AddMyself (&$Which, $HowMuch) { $Which = $Which + $HowMuch; }

    (And a similar example is given in the PHP manual on passing by reference!) So I don't see how this is a really big WTF.

  • Jon (unregistered)

    Add(WTF, 1)

  • mark (unregistered) in reply to JS

    almost correct on either. D+

    return needed for the function and end sub needed for the su not end function.

  • JB (unregistered) 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)



    Excellent advice, this is sort of what TDD does for you, forces you to explain in a test before hand what you are doing. :)

    JB
    Captcha: stfu, now now, no need to be rude :)
  • Minkey (unregistered)

    These guys obviously write my employer's accounts software:

    thismonthscash = salary / 12

    pay(employee,add(thismonthscash, bonus))

    transfer(thismonthscash, beerfund)

  • Bob the Contractor (unregistered) in reply to Paul

    3

  • (cs) in reply to waffles

    Anonymous:
    This is pretty amusing, but I think they should have gone all the way. Why use a '+' when you can wrap it in a Math function that can perform all sorts of math ops based on a variable passed to it?

    I'm envisioning...

    Private Function DoMath(ByRef Which as Integer, ByVal HowMany As Integer, ByVal What as String, Optional ByVal Why as String)
        Select Case What
           Case "+"
              Which = Which + HowMany
           Case "-"
              Which = Which - HowMany
           Case "/"
              Which = Which / HowMany
           Case "*"
              Which = Which * HowMany
           Case "Paula"
              Which = bool.FileNotFound
    EndFunction

    I was thinking the same thing ... but maybe optionally adding in different numbering systems, like binary, hex, etc..

    Private Function DoMath(ByRef Which as Integer, ByVal HowMany As Integer, ByVal What as String, Optional ByVal Why as String, Optional Value How as String)

    HowMany = ConvertToDecimal(HowMany, How)

    etc.

  • (cs) in reply to J.T.
    Anonymous:
    Anonymous:
    Anonymous:
    What is 8.


    It's the number that comes after seven.



    I just bet you were giggling uncontrollably while you typed that, thinking "Gee, I am so clever", didn't ya?

    captcha: stfu.
    "Help! I'm talking and I can't shut up!"


    So Alex, why did the "troll" button disappear? Is there a way of bringing it back?
  • (cs)
    Alex Papadimoulis:
    Private Function Add(ByRef Which As Integer, ByVal HowMany As Integer)
      Which = Which + HowMany
    End Function

    "Reinventing the wheel?"

    Anyway, is their a possibility that such a function could be useful for handling errors in case of numeric overflows?  Of course their is no error handling indicated in the sample, but say you wanted to handle an error for certain integer variables differently than others?  Rather than use the 'On Error Goto HandleMeLikeABitch' you could simply use the function instead.

  • Brent Seidel (unregistered) in reply to byte_lancer

    I think that you could do this with an early version of FORTRAN. In one case you would wind up with 10 having the value of 15 through the rest of your program and in the other case 5 would have the value of 15. Made for some interesting debugging...

  • If I were King... (unregistered) in reply to byte_lancer

    Technically they return the same value. Namely 0. Secondly, I don't think you can pass a literal constant by ref. It is not an lvalue. Consider this example instead:

    a=5 b=10 c = Add(a,b) d = Add(b,a)

    c and d, the returned values, will be equal and 0. a will become 15, and b will become 25!

    The real WTF is the poor choice of function name - Increment() would be a better indicator of the behavior - and the fact that it always returns 0.

  • (cs) in reply to Cowbert

    Anonymous:
    You can pull the same stunt using PHP:


    function AddMyself (&$Which, $HowMuch) { $Which = $Which + $HowMuch; }

    (And a similar example is given in the PHP manual on passing by reference!) So I don't see how this is a really big WTF.

    It's *not* a really big WTF. It's just sort of a smallish, furry, curl-around-your-brainstem WTF.

    Really big WTFs are over on www.ReallyBigWTF.com

     

  • byWTF (unregistered) in reply to John Bigboote

    the REAL wtf... is that this Stupid language has to put by in front of a ref or a val.

    like wtf .. ref would have been fine, and so would have val

    byRef
    by reference, no shit huh sherlock?
    Ref would have been fine as anyone who grasps byRef knows wtf a Reference is.

    same goes for byVal.

    would anyone like to argue with me about this?
    I'm Ready.

    hehe j/k, seriously tho by is dumb.

    CAPTCHA:
     1337
    ...
    lame.

  • If I were King... (unregistered) in reply to If I were King...
    Anonymous:
    Technically they return the same value. Namely 0. Secondly, I don't think you can pass a literal constant by ref. It is not an lvalue. Consider this example instead:

    a=5 b=10 c = Add(a,b) d = Add(b,a)

    c and d, the returned values, will be equal and 0. a will become 15, and b will become 25!

    The real WTF is the poor choice of function name - Increment() would be a better indicator of the behavior - and the fact that it always returns 0.

    On second thought, maybe Excrement() is the best name for this function.

  • Are you deluded? (unregistered) in reply to a possible explanation:

    This is a function which ADDS TWO NUMBERS

    Addition is such a basic operation, its even supported in ASSEMBLER!  For goddsake, it's even implemented in SILICON!

    And this guy wrote a function to do it?

    I assume there is a 'Sub', 'Multiply' and 'Divide' function as well?

     

     

  • (cs) in reply to Ben Adams
    Ben Adams:

    Another effective [debugging] technique is to explain your code to someone else. ... 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)



    Also known as the Cardboard Programmer.

    Siamese cats (like my late and lamented 17-year old blue point Vashti) are excellent listeners. Vashti probably knew more about category theory and hierarchical graphs than any cat alive by the time I finished writing my dissertation. -- Charlie Martin

    I had a PulpFiction movie poster in my cubicle for a while. Uma Thurman's analytical, diagnostic, and motivational skills were amazing. --KrisJohnson

Leave a comment on “Which is on HowMany?”

Log In or post as a guest

Replying to comment #:

« Return to Article