Clean Coding
# Clean Coding
- Use methods
- Keeps your methods short
- Refactoring (changing the structure of the code without changing its behavior)
- Extract repetitive patterns
- Extract highly related statements
Before:
public static void main(String[] args) {
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
int principal = 0;
float monthlyInterest = 0;
int numberOfPayments = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Principal: ");
principal = scanner.nextInt();
if (principal >= 1000 && principal <= 1_000_000)
break;
System.out.println("Enter a value between 1000 and 1000000");
}
while (true) {
System.out.print("Annual Interest Rate: ");
float annualInterest = scanner.nextFloat();
if (annualInterest >= 1 && annualInterest <= 30) {
monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 and 30");
}
while (true) {
System.out.print("Period (Years): ");
byte years = scanner.nextByte();
if (years >= 1 && years <= 30) {
numberOfPayments = years * MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 and 30.");
}
double mortgage = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.println("Mortgage: " + mortgageFormatted);
}
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
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
After refactoring:
final static byte MONTHS_IN_YEAR = 12;
final static byte PERCENT = 100;
public static void main(String[] args) {
int principal = (int) readNumber("Principal: ", 1000, 1_000_000);
float annualInterest = (float) readNumber("Annual Interest Rate: ", 1, 30);
byte years = (byte) readNumber("Period (Years): ", 1, 30);
double mortgage = calculateMortgage(principal, annualInterest, years);
String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.println("Mortgage: " + mortgageFormatted);
}
public static double readNumber(String prompt, double min, double max) {
Scanner scanner = new Scanner(System.in);
double value;
while (true) {
System.out.print(prompt);
value = scanner.nextFloat();
if (value >= min && value <= max)
break;
System.out.println("Enter a value between " + min + " and " + max);
}
return value;
}
// all the logic of calculating mortage are in one place, graceful
public static double calculateMortgage(int principal, float annualInterest, byte years) {
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
float numberOfPayments = years * MONTHS_IN_YEAR;
double mortgage = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
return mortgage;
}
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
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
上次更新: 2022/07/21, 12:07:30