Skip to content

About ++ or -- operator #72

@Whismeril

Description

@Whismeril

This code

int i = 0;
//++ or -- onl
i++;
++i;
i--;
--i;

//++ or -- in instruction
DoSomethingWithI(i++);

DoSomethingWithI(++i);

DoSomethingWithI(i--);

DoSomethingWithI(--i);

is converted like this

Private i As Integer = 0
'++ or -- only
i += 1 'perfect
i += 1 'perfect
i -= 1 'perfect
i -= 1 'perfect

'++ or -- in instruction
DoSomethingWithI(Math.Min(Interlocked.Increment(i), i - 1))'makes questions

DoSomethingWithI(Interlocked.Increment(i))

DoSomethingWithI(Math.Max(Interlocked.Decrement(i), i + 1))

DoSomethingWithI(Interlocked.Decrement(i))
  1. Interlocked.Increment(i) should work if System.Threading is imported, conversion doesn't notify that.

  2. In

Math.Min(Interlocked.Increment(i), i - 1)

why first increment and then calcul i - 1 ?

this

Math.Min(i, Interlocked.Increment(i))

does the same with one subtraction less

Math.Min(Interlocked.Increment(i), i - 1)

could be obscure for beginner, convert in 2 lines mays be easier for them

DoSomethingWithI(i++);

goes

DoSomethingWithI(i)
i += 1

and

DoSomethingWithI(++i);

goes

i += 1
DoSomethingWithI(i)

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions