Found the first thing in Refactor! that I don’t like

So far, the latest versions of CodeRush and Refactor! have been pretty impressive.  Unfortunately, I’ve just found the first bit of behaviour that I’m not too keen on.  Take the following snippet…

foreach (MyProvider provider in lps)
{
  Console.WriteLine(provider.AccountStatus);
}

  
Fair enough. Old Skool way to write a loop, but nothing too extreme. Refactor! prompted me that I may like to “Introduce ForEach Action”. I decided to take its advice, and it turned the routine into

Array.ForEach(lps,
delegate(MyProvider provider)
{
  Console.WriteLine(provider.AccountStatus);
});

 
OK, still does the same thing, but I hate the way it looks. It’s kind of a half-way house between old-skool and lambda. This was even pointed out by Refactor! – it suggested that I “Compress to Lambda Expression”.

So I did. The result was much more pleasing.

Array.ForEach(lps, provider => Console.WriteLine(provider.AccountStatus));

Lovely.

All I’d like to see in Refactor! is the ability to go from the first snippet the the last without having to take a detour through Uglytown. Is that too much to ask?

3 thoughts on “Found the first thing in Refactor! that I don’t like

  1. I’m not sure what you’re using for ‘provider’ and ‘lps’ but I tried to recreate this with integers….

    Note my C# is normally very poor so you’ll have to give me a little leeway..
    ————————————————————-
    foreach (int i in new int[] { 3, 4, 5 })
    {
    Console.WriteLine(i);
    }
    ————————————————————-

    I was offered ‘Introduce ForEach Action’… Which when accepted, gave me….

    ————————————————————-
    Array.ForEach(new int[] { 3, 4, 5 }, Console.WriteLine);
    ————————————————————-
    Which seems good to me… I am using RefactorPro 9.1.2

    Perhaps you have found a bug. It’s hard to tell with the small code sample provided. If so you should report it with a fuller code example to the Support Center (http://www.devexpress.com/Support/Center/)

  2. Thanks for the reply, Rory. I’m not sure I’d class it as a bug – more of an annoyance. I can see instances where you’d want to be able to convert to a delegate, but this simple behaviour isn’t one of them. Here’s a self-contained example in a console app.

    namespace RefactorExample
    {
    using System;

    internal class Program
    {
    internal static void Main(string[] args)
    {
    foreach (MyProvider provider in MyProvider.createProviderArray())
    {
    Console.WriteLine(provider.Name);
    }
    }
    }

    public class MyProvider
    {
    public string Name { get; set; }

    public string Description { get; set; }

    public int Identifier { get; set; }

    public static MyProvider[] createProviderArray()
    {
    return new MyProvider[] {
    new MyProvider {Identifier = 0, Name=”FirstTest”, Description=”This is the first test item.”},
    new MyProvider {Identifier = 1, Name=”SecondTest”, Description=”This is the second test item.”},
    new MyProvider {Identifier = 2, Name=”ThirdTest”, Description=”This is the third test item.”},
    new MyProvider {Identifier = 3, Name=”FourthTest”, Description=”This is the fourth test item.”},
    };
    }
    }
    }

    Apologies for the poor adherence to coding standards – it’s just an example. ;)

  3. Yeah I can see what you mean here… I previously felt similarly about how I had to “Introduce Local” on a string literal, before I could “Promote to Parameter”. This has been rectified in recent builds

Comments are closed.