- 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
And even that looks like it has redundancy. The C++ equivalent would be
dynamic_cast< ICheckBoxControl& >( FindControl( controlName ).Checked = true ;
and the unlikely error case would be handled by the exception mechanism. Somehor I get the feeling, Dijkstra was right all along and (Visual) Basic rots the mind.
Admin
The point is that the C# version stands alone - it never raises an exception at all. Whereas your single line of C++ actually needs wrapping in a try...catch block, which leaves it ending up MORE verbose than the C#, not less.
Admin
The VB.NET compiler will actually return Nothing (for object types) or the default val (for value types) if you don't provide an explicit return. I'd suspect that C# would too, but that there's a compiler option to treat that warning as an error. Obviously, if at runtime you try to call methods and properties on a null reference, you will get a NullReferenceException no matter what language your IL was compiled from (though in VB, shouldn't that be a NothingReferenceException?). FYI, VB.NET 2005 will also warn you (by default - you can also treat it as an error) on not returning a value. As for "adding or omitting parentheses" [that results in syntax errors, not logic errors] and being allowed to compile - I have no idea what you mean there, and without a sample, I don't believe it. Unless maybe you're late binding and the methods you want don't exist (which is a programmer WTF, not the language. Though it should be off by default.)
Admin
Wow, you mean Subversion can automatically detect bad code upon checking in? That's a bad ass version control system you got there.
Admin
Sarcasm mode ON.
There's nothing wrong with that! You know those pesky M$ API programmers always make mistakes... Who can be sure that
and specially that obscure "FindControl" routine works as expected?.
Oh, and also... RTTI is BAD and SLOW! Shitty "TypeOf"! Bleh!
Let him re-create the entire API! Blame M$ on not opening it's source code!
SARCASM MODE OFF
Cheers!
Guido, from Las Palmas de Gran Canaria, Spain
Admin
gender = female (just to be clear).
Eeek, don't go making assignments to gender around here! You'll only confuse the poor lambs with your possible trans-gender ability!
Admin
You forgot; "... and doing it only with zeroes (ones making it waaaay to easy and modern)..."
("Quoted" from Dilbert)Admin
I agree, much cleaner and easier to read, but not so sure about the "grinding to a halt" comment. None Of Those Capitals Mixed Case Every Where End Sentence. It is also especially useful if you are building web applications and you use client side Javascript too. C# is very similar so it is easy to switch from one to the other. Go ECMAScript!
Personally I think that VB.NET was simply an effort by Microsoft to not give VB programmers a heart attack when they saw the .NET Framework development environment!
Seriously though, whatever floats your boat. The Framework will work with almost any language, so who cares. As long as I don't have to maintain COBOL, who gives a shit.
Admin
I think that the using VB.NET or C# is a religous argument, everyone will have their own belief over which is better / more concise / more maintainable / etc., however for a developer coming from a VB6 background VB.NET looks a little too familiar and doesn't encourage thinking in an OO manner despite the fact that it is a very different beast. At least if a VB6 developer moves to C# the different syntax will require them to at least pick up a book and encourage them to read up on OO principles before leaping into an "OO is crap, this used to be so much easier in VB6" train of thought.
Admin
For me there is another WTF here.
The existence of a keyword AndAlso
WTF???
LOL
Admin
well jeffs has got a interesting point here.
Let me think.... Hey fellows we got to make a better language because vb6 sux bigtime.
Yeah but what do we do with the and operator? Lets keep it old style vb6 semantics and introduce a better one wih a different name in vb.net. Sounds like a plan, lets do it.
Smells like dogshit to me...
Admin
I don't think so, me thinks there are a pair of guys who review every checkin/patch to the trunk.
You can use SVN's triggers to do some (limited) automated code analysis on patches though
Admin
I am continually amazed that people that write code like the first example even get jobs as programmers! Nobody hires a professional author without seeing what they can write first... why do companies hire programmers before seeing what they can program? I don't get it. That's the real WTF.
And by the way, as a someone who doesn't know VB... WTF "AndAlso" that's gotta be a typo. You can't be serious... I mean that's like writing "PlusAddition" ... or even "EqualsSameAs"
Admin
Awesome - that's made my day that.
Admin
By "that" I mean "this" - Brilliant
Admin
I don't know VB but I don't know what FindControl returns... if "controlName" is a parameter and this is a method then it is possible that a bad controlName could be passed to this block. In that case you couldn't know if thisControl is of the type "ICheckBoxControl" or not. So the example is an example of defensive programming. The author is defending against future morons using his method called "setCheckBox" to try and set the Checked field of an IButtonControl.
Even if FindControl is written to only return ICheckBoxControl objects then I still wouldn't call this a WTF. We reserve that for monumentally stupid things. Things like defining true... or spending 300 lines running complex regular expressions to return the value that was passed in as a parameter exactly the way you were given it without producing any side-effects.
Admin
Gak! The reworked code looks like COBOL!
Admin
From the MS CIL docs:
<FONT size=2>The </FONT><FONT face="Courier New" color=#000080>isinst</FONT><FONT size=2> instruction tests whether obj (type </FONT><FONT face="Courier New" color=#000080>O</FONT><FONT size=2>) is an instance of class. Class is a metadata token (a </FONT><FONT face="Courier New" color=#000080>typeref</FONT><FONT size=2> or </FONT><FONT face="Courier New" color=#000080>typedef </FONT><FONT size=2>see </FONT><FONT size=2>Partition II</FONT><FONT size=2><FONT color=#ff0000>_alink_partitionII</FONT>) indicating the desired class. If the class of the object on the top of the stack implements class (if class is an interface) or is a subclass of class (if class is a regular class), then it is cast to the type class and the result is pushed on the stack, exactly as though </FONT><FONT face="Courier New" color=#000080>castclass</FONT><FONT size=2> had been called. Otherwise NULL is pushed on the stack. If obj is NULL, </FONT><FONT face="Courier New" color=#000080>isinst</FONT><FONT size=2> returns NULL.</FONT>
<FONT size=2></FONT>
<FONT size=2></FONT>
<FONT size=2>So, yeah, the check for null is necessary, as isinst returns either the object cast to the requested type, or null.</FONT>
Admin
I did not disagree that the rewritten code was more correct than the original (and it appears in VB.Net the best you can do). My stipulation is that, at least in C#, there is a better, faster, way of doing the equivalent logic. Namely, calling FindControl (which, btw, is documented here: http://msdn2.microsoft.com/en-us/library/system.web.ui.control.findcontrol(VS.80).aspx), verifying the return value is of type ICheckBoxControl, performing the cast, and then calling the Checked setter.
Admin
Sorry, misread the post I quoted. Actually, yeah - is does return false if the expression is null. The code could be written as:
Control c = FindControl(name);
if (c is ICheckBoxControl)
((ICheckBoxControl)c).Checked = true;
However, you still perform the typecast twice with this code, versus once when using "as". I would think, though, that a good optimizer would be able to remove the the second cast and make the two forms identical.
Admin
Explain to me why curly braces and semicolons cause you to pull your hair out? I am sure case sensitivity also caused you to pull out your hair then too. These three things is why I love C like languages. i had to take a VB course in college and thought it was the most awful thing i seen. WHILE/WEND ??? IF/END??? there is no structure to that. true its easier to read as a language, but so is c. while () { } makes a lot more sense, easier to see bc all blocks of code use the same things....
Admin
As I mentioned earlier, it's amazing some of the moronic ignorance out there. What's worse is this script kiddie probably makes more than I do and will probably someday be my boss.
What Microsoft did with VB.NET is one of the ballsiest, smartest things a big sofware company has ever done with an existing flawed, but highly successful product. They had an extremely popular language, they recognized it's problems, and they rewrote it from scratch. They did not cater to the beginner, but rather to creating a true, robust OOP language. They took big chances by breaking compatabililty in lots of places to do so. But when they could, they kept compatability with older versions of VB when it didn't inherently violate any core principles.
Of course, this was a no-win situation. If it is 100% compatable with VB <=6 ,the language is a mess. The "VB" bashing continues (in this case, somewhat deservedly so) and it's not a solid language. If they do break compatability, people complain about *that*, even though the language itself is now a million times better. And no matter what, they all will still complain because it looks like BASIC and has the same core keywords and syntax. Should they have changed this? Should they never have released VB.NET? What would you have done with .NET? Just released C#, let VB die off, and that's that? Would that be a smart business decision? Would you continue VB6 on it's current path and release a more patched up, more crappy, more quirky, more "pseudo-OOP" language with VB7? How would you have implemented VB.NET? Broken ALL compatability by removing all of those BASIC keywords (End, Dim, And, Or, If-Then, End If, etc) you because you don't think they're "cool" or "hardcore" enough for you?
Let's get back to the AND operator. In VB6 it didn't short-circuit. In VB5 it didn't. In QBASIC, it didn't. Whether you like it or not, that's the way it was. VB derived from BASIC for god's sake, I don't understand why people get confused about that.
So, along comes VB.NET. As we know, MS is cleaning up VB -- rewriting it from scratch. Breaking it away from BASIC where necessary, but trying to keep it as backwards compatable as possible. Trying to make it a first-class language but still trying to make it accessable to users of older versions of VB. They get to the AND operator and think to themselves:
"Ok, in VB <= 6, it did not shortcircuit. We'd like to implement it as a shortcircuiting operator in VB.NET. But to do so, it will break compatability. It certainly doesn't compromise the integrity of our new language to have the AND operator behave as it does in VB <=6, as long as we add an operator that does shortcircuit. Please easy solution, right? So let's create *another* operator, and call it, AndAlso. Lame name, yep. that's life. But now we have the best of both worlds. Will script kiddies think this is something to complain about? Probably, but it solves the problem, adds a needed feature, it works, let's move on."
Why the hell do people have a problem with this? I know it's M$ and they're evil and "Windows sux" and all that, but this is ridiculous. The addition of AndAlso and OrElse was one of the most highly documented things about .NET, I am amazed that 5 years later people still get confused about it. Since the language has now been out for a while, knowing about these operators is a good indicator of who actually knows the language, and who is just complaining.
Make fun of older versions of VB a little if you want, but remember it's purpose. Remember why it was created. Remember who it was for. Think about how it changed over the years. Was Microsoft "stupid" to leverage BASIC and create a language for beginners? Were they "stupid" to add features over the years to it? Was it a bad thing to create a visual language for Windows that beginner programmers could use? Was it a bad thing to make it somwhat more powerful over time, but still backwards compatable? Should they never have released another version after 1.0?
Critizing the flaws in VB<=6 when comparing it to more robust languages is one thing, but to criticize VB.NET for these things is ridiculous. It's very amusing to me how it scares the "real programmers" out there that Microsoft wrote VB.NET in such a way that it contains virtually all the features of C# with simply a different language syntax (even moreso in .NET 2.0) They freak out about it, try to attack it as much as possible .. "it can't be a real language! It just .. cant' be! I don't know why, but I don't like it! I'm hard-core! My { and } keys have worn off my keyboard! VB sux! Yeah, that's it! "
Can we *finally* get past all this? Please?
Admin
I think its the other way around. From what I understand, the designers included short-circuit evaluation by default, just as god intended boolean expressions to be evaluated, but due to public pressure from the VB dev community ("short-circuit what?? that'll break all our current apps! I don't understand! put it back the way it was, please!"), full evaluation was reset as the default for boolean operators, and short-circuit added as an "extra bonus" with braindead names like "AndAlso" and "OrElse".
-dZ.
Admin
Actually, I think my version is a bit more succinct and easier to read. The intent was not to make things easier for the compiler. It seems 'cleaner' from the standpoint that (of course, in my humble opinion), reads rather nicely and has only one simple logical expression to parse in the if() statement.
Now, that aside, I do have to admit that I am not ignorant of the fact that using both 'as' and 'is' together in that regard is adding redundant IL code. Regardless of how many "trillionths of a second" it saves to avoid it, I think it still shows prudence towards not being wasteful --that in addition to actually writing very readable and maintainable code. I would challenge you to show how the code that I have presented is not maintainable.
I find it all too easy nowadays for people to illegitimately use the phrase "premature optimization" or "sacrifices readability and maintainability" if only to do nothing more than provide an excuse to avoid learning how things work "under the hood" and maintain their ignorance of same. It also explains why some people still write bloated, slow and leaky code for embedded processors after coming from a platform where their wasteful habits were allowed to flourish. Those habits, by the way, are the kinds of things that 'good' programmers should really have to resist.
Admin
The type is not checked twice. The type is checked once with some shorthand reflection, and if it is the correct type, then it is type cast once. Her code is pretty much the most direct equivalent to yours that there would be in VB.
Admin
I disagree. The type is being checked twice:
Now, granted - what she is doing is good. She's checking the type to make sure the cast will succeed to avoid an unnecessary exception thrown if the type isn't ICheckBoxControl. But, the CType function must check the type to ensure the cast is possible. How else with the invalid cast exception be thrown if it doesn't check the type?
Admin
The real WTF is people are assuming the name Michelle means someone is female. I had a boss once who's name was Michelle, and he was definitely male. (He was from France, where apparently that is a common guy's name)
This is the internet, anyone can claim to be anything.
Admin
Michel is a boy's name. Michelle is a girl's name.
Admin
I will bet money that if your boss is a male from France, then he spells his name "Michel".
Admin
So the WTF is using TypeOf?
Admin
After reading everyone's replies to my post, I can't stop laughing. I'm not trying to mock.
All I wanted to do was congratulate women on being competetive in finding these errors. I rarely see a post by a woman.
Somehow I ended up as "the real WTF"!!! hahahahaha
You are all so cute sometimes! I adore so many of you. But look, other times you are so nasty to everyone who decided to speak his or her mind, you confuse me to bits. Would it kill you to respond kindly?? Or at least try to squelch the deep desire to know more than others and criticize even a compliment? Am I alone in this?
In addition and for kicks - I am a woman. I assume programmers are smart - because it takes some brains to do this, even doing it wrong. I am glad when a woman gets any positive recognition. And if Michelle is not a woman - I can say "Oops I was wrong" without needing a hailstorm of offensive comments. And finally, I give a fig.
thanks for responding anyway.
januarys
Admin
I think there's a difference between suboptimal and WTF, though your code is a definite improvement.
Admin
Obviously the right way to do this (for the former team members i mean):
For Each ctrl In Controls
Dim prop as Reflection.PropertyInfo = ctrl.getType().GetProperty("Checked", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)
If (Not prop Is Nothing) AndAlso (prop.PropertyType Is GetType(Boolean)) AndAlso prop.IsWritable Then
prop.SetValue(ctrl, True)
End If
Next ctrl
8-|
<font size="2">(i'm sure there are more WTF's in this code than in the original one, but hey, who cares? :P)
</font>
Admin
Spotted one:
But there have to be more, i did this from the top of my head...
Admin
That's all weel & good, except that it doesn't actually do what the original function did. (The revision assume the existence of a "controlName" variable. To actually duplication the original (in C#)
<FONT face="Courier New" size=2>foreach (ICheckBoxControl c in Control)
{
if (c != null)
c.Checked = true;
}</FONT>
(The foreach automatically handles the "as")
Admin
Don't be so démodé. Everybody knows AJAX is the way to go :)
Admin
That sounds so gay.... A gay gathering..... I don't want to imagine what they may using those sausages for...........
Admin
Well, speaking of ambigous, I'm not sure that I 'grep' your use of the word 'grep' in that context.
Do you mean 'grok'? (http://en.wikipedia.org/wiki/Grok)
And that was Martian, so bonus points for irony by using it in a sentance about english equivilents.
Admin
And it's comments like yours that make me say... good riddance.
Admin
Oh, I just had to add that I noticed the original was poster said she was a woman, so I wonder if Anonymous would say the same thing knowing that...but I am sure she made that comment assuming the original poster was male..so probably not. Shows how pathetic her attitude is, and again, don't let the forum hit your keyboard on the way out, you won't be missed.
Admin
Yes,
but itn't that the reason for colour syntax highlighting?
Admin
It has to do with short circuiting evaluation. Same thing with the OrElse operator. Why doesn't VB have the And & Or operators short circuit by default? Who knows.....
Admin
Today's "Joel on Software" has some very interesting views about interfaces.
Admin
Um, yeah. It's a derogatory term.
Admin
I'm sure that wasn't what was intended. If you're still reading, please help me understand what the offensive element was.
Was it simply calling attention that the submiter was a woman? I read that as more of an encouragement than a derision. More like a statment similiar to 'we need more people like you around here'.
Or was it the use of the term 'chicks'? To my ear, 'smart chicks' takes on a completely different meaning. It is completely different than as in "let's go pick up some chicks". I see the term 'smart chick' as a term of respect. Much in the same way that I refer to myself as a 'computer geek' or a female computer game aficionado may refer to herself as a 'gamer grrl'.
So please don't take offense. While I can't speak for "januarys", I really don't think that any offense was intended. (And by all means, submit a WTF if you have any.)
And if I'm really out of touch and just 'don't get it', you can at least submit my comments as a WTF.
Admin
Hear, Hear. (Here?). The Internet was invented for AJAX. No joke.
Admin
You can't win, you can't break even, and you can't get out of the game. . .
Admin
Your course was in which language again? I haven't seen "WEND" since GW-BASIC. Just because VB is called Basic doesn't mean it's the same as whatever you studied.
I don't know about you, but most of us don't use the delimiters as the main visual cue. We indent blocks.
Admin
There are lots of steps in the casting process that TypeOf doesn't do, so it really isn't doing it twice. Also, CType has some bloat in it to do VB style implicit conversions. Use DirectCast to get the equivalent of C# casting.
Dim thisControl As Control = FindControl(controlName)
If Not thisControl Is Nothing AndAlso TypeOf (thisControl) Is ICheckBoxControl Then
DirectCast(thisControl, System.Web.UI.ICheckBoxControl).Checked = True
End If
<FONT color=#000000>It also wouldn't be too much of a WTF to simply DirectCast it without testing first and let it throw an exception. Wrap the whole method in a Try or let Application_Error in Global.asax catch it. It will NEVER throw an exception unless coded wrong. So, any performance concerns of using Exceptions are irrelevant. By coded wrong, I mean that it has zero chance of actually working correctly, ie. the wrong control name was used. The If will actually swallow the error and make it more difficult to debug. At least add an Else and log the ocurrance if the debug TraceSwitch is on.</FONT>
<FONT color=#000000>Think about it, how often have you asked someone to write a block of code that "Sets the Checked property on the ICheckBoxControl interface of an object to True, or simply and silently does nothing if the object doesn't implement ICheckBoxControl"? Shouldn't the spec for that method say it should throw a "WhatTheHellShouldIDoWithThis" exception? Remember error are the frameworks way of showing what's wrong with your application. They aren't some kind of accusation that needs to be swept under the carpet before your boss sees them.</FONT>
Admin
Natural language? Programming more closely resembles mathematics than storytelling. Ask a mathematician why they use all those confusing symbols instead of writing out "three times X plus Z squared divided by the square root of Y factorial minus W to the fifth power". If you take the time to learn the symbology, it is really and truly easier to read and understand.