Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer a fix for ordering members inside classes and structs following StyleCop patterns #172

Closed
dmgandini opened this issue Dec 24, 2014 · 8 comments

Comments

@dmgandini
Copy link

Regarding the issue Offer diagnostic to allow for ordering members inside classes and structs.
We could offer a fix for ordering classes and structs members following StyleCop documentation as recommended by @paulomorgado .

  1. Within a class, struct, or interface, elements must be positioned in the following order:
    • Fields
    • Constructors
    • Finalizers (Destructors)
    • Delegates
    • Events
    • Enums
    • Interfaces
    • Properties
    • Indexers
    • Methods
    • Structs
    • Classes
  2. Adjacent elements of the same type must be positioned in the following order by access level:
    • public
    • internal
    • protected internal
    • protected
    • private
  3. Fields Constant A constant field is placed beneath a non-constant field
  4. All static elements must be placed above all instance elements of the same type
  5. And finally by alphabetical order. This is my personal suggestion and is not part of StyleCop documentation.

This source:

            using System;
            namespace ConsoleApplication1
            {
                class Foo
                {
                    public class Foo2 { }
                    public struct Struct { }
                    public static Foo operator +(Foo f1, Foo f2) { return new Foo(); }
                    public static Foo operator -(Foo f1, Foo f2) { return new Foo(); }
                    void Method(string a) { }
                    internal void Method(int a) { }
                    public void Method1() { }
                    public void Method() { }
                    public string this[int i] { set { } }
                    public string Property { get; set; }
                    public interface Interface { }
                    public enum Enum { Enum1, Enum2 = 1 }
                    public event Action Event 
                    { 
                        add { EventField += value; } 
                        remove { EventField -= value; } 
                    }
                    public delegate double Delegate(double num);
                    public Foo()
                    {
                        Property = Field1 = Field = "";
                        EventField = EventField1 = () => { };
                    }
                    public event Action EventField1;
                    public event Action EventField;
                    public string Field;
                    public static string Field1;
                }
            }

Became:

            using System;
            namespace ConsoleApplication1
            {
                class Foo
                {
                    public static string Field1;
                    public string Field;
                    public Foo()
                    {
                        Property = Field1 = Field = "";
                        EventField = EventField1 = () => { };
                    }
                    public delegate double Delegate(double num);
                    public event Action EventField;
                    public event Action EventField1;
                    public event Action Event 
                    { 
                        add { EventField += value; } 
                        remove { EventField -= value; } 
                    }
                    public enum Enum { Enum1, Enum2 = 1 }
                    public interface Interface { }
                    public string Property { get; set; }
                    public string this[int i] { set { } }
                    public static Foo operator -(Foo f1, Foo f2) { return new Foo(); }
                    public static Foo operator +(Foo f1, Foo f2) { return new Foo(); }
                    public void Method() { }
                    public void Method1() { }
                    internal void Method(int a) { }
                    void Method(string a) { }
                    public struct Struct { }
                    public class Foo2 { }
                }
            }

@dmgandini is working on it.

@giggio
Copy link
Member

giggio commented Dec 25, 2014

This is related to PR #170. We need to wait until it merges to merge any PR directly related to this, even though the work can already start and use that PR as reference.
@dmgandini Are you planning to work on it?

@giggio
Copy link
Member

giggio commented Dec 25, 2014

BTW, I support the alphabetical ordering. :)

@dmgandini
Copy link
Author

It is my will to work on it this year yet, as soon as I close the issue 76.

@giggio
Copy link
Member

giggio commented Dec 26, 2014

Ok, it is your, then.

@giggio
Copy link
Member

giggio commented Dec 26, 2014

#76 is done. You can start this one now.

@dmgandini
Copy link
Author

@giggio, please, feel free to move it to working

@AdamSpeight2008
Copy link

We could do a group by the classification first, then sort each grouping.

@giggio
Copy link
Member

giggio commented Jan 14, 2015

Closed on #184.

@giggio giggio closed this as completed Jan 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants