Class could have static, private开发者_如何学Go, protected, public methods. Each method is made for modifying, adding, removing etc.
How do you group functions in class's code to make it clean to read? What is the best practices?
Thank you.
This is how I do it for Java classes:
- constructors
- public methods from implemented interfaces
- public methods overridden or methods declared abstract from extended classes (not Object, see below)
- public methods (other than getter/setter/Object methods)
- getters and setters, in the order the property is declared
- equals, hashCode and toString
- private methods
- public static methods
One convention need not fit all scenarios - typically, in our team, we use C# and we use "region" for grouping private fields, static members, private methods, constructors, protected methods and public methods. Order doesn't matter much because VS can collapse all regions nicely giving a summary view. Sometimes, we also use "overrides" and/or "virtual" regions. It also depends upon the complexity of the class in question. For few complex classes, you will even find regions based on functionality. For example, all "Parsing" stuff (variables, private methods, public methods involved in parsing) would be together under one region. In the end, goal is to have readable (maintainable) code and "consistency" would be one of the tool for that - as long as team understands this, there shouldn't be any issues.
精彩评论