What is your preferred order of elements in the program code text? What for?

Of course, at the top of the .java or .cs file there is a package declaration / namespace followed by the import / using statement.

But what kills me, where should I put a variable declaration? At the top or bottom?

This is how I prefer:

package/namespace declaration
import/using declaration of microsoft's/java namespaces
import/using declaration of standard 3rd party namespaces
import/using declaration of my personal namespaces

constructors
private methods, each preceded by any private variable that was first used.
protected methods, each preceded by any private variable that was first used.
public methods, each preceded by any private variable that was first used.

inner/nested classes and enums

private anon classes assignment
protected variables/anon classes assignment
public variables/assigned anon classes assignment

But I understand that many people like to add all the variables right at the top after the package / namespace and import / use declarations. This is troublesome, because sometimes the number of variables and assigned anon classes gets too many, which hides the visibility of the constructors - the first lines I need to see are the constructors because they tell me the signature of the instance.

anon, - " ", .

, .

+3
2

Java , . , . , , . # ( ),

using statements
namespace
private static fields
private static methods/properties
public static methods/properties
private fields
private methods/properties
protected methods/properties
public methods/properties

, , , ( , ). , .

, ( )

+1

, . , using, , , , , , . , , -, , , , . ; , , , .

, , . , #region. , , , , -, , , .

, , , , , . , "" , , , , , .

+1

Source: https://habr.com/ru/post/1755589/


All Articles