none
C# refactoring free tool RRS feed

  • Question

  • Hello,

    Tool

    I'm still looking for a free tool or via browser.
       Input code --> Output code improved code, opportunities show.
      
    Does anyone know something?
    Like this

    https://entwickler.de/wp-content/uploads/2016/09/C_Spickzettel_entwickler-de.pdf

    or this.
    
    //OLD
    public double Volume
    {
      get{ return a * b * c;}
    }
    
    //NEW
    public double Volume => a * b * c;
    
    
    //OLD
    public string WholeName
    {
      get{ return this.FirstName + " " + this.Name;}
    }
    
    //NEW
    public string WholeName => this.FirstName + " " + this.Name;
    
    
    //OLD
    public void DoSomething2()
    {
    	var handler = SomethingHappened;
    	if (handler != null)
    	{
    		handler(this, EventArgs.Empty);
    	}
    }
    //NEW
    public void DoSomething() => SomethingHappened?.Invoke(this, EventArgs.Empty);

    Best regards Markus

    Monday, June 24, 2019 2:35 PM

Answers

  • Resharper refactored to the below by using a ternary operator, which is done by clicking on the if-statment and folowing the suggertions suggested by the light bulb.

    You can click on a line of code, and Resharper may or may not determine that the code can be refactored and make suggestions. 

    https://www.dotnetperls.com/ternary

    public void Save(DtoProduct dto)
            {
                int? y = 3;
                int? x = 2;
    
                y = x == null ? -1 : x;
    
            }
        }



    • Edited by DA924x Thursday, June 27, 2019 7:30 PM
    • Marked as answer by Markus Freitag Sunday, June 30, 2019 3:23 PM
    Thursday, June 27, 2019 7:26 PM
  • The idea behind that code sample is a analyzer will/may evaluate the code without hard coded values.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, June 29, 2019 11:57 AM
    Moderator

