codecadwallader / codemaid

CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
http://www.codemaid.net
GNU Lesser General Public License v3.0
1.89k stars 356 forks source link

Feature Request: Sort Local Functions #947

Open kmgallahan opened 1 year ago

kmgallahan commented 1 year ago

Description

As per the title, it would be nice if CodeMaid had an option to sort local functions while reorganizing a file.

codecadwallader commented 1 year ago

Thanks for the suggestion. Do you have an example function which has multiple local functions inside of it? I haven't normally seen functions that are that complex.

kmgallahan commented 1 year ago

I love local functions! Although my coding style probably isn't broadly used, I find local functions great for two main cases:

I find this style helps with readability, keeps code close to where it is actually used, and like how local functions get indented by tools like Spade to show the hierarchy well.

An example of some pseudo-code that uses both would be rendering visuals using Win2D:

public void RenderSceneZed(){
  // asserts

  ...

  CheckLayoutMeasurements();
  RenderBackgroundLayer();
  RenderForegroundLayer();
  RenderUpdateLayer();
  RenderForemostLayer();

  void CheckLayoutMeasurements(){
    ...
    CheckLayout(a);
    CheckLayout(b);
    CheckLayout(c);

    bool CheckLayout(object asdf){
       ...
       return check;
    }
  }

  void RenderBackgroundLayer(){
    ...
    ds.FillGeometry(CreateSomeShape(someSize), x, y, Colors.Green);
    ds.FillGeometry(CreateSomeShape(someSizez), x, y, Colors.Green);
    ds.FillGeometry(CreateSomeShape(someSizea), x, y, Colors.Green);
  }

  void RenderForegroundLayer(){
    ds.FillGeometry(CreateAnotherShape(someSize), x, y, Colors.Green);
    ds.FillGeometry(CreateAThirdShape(someSizez), x, y, Colors.Green);
    ds.FillGeometry(CreateSomeShape(someSizea), x, y, Colors.Green);
  }

  void RenderUpdateLayer(){
    ...
  }

  void RenderForemostLayer(){
    ...
  }

  CanvasGeometry CreateSomeShape(Size size){
    ...
    return new CanvasGeometry(...);
  }

  CanvasGeometry CreateAnotherShape(Size size){
    ...
    return new CanvasGeometry(...);
  }

  CanvasGeometry CreateAThirdShape(Size size){
    ...
    return new CanvasGeometry(...);
  }
}