- 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
I do this all the time. Is this... bad then?
Admin
Please enlighten me as to its purpose.
Admin
I don't know what's worse, the fact that you're wasting precious time doing a calculation on a value that (ultimately) equals itself, or the fact that on the next line you're doing a calculation that (ultimately) equals..itself.
Admin
At first I was thinking it was some kind of elaborate multiple check, but then I remembered that in VB (int)/(int) returns a double, so the result, barring floating-point error, is always going to be the value of iStart. I'm scared of how this code snippet makes sense to someone...
Admin
Its obviously a check to see if IOffset is zero....
Admin
Well, there's the biggest WTF yet!
How do you do integer division?
Admin
For those not aware, in a language like C, that'd be perfectly logical. Assuming that everything is an int, then the line
i=j*(i/j)
can be used to set i to the greatest multiple of j that is less than or equal to i.
of course, if VB treats int/int as a double, as pointed out above, then all bets are off...
Admin
Or, with higher optimization levels, it is a check as to whether iStart is 0, assuming that is really an assignment instead of an equality check.
Admin
For those not aware, in a language like C, that'd be perfectly logical. Assuming that everything is an int, then the line
i=j*(i/j)
can be used to set i to the greatest multiple of j that is less than or equal to i.
of course, if VB treats int/int as a double, as pointed out above, then all bets are off...
Captcha: kungfu (and not code fu...)
Admin
Aww, crap. Double post.
I'm an idiot.
Admin
Single equals sign IS the equality check in VB. Party on!
Admin
And you've just hit upon one of the many reasons why I hate VB.
Admin
Admin
You mean "a=b=c" being confusing as to whether it's the same as "a=c:b=c" or "a=(b=c)"?
Admin
That code was clearly not originally in VB, because that code would not do what the story claims it does in VB - the operator required for that would be , not /.
Admin
To do integer division in VB you use the backslash ''. Don't know why.
Admin
Admin
It just wouldn't be a TDWTF post without pointless complaints about the equality operator in VB (or Delphi, or SQL, or FORTRAN, or any functional language, or...)
Admin
It isn't clear WHAT the code is doing and the story doesn't make a claim to know either.
\ is integer division (Divide and the remainder disappears) / is normal division (Divide and the expected result is returned)
4\2 = 2 4/2 = 2
5/2 = 2.5 5\2 = 2
iOffset can be eliminated from the code as long as it is not 0. Same with the if-statement and equality comparison.
Admin
Interger division is with a backslash ().
Admin
In C, such an if would check whether iStart is aligned to iOffset boundaries. This assumes iStart and iOffset are integers. (The Hungarian notation would suggest this, but given the overall quality of the code, I would not be surprised to read something like "Dim iMyInteger As Double".) "if (iStart % iOffset == 0)" would be more legible and possibly more efficient, though. In Visual Basic, as was mentioned, this doesn't work at all because integer/integer returns a double. This check would return true or false in an unpredictable way depending on floating-point rounding. This is a Real WTF(tm).
And if it actually worked, then, since we already established that iStart is aligned, the iOffset*(iStart/iOffset) could be replaced with iStart. This is a also Real WTF.
The name of the function called in this line (aResults) is also a big WTF - no indication of what it does whatsoever. Let alone calling a variable iOffset that obviously doesn't store an offset but an alignment.
All this taken together makes the code unintelligible and dysfunctional. A worthy submission to Error'd.
Admin
If these are float variables, then iStart is often not equal to ioffset * ( iStart / ioffset ). It's going to be a few LSB off sometimes.
Of course,t hat assumes the equality operator does exact comparison, which isn't true in every language.
Admin
What's WTFed is the bad play on words: Hannes...truly lucked out. Hans in Luck...what a bad fairy tale. Fitting though, considering the fairy tale is a WTF in its own. +2 points for creativity :).
Admin
We knew that the moment you posted your captcha.
Admin
Float? In old ASP code? Surely you jest! Everything in "classic" ASP is a "variant". We'll have none of those confusing "type thingies" here!
Scott
Admin
Shouldn't that be..
"oh you've got be kidding me," "why whould they do that," "worse than failure --?!"
/Still don't like the name change.
Admin
PLEASE tell me you're kidding me...... Damn I hate VB :-|
Admin
This discussion of equality and assignment operators has me wondering. What is the value of an assignment in Visual Basic?
For example, in this snippet...
x = (y = 3)
...what is the value of the expression "(y = 3)"? Is the equal sign in this context treated as an assignment or as an evaluation? It's not a normal way to write a statement in VB, but what if one did?
I suspect that one of these cases would apply:
x gets the value True if the value of y is 3. Otherwise x gets the value False.
y gets the value 3, which is then assigned to x.
Every assignment yields the same value -- perhaps True. So y gets the value 3 and x gets the value True.
The compiler would kak. You're just not allowed to do this.
Anyone know for sure?
Admin
It's original purpose IMHO was to check if iOffset was any multiple of iStart...
In C, of course XD
Admin
x = y = 3 is equivalent to saying:
Compare y to 3, and take the boolean result of that comparison and assign it to x.
Program in VB for any length of time and you don't even have to think about it. It's perfectly regular.
I still have to think about if I'm doing integer or floating division, though. I can never remember which "/" or "" is which.
AdT: you mention the function "aResults". I'm sure that's not a poorly-named function, it's an equally poorly named array.
Admin
The leftmost equals sign in statement context is an assignment operator, and sets up an expression context on its righthand side. In an expression context, equals signs are comparison/equality operators.
This is what happens in C-like languages, and so is probably what most people on here expect...Admin
Just to clarify some of the VB6.0 questions:
Dim x As Integer Dim y As Integer
Private Sub Form_Load() x = (y = 3) MsgBox x 'This displays 0 y = 3 x = (y = 3) MsgBox x 'This displays -1 MsgBox (y = 3) 'This displays True End Sub
Admin
And in reverse, no less - in C the code as presented would be checking whether iStart was a multiple of iOffset!
(Which kind of makes me wonder if the values in the article got switched during anonymisation...)
Admin
Good answers, folks. Thanks. So, to sum up:
This Visual Basic statement: x = y = 3
...is equivalent to this Java statement: x = y == 3;
Admin
"\ is integer division (Divide and the remainder disappears) / is normal division (Divide and the expected result is returned) "
This is a carry-over from the old days (QBasic, GWBasic, etc.) when doing a floating point divide was more expensive than a integer one (actually it it still is, but it's all so fast, it doesn't matter anyway). There are some times (can't think of them now), when I wanted these features in C, normally there is a better way around the problem.
Admin
Still won't work.
iStart = 20 iOffset = k*20 (assume k>1) iStart/iOffset = 1/k which is zero in integer division.
So, depending on how integer division is handled, there are two possible answers to iOffset*(iStart/iOffset) 0, or iStart.
Neither of which appears to be a very useful result.
Now, if the parentheses were removed, it would be a different story.
So what the original coder was doing is some very subtle code that tests whether or not the system performs integer division or not. This makes the code completely cross platform, and can be easily ported to any one of the multiple implementations of VB used in production.
Admin
Here's your sign.
Admin
In VB the rules are simple:
---> 2) If this is an expression, then it is equality ---> 3) If this is a statement, then it is assignation
So to answer your question: x = y = 3
In that case, the first = is clearly a statement and thus an assignation. The second = is an expression and thus a comparison.
This means that
Using parentheses in that instance does not change anything else than readability (I sometimes use that kind of constructs, and in that case I use parentheses).
Addendum (2007-06-25 13:41): As someone was kind enough to point out my error, I guess I should correct myself on the assignation word. You would have to read assignment, obviously.
Please excuse the errors of a non-native English speaker/writer. And if you don't, well too bad.
Admin
I guess WTF would not be the same if people were to stop arguing about VB's syntax.
Now if those same people could spare one hundredth of the energy they use for complaining, and use it to actually try to understand how VB operators work, that would be a nice change. Obviously this is not going to happen since people have been trying to explain that for as long as I can remember on this site.
You don't like using the same operator for equality and assignation? Fine, I don't like having the opportunity to shoot myself in the foot every time I write a condition by forgetting an equal sign. But you don't see people like me complaining about it every third post. VB syntax on this makes perfect sense, as far as VB programmers are concerned (and please, VB programmers actually DO now other languages).
It seems to me that the more we see VB stuff, the quicker the focus changes from the code to the fact that people don't even try to understand VB's syntax and forget about what the article is about.
In any case, as the saying goes: a good programmer will be good whatever the language. A bad one will be bad whatever the language. You can write WTFs (original meaning) in whatever language you choose, they all offer plenty of ways to shoot yourself in the foot. Just because you can't be bothered trying to undertand the syntax (which would require about 1 minute of half concentration) does not mean the language is the WTF.
If you want to discuss the pros and cons of VB (and then please state the version because VB 6 and .NET are almost as different as C and C++) then fine, be my guest, I'll entertain you. But try to get your facts straight and don't argue on stupid syntax differences that don't matter.
This is why I prefer architectural WTFs. They are usually a much better insight into humanity's insanity.
Sorry, needed to vent this time.
Admin
Sad but true. I'm maintaining a couple old ASP websites right now and I've worked with ASP in the past. As far as practical use, I've never actually seen '' used (on purpose).
Admin
I use it regularly for the kind of project I do, which are not web based (control systems for test stands). You could perfectly argue that this actually make sense since in that case you don't rely on the type of the operands (which might be defined completely somewhere else) to know what kind of operation you are actually performing. All the context you need is right here. Again, a matter of taste and habits.
The fact that VB.net uses accounting rules for rounding floats into integers (alternating rounding to lower / rounding to upper based on the evenness of the operand), now THAT is painful in my line of work :)
Admin
You keep using that word. I do not think it means what you think it means.
Admin
Yeah, sorry, french word creeping up :( I realized that after the fact and was too lazy to edit my post :p
Addendum (2007-06-25 13:37): posted by "someone I know" ? Wonder who that could be... Care to give some clues ? (geographic location is not accepted as it would not narrow the field of possibilities at all).
Admin
In C you would have to use casts, I think.
Admin
Oh, finally a sane post. Glad to see I'm not alone.
Admin
That is precisely why Hungarian notation is a bad idea.
Admin
Clue #1: I'm a registered user. Clue #2: You're not the only person who's read my posts.
Admin
Woops, did not see you were registered.
I guess that might make me a bit self-centered then...
Admin
If you change type x to Variant it returns True as well... True equals -1 (at least in VB it does).
Admin
I do something like this to find out what part of the map the mouse is over...
I have an array of stuff that I need to access when the user clicks some where. The map is a grid of 16x16 squares(tiles)...
int id_x = Convet.ToInt32(Math.Floor(Mouse.X/16) * 16); int id_y = Convet.ToInt32(Math.Floor(Mouse.Y/16) * 16); Bitmap tile = (Bitmap)tiles[id_x][id_y]; //I'm not sure that is how I have the array hashed...
That isn't exact code... but that is the general idea... I think I pulled out most of my hair before I said you idiot Floor the value....
captcha: smile.. I'll try but it makes my face hurt