-
Notifications
You must be signed in to change notification settings - Fork 2
/
code_style_example.java
108 lines (82 loc) · 1.96 KB
/
code_style_example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// package at the top
/*
varius imports
*/
// funcion description
// classes are singular nouns
// function name is the same as file name
// one Class per file, with the exception of nested classes
class ClassName
{
// space after comment
/*
TODO: multi line get indented
*/
// singleton at the top
private static instance = new ClassName();
public static getInstance() { return instance; }
// variables at the top
// variables have nouns
int variableName;
// variable description on top, or at the side of variable
// but try to make it clear what variable does without using comments
int variableName2;
bool isAlive;
bool hasFinished;
// use wrappers inside collections
List<Integer> pluralNoun;
/*
FUNCIONS ORDER:
1. getInstance()
2. constructors
3. framework methods (@FXMl initialize(), onKeyPressed(), etc... )
4. public methods
5. private methods (if closly related can be close to related public methods)
6. getter & setters
*/
// constructor first
public ClassName()
{
// var used only when the type is clear and is seen on the same line
var a = new Integer(5);
// borderline acceptable when function makes it clear what type it's returning
var b = Factory.buildNewPerson();
// specify in all other cases
Person c = Database.get(0);
}
// function description
// functions have verbs
public int functionName()
{
// tabs for indentation
if (condition1)
{
// ...
}
else if ()
{
// ...
}
else
{
// ...
}
// outer if must have brakets
if (outerCondition && otherCondition)
{
if (nested_condition)
singleIfInstruction
else
singleElseInstruction
}
String title = switch (person)
{
case Dali, Picasso -> "painter";
case Mozart, Prokofiev -> "composer";
case Goethe, Dostoevsky -> "writer";
};
}
// getter and setters at the end of file
void setVar(int param) { variableName = param; }
int getVar() { return variableName; }
}