• (cs)

    Lazy? He wrote loads of code! If it was lazy it would use all those pre-packed library functions. That's lazy.

  • RFoxmich (unregistered)

    Function names as return values...hey Fortran did that frist.

  • foo (unregistered) in reply to RFoxmich
    RFoxmich:
    Function names as return values...hey Fortran did that frist.
    And then Pascal, which is probably what VB stole it from.

    Actually, this is one of the very rare occasions where I'm not saying VB is TRWTF, because I actually find this feature quite practical. In C/C++ I often find myself having to declare a result variable (and thus repeat the result type, remember to return it on every exit etc.). An implicit one can save some boiler-plate typing.

    Of course, the naming is secondary, whether it's the function name or a predefined one such as "result".

  • foo (unregistered)
    But this is simply an ugly and lazy HTML encoding function.
    And inefficient (O(n^2)) and wrong (32 is of course a normal space, not nbsp; turning all spaces into non-breaking ones might be useful in some limited cases (but then the function does more than it name suggests), but would be quite a WTF in normal text).
  • acode (unregistered)

    WTF

  • (cs)

    I didn't think VB.NET still allowed the "name as return value", I thought they required Return since like 2.0 (maybe even since the first version).

  • (cs) in reply to ObiWayneKenobi

    It still works, you just shouldn't do it.

  • Tom (unregistered)

    Switch statements don't fall-through by default in VB? I thought that was the WTF as I spent several seconds looking for a mention of that in the description.

  • w0rp (unregistered) in reply to Remy Porter

    This comment applies to Visual Basic in general.

  • Tongle (unregistered)

    I was told there would be unicorns...

  • Damien (unregistered) in reply to Tom
    Tom:
    Switch statements don't fall-through by default in VB? I thought that was the WTF as I spent several seconds looking for a mention of that in the description.

    They can't in C# either - but because it's meant to look like C, you still have to have an explicit break statement.

  • (cs) in reply to ObiWayneKenobi
    ObiWayneKenobi:
    I didn't think VB.NET still allowed the "name as return value", I thought they required Return since like 2.0 (maybe even since the first version).
    While the "return x" method was introduced a while ago, the VB4-6 mechanism of "FuncName = x" still works. Either can be used, but you will likely confuse yourself and others if your try to use both mechanisms in the same function.
  • (cs) in reply to Tom
    Tom:
    Switch statements don't fall-through by default in VB? I thought that was the WTF as I spent several seconds looking for a mention of that in the description.
    TRWTF is in C#; you must put a "break" or "return" at the end of each case of a switch statement. If code execution would hit the next case statement, the compiler complains. As the break is invariant in the case, don't make it look like you support case fall-though, like C and C++, in the first place. Just skip to the end of the switch when you get to the end of the individual case.
  • Pete (unregistered)

    Completely by accident, I found the source of the 'decode' part today:

    http://www.devx.com/vb2themax/Tip/19163

    It's almost OK when the WTF is internal, but when you find it while looking for genuine help it's a little scary...

  • (cs) in reply to foo
    foo:
    and wrong (32 is of course a normal space, not nbsp; turning all spaces into non-breaking ones might be useful in some limited cases (but then the function does more than it name suggests), but would be quite a WTF in normal text).
    And don't forget that if the original string came from Word, you might have one of Word's non-breakers in there, which won't be encoded as   (oops, did I write that?) but won't be recovered as a non-breaker either.
  • (cs) in reply to Damien

    Not strictly true:

    string s = "12345", res = ""; foreach (char c in s) { switch (c) { case '3': case '5': res += "(3or5)"; break; default: res += new string(c, 1); break; } } Console.WriteLine(res); // Produces: 12(3or5)4(3or5)

  • JustDizGuy (unregistered)

    Won't the decode function go into an endless loop if the input actually contains "&"? I'm not up on VB, but it looks like it decodes it to a literal ampersand, and the exit condition is "no more ampersands"

  • (cs) in reply to JustDizGuy
    JustDizGuy:
    Won't the decode function go into an endless loop if the input actually contains "&"? I'm not up on VB, but it looks like it decodes it to a literal ampersand, and the exit condition is "no more ampersands"
    No, because of the i+1 in the instr() call.
  • Skandranon (unregistered) in reply to Steve The Cynic

    The first argument of Instr is the position to start searching at, so it should, eventually move forward and past the new ampersand, though this code works more by accident than by design.

  • Smug Unix User (unregistered) in reply to Nutster

    Func = Something -> sets the value Return something -> sets the value and returns

  • Damien (unregistered) in reply to VeeTheSecond
    VeeTheSecond:
    Not strictly true:

    string s = "12345", res = ""; foreach (char c in s) { switch (c) { case '3': case '5': res += "(3or5)"; break; default: res += new string(c, 1); break; } } Console.WriteLine(res); // Produces: 12(3or5)4(3or5)

    Yes, multiple case labels can be applied to a single block, but that's still not the same as fall-through.

  • Krunt (unregistered) in reply to Nutster
    Nutster:
    Tom:
    Switch statements don't fall-through by default in VB? I thought that was the WTF as I spent several seconds looking for a mention of that in the description.
    TRWTF is in C#; you must put a "break" or "return" at the end of each case of a switch statement. If code execution would hit the next case statement, the compiler complains. As the break is invariant in the case, don't make it look like you support case fall-though, like C and C++, in the first place. Just skip to the end of the switch when you get to the end of the individual case.

    Because C#'s switch statement essentially boils down to a series of jumps. The VB.NET Case statement compiles down to conditional blocks.

  • (cs) in reply to Nutster
    Nutster:
    TRWTF is in C#; you must put a "break" or "return" at the end of each case of a switch statement.
    Or a "goto case": case fallthrough is supported, but must be explicit, which is the way it should be. Requiring that the programmer's intention be made explicit is a good thing in any project long-lived enough to require maintenance.
  • (cs)

    They should have used quantum bogodecode:

    Function HTMLDecode(ByVal html As String) As String Dim s as String

    s = 'A random string selected using a quantum process If html = HTMLEncode(s) Then HTMLDecode = s Else 'TODO: Destroy the universe End If End Function

  • (cs)

    Bad and buggy? Sure! WTF-Worthy? Probably not. It's rather at the "oh dear!" level on the scale of fuglyness.

  • JayGee (unregistered)

    If using the function as a variable is a return statement. Then doesn't the 4th line return what was passed in before it even gets to any of the code being bashed?

    HTMLEncode = Text
    

    If that is the case, then the rest of the code is unreachable. What's the point in critiquing unreachable code?

  • Sizik (unregistered) in reply to JayGee

    It only sets the value to be returned. It doesn't actually return from the function until you hit End Function.

  • Clement (unregistered) in reply to JayGee
    JayGee:
    If using the function as a variable is a return statement. Then doesn't the 4th line return what was passed in before it even gets to any of the code being bashed?
    HTMLEncode = Text
    

    If that is the case, then the rest of the code is unreachable. What's the point in critiquing unreachable code?

    The function name works as a variable that is ultimately returned. It is the same as declaring a variable at the beginning of the function and then returning that variable at the end of the function. The function does not exit as soon as the variable is assigned.

  • Ross Presser (unregistered) in reply to JayGee

    You might try actually looking up the behavior before posting. Then you'd appear less stupid. Your language bigotry would still shine through though.

    CAPTCHA: inhibeo. I wish I could inhibeo all the anti-VB bigots.

  • Ross Presser (unregistered) in reply to Ross Presser
    Ross Presser:
    looking up the behavior

    Aw, crap.

  • (cs) in reply to Ross Presser
    Ross Presser:
    Ross Presser:
    looking up the behavior

    Aw, crap.

    Yeah, you need square brackets, not anglies.

  • golddog (unregistered)

    On the plus side, it should be easy to refactor to use the standard library.

    I'm sure all the carefully-crafted unit tests will work just right afterwards...

  • JohanE (unregistered) in reply to Pete
    Pete:
    Completely by accident, I found the source of the 'decode' part today:

    http://www.devx.com/vb2themax/Tip/19163

    It's almost OK when the WTF is internal, but when you find it while looking for genuine help it's a little scary...

    Note that the Language in this page says: VB4,VB5,VB6,VBS Seems to me that someone brought this code into VB.NET from VB6 (or below) and never cleaned up (Indicated also by use of "Mid" function, among all other old-VB-isms). Without the framework, you'd have to do something like this. So, this is more dinosaur code that a WTF IMHO...

  • (cs) in reply to JayGee
    Smug Unix User (unregistered):
    Func = Something -> sets the value Return something -> sets the value and returns
    JayGee:
    If using the function as a variable is a return statement. Then doesn't the 4th line return what was passed in before it even gets to any of the code being bashed?
    HTMLEncode = Text
    

    If that is the case, then the rest of the code is unreachable. What's the point in critiquing unreachable code?

    I know not reading previous comments is kind of de rigeur on TDWTF, but this may be the first time I've seen a question that has been perfectly answered earlier in the thread.

    Actually, I quite like VBs approach in a lot of ways. For many trivial functions a result variable is unnecessary, but for longer/more complex functions it's a long way from uncommon to declare your own result variable and return it at the end of the function. All VB does is encapsulate that paradigm in the language.

    Addendum (2013-10-22 10:17): Sadly, taking so long to post that other people have already picked up on this makes me look less than smart :(

  • JayGee (unregistered) in reply to Sizik
    Sizik:
    It only sets the value to be returned. It doesn't actually return from the function until you hit End Function.

    That makes more sense. Well, everyones reactions and comments make more sense. Why someone would use that method of returning values in VB.NET still doesn't.

  • augue (unregistered) in reply to Ross Presser
    Ross Presser:
    Your language bigotry would still shine through though.
    Except he didn't actually say anything negative about VB. If anyone's the bigot, it's you for making assumptions about people who don't know VB and therefore need to ask questions about it.
  • Muphry (unregistered) in reply to Ross Presser
    Ross Presser:
    Ross Presser:
    looking up the behavior

    Aw, crap.

    Classic!

  • Dennis (unregistered) in reply to Krunt
    Krunt:
    Nutster:
    Tom:
    Switch statements don't fall-through by default in VB? I thought that was the WTF as I spent several seconds looking for a mention of that in the description.
    TRWTF is in C#; you must put a "break" or "return" at the end of each case of a switch statement. If code execution would hit the next case statement, the compiler complains. As the break is invariant in the case, don't make it look like you support case fall-though, like C and C++, in the first place. Just skip to the end of the switch when you get to the end of the individual case.

    Because C#'s switch statement essentially boils down to a series of jumps. The VB.NET Case statement compiles down to conditional blocks.

    Yes, C#-switch & VB-Select Case are totally different. At least in VB i could do:

    Select Case True
      Case A = 1
         DoStuff()
      Case B = 5
         DoOtherStuff()
    End Select
    
  • Deanis (unregistered) in reply to Pete
    Pete:
    Completely by accident, I found the source of the 'decode' part today:

    http://www.devx.com/vb2themax/Tip/19163

    It's almost OK when the WTF is internal, but when you find it while looking for genuine help it's a little scary...

    Written for VB4-VB6 (and VBS) in 2001. So I have no problem with the original code. The real WTF is that someone blindly copied-and-pasted it into a VB.NET project without checking for something newer and better. Likely a straight upgrade project.

  • (cs) in reply to Roby McAndrew
    Roby McAndrew:
    Lazy? He wrote loads of code! If it was lazy it would use all those pre-packed library functions. That's lazy.

    Lazy? Hey, the R-language deliberately uses lazy evaluation, you insensitive clod!

  • Evan (unregistered)

    "A quick and easy way to generate WTF code is to find some deceptively simple problem that’s been well solved, and reimplement a naive solution to the problem.

    Here's my puzzle contribution, which I think I posted to the forums a few years ago. Write the body of the following function:

    // Returns the average of 'a' and 'b' if it is a whole 
    // number. If the average is not a whole number, then return
    // either of the two closest integers. (For example, you 
    // might use this in a binary search where all you need to 
    // do is get "about" in the middle.)
    int average(int a, int b) {
      ....
    }
  • Herr Otto Flick (unregistered) in reply to Deanis
    Deanis:
    The real WTF is that someone blindly copied-and-pasted it into a VB.NET project without checking for something newer and better. Likely a straight upgrade project.

    Less gay bashing plz.

  • Evan (unregistered) in reply to pjt33
    pjt33:
    Nutster:
    TRWTF is in C#; you must put a "break" or "return" at the end of each case of a switch statement.
    Or a "goto case": case fallthrough is supported, but must be explicit, which is the way it should be. Requiring that the programmer's intention be made explicit is a good thing in any project long-lived enough to require maintenance.
    I thought they also allowed "continue"; I think I like that more than "goto case". Though now that I think about it, I also think I've made that mistake before. :-)

    But yeah, I agree; I think C# has the best compromise (at least without a syntax change); it avoids the confusion with C/C++ semantics for people who expect it to work that way, but also doesn't have the same problems, and the times when you actually want fallthrough are rare enough that the extra syntactic burden is minimal.

  • Sole Reason for Visiting (unregistered) in reply to Muphry
    Muphry:
    Ross Presser:
    Ross Presser:
    looking up the behavior

    Aw, crap.

    Classic!

    Clbuttic, surely?

    And there's absolutely nothing wrong with VB. Nor with the programmers who make such excellent use of it.

    OTOH, I see nothing wrong with using the function label (name) as the return value, provided you have a duck-typey sort of language. Why bother explicitly defining the return type?

    Not that VB (in any of its incarnations) is an FP language by any stretch of the imagination; but it's no sillier than requiring curly brackets all over the place. It's nothing more than a choice of syntax.

  • (cs) in reply to Ross Presser
    Ross Presser:
    Ross Presser:
    looking up the behavior

    Aw, crap.

    I just choked on the irony.

  • (cs) in reply to Evan
    Evan:
    "A quick and easy way to generate WTF code is to find some deceptively simple problem that’s been well solved, and reimplement a naive solution to the problem.

    Here's my puzzle contribution, which I think I posted to the forums a few years ago. Write the body of the following function:

    // Returns the average of 'a' and 'b' if it is a whole 
    // number. If the average is not a whole number, then return
    // either of the two closest integers. (For example, you 
    // might use this in a binary search where all you need to 
    // do is get "about" in the middle.)
    int average(int a, int b) {
      return ((a/2) + (b/2) + (a&b&1));
    }

    FTFY

  • (cs) in reply to Sole Reason for Visiting
    Sole Reason for Visiting:
    OTOH, I see nothing wrong with using the function label (name) as the return value, provided you have a duck-typey sort of language. Why bother explicitly defining the return type?

    heh. maybe I'm missing something. if you do FunctionName = val, and you do declare the Function with a return type, val is expected to be of that type (depending on your project settings, it may or may not compile).

  • (cs) in reply to Evan
    Evan:
    pjt33:
    Nutster:
    TRWTF is in C#; you must put a "break" or "return" at the end of each case of a switch statement.
    Or a "goto case": case fallthrough is supported, but must be explicit, which is the way it should be. Requiring that the programmer's intention be made explicit is a good thing in any project long-lived enough to require maintenance.
    I thought they also allowed "continue"; I think I like that more than "goto case". Though now that I think about it, I also think I've made that mistake before. :-)

    But yeah, I agree; I think C# has the best compromise (at least without a syntax change); it avoids the confusion with C/C++ semantics for people who expect it to work that way, but also doesn't have the same problems, and the times when you actually want fallthrough are rare enough that the extra syntactic burden is minimal.

    Yes, both VB .NET and C# .NET allow "continue"

  • (cs) in reply to Dennis
    Dennis:
    Yes, C#-switch & VB-Select Case are totally different. At least in VB i could do:
    Select Case True
      Case A = 1
         DoStuff()
      Case B = 5
         DoOtherStuff()
    End Select
    

    I've been hard-pressed to find a case where an If statement isn't better:

    If A = 1 Then
       DoStuff()
    Else If B = 5 Then
       DoOtherStuff()
    End If
    
  • Evan (unregistered) in reply to herby
    herby:
    Evan:
    "A quick and easy way to generate WTF code is to find some deceptively simple problem that’s been well solved, and reimplement a naive solution to the problem.

    Here's my puzzle contribution, which I think I posted to the forums a few years ago. Write the body of the following function:

    // Returns the average of 'a' and 'b' if it is a whole 
    // number. If the average is not a whole number, then return
    // either of the two closest integers. (For example, you 
    // might use this in a binary search where all you need to 
    // do is get "about" in the middle.)
    int average(int a, int b) {
      return ((a/2) + (b/2) + (a&b&1));
    }

    FTFY

    "X -> average(-1, -1) = 1" "X -> average(-1, -5) = -1"

Leave a comment on “Decode("
")”

Log In or post as a guest

Replying to comment #:

« Return to Article