-
Notifications
You must be signed in to change notification settings - Fork 233
VB -> C#: Avoid loopTo in for loops if possible #1243
Description
Thanks for the wonderful tool. Here is an improvement that would make code conversion much easier.
When converting For loops from VB.NET to C#, if the final value of the range is not constant, a loopTo variable is added. While it makes sense from a semantic point of view, this makes the code slightly less readable.
If there are multiple for loops, we end up with a series of loopTo, loopTo2, loopTo3 etc. It would be wonderful if this could be simplified if possible.
For example, the following VB.NET code:
For i As Integer = 1 To n
NextIs translated as:
for (int i = 1, loopTo = n; i <= loopTo; i++) { }Proposed solution
When the upper bound is a local variable that is not explicited modified or passed as reference in the loop, simply use that variable instead of creating a duplicate. In the previous example, this would be:
for (int i = 1; i <= n; i++) { }Another handy improvement would be when iterating from 0 to n - 1 when the loop variable is an integer.
For i As Integer = 0 To n - 1
NextWould be translated using a strict comparison:
for (int i = 0; i < n; i++) { }