Increment operators ?

Does VB need prefix and postfix increment and decrement operators ?  Here’s an example I posted today for a question on adding an index with LINQ:

   Sub Main()

      Dim values() As String = {“aaa”, “bbb”, “ccc”}

      Dim index As Int32 = -1


      Dim view = From v In values _

                 Let x = increment(index) _

                 Select x, v


      For Each item In view

         Console.WriteLine(item.x & ” : “ & item.v)



   End Sub


   Public Function increment(ByRef value As Int32) As Int32

      value += 1

      Return value

   End Function


And this is the same code in C#:


      static void Main(string[] args)



         string[] values = { “aaa”, “bbb”, “ccc” };

         int index = -1;


         var view = from v in values

                    let x = ++index

                    select new{x, v};


         foreach (var item in view)


            Console.WriteLine(item.x + ” : “ + item.v);





Note in C# I didn’t need to write the increment function.

4 Comments so far

  1.   Jonathan Allen on March 25th, 2008          

    I would rather not open that can of worms. Those operators tend to make it much harder to reason about code. And as you demonstrated, there is an escape route when needed.

  2.   bill on March 25th, 2008          

    They don’t make it harder to reason about code when used appropaitely, as the above example demonstrates. With the push towards IEnumerable, and lambdas, I think there is a real need for them.
    You’re arguement that there is a workaround, can be taken against virtually any language construct, such as LINQ expressions and lambdas. The focus should be on simple expressions rather than having to stop and write some plumbing code then go back to the expression you were trying to write. In this example, C# is more fluent.

  3.   Michael Giagnocavo on March 31st, 2008          

    Yea I have to agree with Johnathan. I hate those operators in general. You’re taking advantage of a side effect to produce some procedural code — it feels dirty.

    If you want imperative code like incrementing indexes, then use an imperative structure. In this case, I’d write the code like this:

    var view = values.Select((v,x) => new { v, x });

    Some of the enumerable extension methods provide explicit indexing support for your use if you really need it.

    Yes, that doesn’t directly solve your issues if you’re using the query comprehension syntax — in that case, I’d break it down into smaller queries where you can explicity add an indexer.

  4.   Michael Giagnocavo on April 1st, 2008          

    Oh, I’ll back up my statement about it feeling dirty. Add a second foreach, and you’ll see that your numbers continue on up — probably undesirable. This hurts because the benefit of lazy lists is lost since you really must specify ToList to avoid corruption.

    For instance, before you print, add this:
    if (view.Any()) { … print here … }

    Oops. Now it starts printing at 1 instead of 0. This is actually one of the trickiest things with IEnumerables and captured variables — you gotta remember someone can start the process over. I’ve been bit a few times because of this.

    There are times for using imperative structures in LINQy code, but they should be really explicit, like by applying “ToList” or “ForEach”.