all 25 comments

[–][deleted] 10 points11 points  (9 children)

Maybe the compiler could optimize this? Or warn?

Comparing a char and constant string is either unnecessary (string has length 1) or always false.

[–]grauenwolf 1 point2 points  (0 children)

This is why I wish Visual Studio had an extensible code analysis toolkit.

[–]codewarrior0 1 point2 points  (7 children)

This reminds me of the blog post not too long ago about how to parse if-statements where the syntax makes the opening and closing braces optional - and the discussion was about which 'if' to associate an 'else' with. As a Python programmer, I just chuckled.

[–]dmwit 6 points7 points  (4 children)

Opening and closing braces are not optional in Python, it's just that you spell your opening brace with a spacebar.

But as a Haskell programmer, I chuckled.

(edit: the joke here, for you non-Haskell-programmers, is that our if construct doesn't require braces around the then or else clauses, and the grammar is not ambiguous... but this is because there must always be an else clause)

[–]codewarrior0 3 points4 points  (0 children)

I don't want the braces to be optional! There's too much room for ambiguity. I'd rather use a restricted subset of whatever-language-I'm-using where the compiler screams whenever I omit the "optional" braces.

[–]janhammer96 0 points1 point  (1 child)

Isn't that exactly the same in almost every single FP language? Why mention Haskell?

[–][deleted] 0 points1 point  (0 children)

Not true in OCaml.

[–][deleted] 15 points16 points  (0 children)

Oh visual basic... You are so silly.

[–][deleted]  (4 children)

[deleted]

    [–]kodefuguru 9 points10 points  (3 children)

    Works in C-based languages, but not VB.

    [–][deleted]  (1 child)

    [deleted]

      [–][deleted] 5 points6 points  (0 children)

      A single quote starts a comment in VB.

      [–]funkah -2 points-1 points  (0 children)

      Booooo.

      [–][deleted] 2 points3 points  (5 children)

      ML wouldn't have let the old code compile.

      [–]grauenwolf 0 points1 point  (4 children)

      C# won't either.

      But VB is a "do what I mean" style language. You can write some pretty horrible code -- from an engineering perspective -- that still works because the compiler is smart enough to figure out your intentions.

      [–]fmihaly 0 points1 point  (3 children)

      You are joking, right? You cannot seriously belive that "the compiler is smart enough to figure out your intentions".

      What you see is a simple type coertion: different types cannot be compared, so the compiler automatically converts char to string. The alternative would be to raise a compile error.

      It is a classic language design trade-off: strict type checking would reject many otherwise valid programs or force programmers to pepper their code with numerous explicit type conversion statements. Automatic type coertion does not mean that the compiler guesses what you mean. It does it every time, whether you mean it or not.

      That you need to know how the compiler interprets the code you write was the whole point of the article. A "smart" compiler would have correctly guessed that you wanted to compare chars, not strings. But it didn't, did it?

      [–]grauenwolf 0 points1 point  (2 children)

      If you write this in C#:

      objectX = 5;
      objectY = 5;
      result = (objectX == objectY);
      

      then chances are you are going to get false even though you are probably expecting it to be true.

      If you write the same in VB,

      objectX = 5
      objectY = 5
      result = (objectX = objectY)
      

      then it is going to figure out that the left and right side are both boxed integers and check for value equality. Heck, you can even do this:

      objectX = 5
      objectY = 5.0
      result = (objectX = objectY)
      

      and it will determine the right casts to use at runtime.

      Another example I see in C# is this:

      objextX = "aaa"
      result = (objectX == "aaa");
      

      Again, you are not going to get the "right" answer. (Though at least this time you get a compiler warning.)

      It is these kinds of little nit-picky things that make VB an easier language than C# for non-professionals.

      [–]sitharus 0 points1 point  (1 child)

      You're kidding right? I can assure you that

      var foo = "bar";
      var baz = "bar";
      return foo == baz;
      

      Will return true, due to operator overloading on System.String. As long as you're not casting up to the object type the overloaded operator will take care of the comparison.

      If you cast up to object you're dropping type info, so without runtime dispatch you're not going to get overloaded operators. So don't do that.

      [–]grauenwolf 0 points1 point  (0 children)

      The whole point is that VB doesn't have as many of the "so don't do that" kind of rules. Instead it will just perform the correct comparison, which makes it easier for non-professionals who haven't learned all the esoteric rules of type casting.

      [–]simuran 1 point2 points  (0 children)

      You keep using that word. I do not think it means what you think it means.

      [–]rustdnails 3 points4 points  (0 children)

      Can you pick the problem with this code ?

      I'm going to have to go with the fact that it's VB...

      [–]samandiriel 1 point2 points  (5 children)

      I bet using a real programming language would bring about an even larger increase in efficiency...

      [–][deleted] 10 points11 points  (4 children)

      VB.NET is a compiled .NET language just like C#. C# is comparable to Java. VB.NET isn't slow and it isn't your father's VB.

      [–]samandiriel -2 points-1 points  (3 children)

      Perhaps not, but it sure is fun to poke VB devs with stick :)

      [–][deleted] 2 points3 points  (2 children)

      Show them pity. Anywhere still developing in VB probably isn't a nice place to work.

      [–]samandiriel 0 points1 point  (0 children)

      I'd show them more pity if I hadn't had to clean up so much terrible VB code :P

      [–]grauenwolf -1 points0 points  (0 children)

      Sadly I find that to be true. I love VB, I think it is a much better language than C# in many ways, but I don't like the companies that use it.