- 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
Finally someone displays some common sense around here!
It amazes me that the sort of people who frequent this site, who you would imagine would all be highly competent programmers (and there clearly are a few...) simply do not understand pass by reference. In my opinion this means they should not be a professional developer.
In interviews I have posed the following questions to candidates who claim several years experience in C and Java:
What does this C function do?: (then present a simple (if stupid) C function that does an add operation similar to the one in this WTF, using one pointer-to-int and one int.)
Explain what happens in Java when evaluating string1 == string2 versus what happens when evaluating string1.equals(string2)
The results are frankly, horrifying. "Senior developers", "team leads", "head programmers" all crumble into dust. Don't let this be you - UNDERSTAND what you are instructing your computer to do!
Admin
Yeah... what's up with that?
This is a perfect example of de-coupling the numbers from math. Similar to de-coupling the letters from the alphabet, as in cryptography. The fact that the initial value is a ref, suggests that it is a moving target, and the function add() is simply, an effect on the target. One would only hope there is a reason for the reference, and that there are similar functions that affect the target, such as div, mult, sub. And that the target is really an array of possible targets, run throught a gamet of possibilities.
Or perhaps the function is really as pointless as it appears.
Admin
Funny enough, this is the fist time I've seen a post I couldn't say that for. Other than maybe the function not returning the value and using ByRef, but thats not a WTF, and neither is moderately bad naming structure. since those are local to the function, and the function is all of one line. Granted that sort of though process gets you into trouble later. but in the context of this piece of code its lame, but no WTF like yesterdays access db :)
captcha - perfection
Admin
Or... perhaps it's a primitive function for a home spun quick-sort routine where the ByRef[which] is an index to an array of pointer/handles of sort values, and the ByVal[HowMany] is the bi-section of the candidates.
I'm trying really hard to put a positive spin on this sucker.
Admin
Hmm, Love the comment, and can see how it could work. However I can just see the flood of calls to the helpdesk afterwards:
User: I still can't print to the printer.
Help Desk: I'm sorry sir, I don't have any record of your contacting the help desk.
User: But I told the teddy bear . . .
Admin
This function does not return a value. If it's a database project, you could write the result to a table. Or otherwise use a file.
CB
Admin
Admin
<FONT color=#808080><FONT color=#000000>Its for added security,</FONT> </FONT><FONT color=#000000>so they can track who is adding all the time and taking resources.</FONT>
Admin
I'm not sure where this 'people frequenting this site ought to be competent' comes from. My guess is that TDWTF is like a magnet to stupid and incompetent people who have an urge to feel superior and laugh at other people's shortcomings.
(This is obviously no different from us inteligennt and commpetent people seeking amusement, but I'm not sure we're even in majority.)
Admin
Nice of you to drop by and visit us "Birds of a Feather"
Admin
The really interesting question is:
XYZ = 5
XYZ = Add(XYX, 3)
What is the value of XYZ when the Add function returns?
Admin
I dont really see this as a What TF, more as a Why TF
My biggest issue with this is that it will change whatever variable I pass in for Which,
So my calling code has to be aware of this, If i dont want to change the value of the parameter i am passing in then the calling code will get messy. As i have to assign a temp variable to see the result i.e.
<FONT color=#0000ff size=1>Dim</FONT><FONT size=1> a </FONT><FONT color=#0000ff size=1>As</FONT><FONT size=1> </FONT><FONT color=#0000ff size=1>Integer</FONT><FONT size=1> = 5</FONT>
<FONT color=#0000ff size=1>Dim</FONT><FONT size=1> x </FONT><FONT color=#0000ff size=1>As</FONT><FONT size=1> </FONT><FONT color=#0000ff size=1>Integer</FONT><FONT size=1> = 0
x = a
Add(x, 10)
</FONT>Admin
I dont know VB, maybe B able to pass functions ar arguments (example is provided as fake Basic, I dont know VB)
<font color="#0000ff"></font><font color="#0000ff" face="Courier New">Function ForEach(ByVal FunctionExec As Function, ByRef Operator As Array, ByVal Params as Array)
FOR T=0 TO T<LENGHT(Operators) THEN
FunctionExec(Operator[T],Params[T]);
NEXT
End Function</font>
so you can do:
<font color="#0000ff" face="Courier New">ForEach( Add, Accounts, Tax )</font>
This can be usefull If you have assembler, c , perl, python or ruby background and you code VB.
--Tei
Admin
sorry pal. It can not. VB.NET arrays _can_ start with index 1 (i will not discuss this over and over). The For Each statement detects this for itself. But "<font color="#0000ff" face="Courier New">FOR T=0 TO T<LENGHT(Operators) THEN" <font color="#000000">will not</font>. <font color="#000000">You will most likely miss the last element.</font> <font color="#000000" face="Arial" size="2">If you wanna know more. Google yourself, or better: use the debugger!</font><font face="Arial" size="2">
</font>
</font>
Admin
This code reminds me of the days I was hired to assist on a VB456 (cant remember the version) project.
One day I came across the following function:
When I asked my coworker what the reason for this function was, he replied that the previous coder couldn't remember that in VB you use the & to concatinate strings. So they added a function for it.
Admin
What a funny thing. I'm wondering which language has ever tried to implement the assignment of a value to the result of a function. After all... Why not ! I assume it could reverse the execution of the function and give back the original parameters ?
Now, let's say add5(3) = 10
Would x = 7 ?...
Admin
Yanks eh? Still trying to do it their own way, they tried but got knocked out of the World* Cup early doors.
Also - Visual Studio.NET will point out stupid WTFs like this with the default warning settings. Anyone who can't see 'This function does not return a value' needs training. As language flame wars develop you should remember that c# and vb.net are far superior to java simply by virtue of the fact that they have used all the good ideas from Java and binned the rubbish ones. Build on this you dont need the rubbish JVM as all .net msil runs direct from the chipset.
Loved the teddybear helpdesk thing.
Also - agree with the 'incompetent noobs looking at mistakes to feel clever' point. I feel cleverer just by agreeing.
*WHERE World = 'ALL_COUNTRIES' NOT 'USA'+'CANADA'
Admin
We have a name for guys like you here "TheDoom".
You are a "drag and drop doos"..............
"c# and vb.net are far superior to java" .....bwahahwahawwaaaa
Jeez what a chop..........
So tell me, is the actual "pointer" passed by value with reference to an object, or .....oh sh*t, hang on .....can't talk object orientated stuff with C# .NET guys.......sorry dude
Admin
When will people learn there is a time and a place for every langauge. If you are so short sighted and narrow minded that you think that the language you choose to develop in is the best for every situation you are sadly mistaken.
Admin
Me, too. I tried "seines", but that didn't make any sins, and I'm feeling kinda evil today.
Admin
The /real/ WTF here ist the initial Posting... Let's go back to the roots:
- In the beginning, there has been someone who /thought/, that he understood this -simple looking- function
- afterwards he /thought/, that there would be a quite easier, equivalent solution (+=)
- and posted it to WTF.
But let's start to look at the gory details:
1.) This function /is not/ the same as a "+=". I don't know anything about VB - but I assume, that "+=" is allowed for many datatypes - so let's say two longs. This function assures that the calling block uses at least one integer (I assume automatic conversion with byValue-Types but not with byReference). So when a caller uses something like "byte a ; add(a,5);" - the mistake could easily be spotted.
2.) I never had the idea to code an add-Function, but now I know what I missed all the time :-) Assume you are sometimes working with a german keyboard-layout, sometimes on a french, sometimes on an US, ... - both "+" and "=" are always on different keys... Big problem. Assuming 50000 Programmers working 10 times a day on different keyboards, they sacrify at least 5 minutes a day for "refinding" the right "+"-key, so throughout a year, the company will lose some trillion USD. So after years of losing dollars and competitions - finally the ultimate "add"-function was built... Voila: No more "+" or "="-seeking... And... take a closer look at the clever name of the function:
Function-names with an "X" or "Y" would be a stupid solution (as they also change place in different keyboard-layouts).
So after years of searching, an intuitive name "add" was born. I'm fascinated !
Admin
The function increments it to 8, then the returned value of zero overwrites it: zero
Admin
quoted from Marcelo:
> i used to only know VB and i hated language flame wars because I was on the weaker side.
LOL! Brilliant!
> steering as far from pascal as humanly possible.
I agree if you meant Wirth's original Pascal. Delphi's Pascal > *
Admin
Looks to me as this code is a left over from switching from old ASP to ASP.Net. In ASP the return value of a Function is implicitly a Variant if it is not specified (still the wtf is that no value is returned). So this will introduce 2 more WTFs:
a function that does not return a value should be declared as Sub
Integer in old ASP is 2 byte while it is 4 byte in .Net
So long ... and cheers that site is awesome :)
PS: cba to read all 129 comments so forgive me if someone did already point that out :P
Admin
Maybe so, but it's still a WTF in my opinion because the whole function could have been replaced by in the first place:
Which = Which + HowMany
Consider the performance implications of using a function/method to perform a simple addition. Under the hood, the function translates into low-level code that:
1. pushes two parameters on the stack (the first being a reference pointer - an address)
2. pushes the current instruction pointer on the stack
3. creates thread local storage for any local variables (even if we have none).
4. jumps to the function address
5. performs the addition (see steps 1-4 below)
6. pops the original instruction pointer
7. pops the original parameter values
Contrast that with a simple addition operation which translates into:
1. Move contents of "Which" (memory value) into a register
2. Move contents of "HowMany" (memory value) into a register
3. Perform a register addition
4. Copy register result into "Which" memory location.
A lot of overhead for a simple math operation.
You do touch on an interesting point though - there are a lot of really bad VB.NET applications out there that started their life as a VB6 port. When migrating VB6 code, why not do a good thing and rewrite the bad portions of code as part of the process?
Admin
Yes, it's scary! I've worked with a lot of .NET programmers, and a lot of them don't know how reference parameters work - computer science 101.
Oooer.
Admin
You're using "Something.....GetContents() = value"? Why??
There's always a solution to messy things like that. You'd never do that since Connect2Computer, Hardware, Disks, Partition, Indexer, GetPersonalData etc... any of those propery accessors/methods might fail - if you can think of a 'real' example similar to this one but just as long, I can explain how there's always a really nice, easy, far cleaner method of accessing things. Creating an 'add' function is crap.
(captcha: 1337)
Admin
You must be new here.
Captcha: knowhutimean
Admin
This thread disproves your theory. Even with the By, a lot of people didn't realize that it was being passed by reference. Also, VB tends to be more descriptive rather than optimizing for shorter source code. Think about it, are you limited in how much code you write each day by your typing speed? Whould making all functions one or two letters help or hinder productivity?
Admin
A real world example, change "Which is on HowMany?" to "Re: Which is on HowMany?" in this forum:
<font size="1">
</font>[1]
<font size="2">document.getElementById("_ctl0__ctl1_bcr_PostForm___PostSubject").setAttribute("value","Re:" + document.getElementById("_ctl0__ctl1_bcr_PostForm___PostSubject").getAttribute("value"));
And a alternate:
</font><font size="1">
</font>[2]
<font size="2">function id(nameThing){
return document.getElementById(nameThing);
}
</font><font size="1">
</font>[3]
<font size="2">
id("</font><font size="2">_ctl0__ctl1_bcr_PostForm___PostSubject").value = "Re: " +</font><font size="2">id("</font><font size="2">_ctl0__ctl1_bcr_PostForm___PostSubject").value;
Yet another alternate:
</font><font size="1">
</font>[4]
<font size="2">
</font><font size="2">var xsubject = id("</font><font size="2">_ctl0__ctl1_bcr_PostForm___PostSubject");
</font><font size="2">xsubject.value = "Re: " + </font><font size="2">xsubject.value;</font>
<font size="2">
</font><font size="2">Some people nowdays code with style [1]. Notice that the use of a simple function "id", is for more easy to read code. Better code. So maybe this Add funcion is not a WTF.
</font><font size="2">
--Tei
</font>
Admin
Actually, the very moment it is returning I'd not be so sure it wont still be 8. The moment after it has returned 0.
Admin
Ok, got it. Last time I coded was in VB 3.0 (yes, the 16-bit version) so I was blissfully unawares. :P
Admin
You're deth-th-th-th-picable!
Admin
I never claimed I was a PROFESSIONAL programmer... :P
Admin
Wait, somebody missed a Star Trek referrence? On an IT site? Shameful....
Admin
Hey, give byte_lancer a break. The return values are, of course, the same, but it's still not symmetric. The side effects are different.
In the first case, 15 gets assigned to 10 while in the second case, 15 is assigned to 5.
Admin
As opposed to the VB.NET depraved?
Admin
149 responses, and no one has pointed out how convenient this function is if they ever change the definition of "add"?
--Rank
Admin
Fell for it hook, line and sinker noob......nice troll Doom....lol msil on chipset.....haha
Admin
No, no, no. You cannot write that unless you know that b = 0. Otherwise you get something like 1 = 1 + 2, which is wrong. That's why you need an Add function.
And don't ever Let 1 = 1 + 2. If you let that happen, the universe will come to an end. Worse, my bank balance will be cut in third.
--Rank
Admin
I remember that horror - ran into it on my first job (FORTRAN IV) - debugged it all the way down to a constant, dumped the assembly code in the debugger - looked at the data segment, and the location for '5' had been overwritten, so all subsequent references at that, and all other points - for '5' got the location of '5', but used the subsequently changed value. - argh!
Learned a *lot* from programming with those tools ;)
Admin
The anonymizers!! They do nothing.
Ok, I've got to admit that I goofed up. Twice. Got my mind cluttered by thinking too much.
And since this place cannot afford the luxury of a delete I let it be.
I should not have used constants in my 1st example; my bad - should have passed my code through a compiler like the others do.
I knew it returned the same values, but with two different side effects. Should have used post and pre-conditions to talk about the symmetric part.
Hope I'm excused now.
Admin
You know, this is an example of a WTF that makes no sense at first, and then you find more and more legal uses the longer you look at it.
A few good ones:
1) Suppose the object that implements this add method already has an add method -- maybe it extends AddableObject or something -- that takes two Objects, itnent on adding the second to the first. Now let's say the base class's method is very slow, uses reflection or something. Our new Add(Integer, Integer) method would shadow Add, and be much faster for Add methods internal to the class where both arguments are guaranteed to be Integers.
2) Suppose I have a delegate, Operation(Integer ByRef, Integer ByVal). Now, when the object is in count up mode, I set my local operation = new Operation(Add), and when it's in count down mode, I set operation = new Operation(Subtract). Now my Step() method just calls operation(counter, byHowMuch) -- no need for an ugly case statement. You could also build a multicast delegate and chain together a bunch of addition operations, throw in a subtraction, maybe an absolute value, anything that takes a pair of integers and affects the first one by the second.
And complaining because this is a function, not a subroutine, is pretty silly.
Admin
The real WTF is that...is that...oh never mind.
Admin
WTF are you talking about?
Admin
True dat. Except for ruby, which is just more evolved than the rest of the languages and actually makes you SMAERTER and increases your penis size when you write code in it.
I'm going to have to stop writing code soon, one of the hot chicks walks by and I accidentally knock my cube wall over.
sd
Admin
Integers only.
Admin
Add and sub are for cases where the field being added to is buried two or three levels down in a record type.
Mult and divide were functions that operated on data elements bigger than what the system supported.
Admin
Well, I am very in doubt with myself, whether I should call the original one a WTF. My first opinion was: This is a clear WTF, because even if the += Operator doesn't exist (for example in VB6), you could always do
<font size="2">a = a + b</font>
so there is no need for this function.
Then I remembered that I did the same in my early VB6 days:
<font face="Courier New" size="2">Public Function Inc(ByRef X As Long, Optional Y As Long = 1)
X = X + Y
Inc = X
End Function
Public Function Dec(ByRef X As Long, Optional Y As Long = 1)
X = X - Y
Dec = X
End Function</font>
The reason was the language I used before which had functions like that. I also thought, this was very handy, because you could write
<font face="Courier New" size="2">inc a</font>
instead of
<font face="Courier New" size="2">a = a + 1
</font>which of course is most used and some kind shorter, especially the longer the variable name is. And if you wanted to add something other, you could use it like our origin post code, something like:
<font size="2"><font face="Courier New">inc a, 5</font>
</font>But afterwards I didn't use it very much. First I put it in a separate common module to be independent and include it in several projekts. But then I was not very pleased to have to include another module for some sort of "basic" functions.
When I came across OOP, it was the dead for this function. I you want to have your classes as independent as possible, you should avoid refering to as few outside functions as possible. So the other choice is to put it in the class as a private method. And looking forward, to have this in many classes as as a private method looked very silly. So inside of classes I used the built in traditional
<font face="Courier New" size="2">a = a + 1<font face="Times New Roman">
</font><font face="Arial" size="3">My conclusion is:
In procedural programming maybe nice, in OOP it's a complete crap.</font><font face="Arial">
</font>
</font>
Admin
Look at this: Add(x,y)
Imagine you're a programmer looking at this line. You'll be asking yourself why it doesn't say AnyVariable = Add(x,y), and you'll most likely take one look at the add function and know why instantly. Provided you have any concept of reference values.
The only problem I can see in that respect is when a new guy starts using Add without reading the function or its previous applications.
x = Add(x,y) // Add assigns a value to (reference) x, and return 0: x = 0.
To be sure this doesn't happen, it should be declared a sub (with no ability to return values) rather than a function, so that x = Add(x,y) will be a syntax error and quickly debugged. (If ever occuring)
This is mostly based on assumptions regarding VB function handling. I've only programmed in Euphoria and C# myself.