- 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
<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???
Admin
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)
Admin
You guys are right about ByRef, the result is returned in the first arg, sadly, I see this alot
Admin
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)
Admin
Probably both.
Admin
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.
Admin
Which is what?
Admin
It's the number that comes after seven.
Admin
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.
Admin
The real WTF here is the lack of Option Strict.
Admin
Admin
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.
Admin
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!"
Admin
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!"
Admin
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:
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
Um, check that one again.
Admin
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.
Admin
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?
Admin
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.
Admin
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
Admin
Yea, I couldnt think of a variable name to put there after seeing that Which and HowMany WTFery.
Admin
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...
Admin
Isn't the good old days more like
<font face="Courier New">let which = which + howMuch</font>
Admin
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)
Admin
or better:
load which
add howmuch
mov wtf
Admin
The real WTF is why a good function is being criticized while the "terrible" ones remain a mystery.
Admin
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; }
Admin
Actually, C# does require a return statement when the return type of the method is not void.
Admin
Props for funniest function name I've seen in awhile.
Admin
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??
Admin
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.
Admin
Add(WTF, 1)
Admin
almost correct on either. D+
return needed for the function and end sub needed for the su not end function.
Admin
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 :)
Admin
These guys obviously write my employer's accounts software:
thismonthscash = salary / 12
pay(employee,add(thismonthscash, bonus))
transfer(thismonthscash, beerfund)
Admin
3
Admin
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.
Admin
So Alex, why did the "troll" button disappear? Is there a way of bringing it back?
Admin
"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.
Admin
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...
Admin
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.
Admin
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
Admin
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.
Admin
On second thought, maybe Excrement() is the best name for this function.
Admin
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?
Admin
Also known as the Cardboard Programmer.