Closed ikokostya closed 6 years ago
Order
public props;
constructor;
public methods;
protected props;
protected methods;
private props;
private methods;
seems to work OK for us. However, we don't use short field declarations.
@dmikis Where is a place of private constructor and static properties?
Private constructor is between private props and methods, analogous to a public one.
Static props are always after non-static ones within access qualifier. I.e. public instance props, public static props, protected instance props, protected static props, etc.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
General
In this issue I want to discuss about fields order in a class declaration.
Public, private, and protected modifiers
Because most code users or readers are first interested in the public interface, not in the implementation details, make sense use the following order:
public
,protected
,private
. But there are some drawbacks:Private field declarations are placed at the end of a class, but constructor (where this fields are initialized) is placed at the beginning of a class. For big classes you need often scroll code up and down each time when you want to see variable type before initialization. In other words, distance between declaration and initialization of some field can be long, but we prefer keep it short:
TypeScript allow short field declaration:
instead
Or allow initialize variable with declaration:
In this cases fields initialization is happen in different places.
Static and non-static
Should
static
fields be placed before or after non-static fields?