All replies

  • VS itself shows some.. press Ctrl-dot. Just run code analysis.

    The most referenced 3rd party tool is imho ReSharper.

    Tuesday, June 25, 2019 2:59 PM
  • Going with what Sfefan indicated about ReSharper, the product is amazing for refactors with plenty of customization yet it's not free. VS2019 is better than older versions of Visual Studio, one of PM's at Microsoft urge me to try it over ReSharper and did with a co-workers VS2019, it's very good but not as good as ReSharper. Note Resharper runs parallel with VS similar feature engine so it takes longer to fire up Visual Studio and sometimes the kind of compete against each other. 

    What I have not tried is Refactoring Essentials for VS2015 and VS2017, see refactors here.

    Also Roslynator.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, June 25, 2019 4:36 PM
    Moderator
  • I keep a subscription for Resharper. I can install it on any machine with the permission of the client and remove it when done. I do nothing in home development or professionally without Resharper.

    It doesn't break the bank.

    Tuesday, June 25, 2019 4:57 PM
  • Dear All,
    Thanks.
    I'm looking, that's right. It does not work as expected?
    //int y = x ?? -1;
    //if  x == null, set y to -1.
    int? y = 3;
    int? x = 2;
    if (x == null)
     y = -1;
    else
     y = x;

    t1

    t3

     
    How is the procedure to optimize a code snippet?
    Please see my screenshots.

    Regards Markus
    Thursday, June 27, 2019 5:24 PM
  • Resharper refactored to the below by using a ternary operator, which is done by clicking on the if-statment and folowing the suggertions suggested by the light bulb.

    You can click on a line of code, and Resharper may or may not determine that the code can be refactored and make suggestions. 

    https://www.dotnetperls.com/ternary

    public void Save(DtoProduct dto)
            {
                int? y = 3;
                int? x = 2;
    
                y = x == null ? -1 : x;
    
            }
        }



    • Edited by DA924x Thursday, June 27, 2019 7:30 PM
    • Marked as answer by Markus Freitag Sunday, June 30, 2019 3:23 PM
    Thursday, June 27, 2019 7:26 PM
  • I'm curious now to your last test which refactor may see the true values rather than random values such as the following.

    var result = Enumerable.Range(0, 10)
        .Select(item => (int?)item)
        .Select(x => x.Value % 2 == 0 ? x : null as int?)
        .ToArray();
    
    for (var index = 0; index < result.Length; index++)
    {
        Console.WriteLine(result[index].HasValue
            ? $"index: {index} value: {result[index].Value}"
            : $"index: {index} value: null");
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 27, 2019 7:47 PM
    Moderator
  • Hello Karen,

    I think it works only through the cost-sensitive tool Resharper?
    The code is displayed with the free tools not optimized? After the new features.
    Or can someone publish an example we should go with the tool?http://vsrefactoringessentials.com/

    Maybe the new tool from VS2019 enough? I do not have that yet.

    20190628, 13:44:32.117 [012336] info: index: 0 value: 0
    20190628, 13:44:32.117 [012336] info: index: 1 value: null
    20190628, 13:44:32.117 [012336] info: index: 5 value: null
    20190628, 13:44:32.117 [012336] info: index: 6 value: 6
    20190628, 13:44:32.117 [012336] info: index: 7 value: null
    20190628, 13:44:32.117 [012336] info: index: 2 value: 2
    20190628, 13:44:32.117 [012336] info: index: 9 value: null
    20190628, 13:44:32.117 [012336] info: index: 8 value: 8
    20190628, 13:44:32.117 [012336] info: index: 3 value: null

    What do you want to say? Can you  me/us explain it? 

    Thanks for all answers.
    Greeting Markus

    Friday, June 28, 2019 3:48 PM
  • Hello Karen,

    I think it works only through the cost-sensitive tool Resharper?
    The code is displayed with the free tools not optimized? After the new features.
    Or can someone publish an example we should go with the tool?http://vsrefactoringessentials.com/

    Maybe the new tool from VS2019 enough? I do not have that yet.

    20190628, 13:44:32.117 [012336] info: index: 0 value: 0
    20190628, 13:44:32.117 [012336] info: index: 1 value: null
    20190628, 13:44:32.117 [012336] info: index: 5 value: null
    20190628, 13:44:32.117 [012336] info: index: 6 value: 6
    20190628, 13:44:32.117 [012336] info: index: 7 value: null
    20190628, 13:44:32.117 [012336] info: index: 2 value: 2
    20190628, 13:44:32.117 [012336] info: index: 9 value: null
    20190628, 13:44:32.117 [012336] info: index: 8 value: 8
    20190628, 13:44:32.117 [012336] info: index: 3 value: null

    What do you want to say? Can you  me/us explain it? 

    Thanks for all answers.
    Greeting Markus

    Hello Markus,

    Here is how I see it, each tool has their own set of rules. If you were to use three different refactoring/analyzers each which report differently on the same code.

    Before I had ReSharper I wrote code against a different refactoring/analyzer then later on opened that code with ReSharper installed and reported the code the first analyzer said was good ReSharper disagreed and upon inspecting both recommendations agreed ReSharper did a better job to what I know are best practices.

    Now there are times I disagree with ReSharper and it has an option to indicate I have ignored it's recommendations while other tools that is not possible. 

    Bottom line is the tool is not always right nor is the developer. With experience as a developer one learns when and when not to listen to a tool.  

     


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 28, 2019 4:41 PM
    Moderator
  • Hello Karen,

    The tool is ok, I understand what you mean.

    But what you want to say with this?

    var result = Enumerable.Range(0, 10)
        .Select(item => (int?)item)
        .Select(x => x.Value % 2 == 0 ? x : null as int?)
        .ToArray();
    
    for (var index = 0; index < result.Length; index++)
    {
        Console.WriteLine(result[index].HasValue
            ? $"index: {index} value: {result[index].Value}"
            : $"index: {index} value: null");
    }

    Have you a short explanation, please?

    Thanks. 

    Greetings Markus

    Saturday, June 29, 2019 11:29 AM
  • The idea behind that code sample is a analyzer will/may evaluate the code without hard coded values.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, June 29, 2019 11:57 AM
    Moderator