Skip to content

Instantly share code, notes, and snippets.

@ankschoubey
Created December 20, 2019 03:35
Show Gist options
  • Save ankschoubey/c9785400ce2c1aee8271485b15dafd6d to your computer and use it in GitHub Desktop.
Save ankschoubey/c9785400ce2c1aee8271485b15dafd6d to your computer and use it in GitHub Desktop.
Tips on removing nesting
import java.util.Arrays;
import java.util.List;
//PART OF Clean Code series //https://medium.com/@ankushchoubey/series-tips-on-writing-clean-code-30d717f32ae4
class Student { // POJO 🤘
String name;
List<Integer> grades;
// skipping getter/setter:
// tip: google "project lombok"
}
class BadGrader { // Line of Code: 9, Indentation: 2
void printAllAverageGrades(List<Student> students) {
for (Student student : students) {
int count = 0; // the logic of calculating grade is embedded within the loop
float sum = 0;
for (Integer grade : student.grades) {
sum += grade;
count+=1;
}
System.out.println("Name: " + student.name + "\Avg Grade:" + sum / count);
}
}
}
class BetterGrader { // Line of Code: 10, Indentation: 2
float calculateAverageGrade(List<Integer> grades) {
// now we can use our same grading logic else where.
int count = 0;
float sum = 0;
for (Integer grade : grades) {
sum += grade;
count+=1;
}
return sum / count;
}
void printAllAverageGrades(List<Student> students) {
for (Student student : students) {
System.out.println("Name: " + student.name + "\tAvg Grade:" + calculateAverageGrade(student.grades));
}
}
}
class GoodGrader {//Line of code 2:
double calculateAverageGrade(List<Integer> grades) {
// Using streams
// https://stackoverflow.com/questions/48906552/using-java-stream-to-get-a-sum-average-and-sort#48906784
return grades.stream().mapToInt(Integer::intValue).average().getAsDouble();
}
void printAllAverageGrades(List<Student> students) {
students.forEach(student -> System.out.println("Name: " + student.name + "\tAvg Grade:" + calculateAverageGrade(student.grades));
}
}
class GoodShorterGrader {//Line of code 2:
double getAvgGrade(List<Integer> grades) {
// Using streams
// https://stackoverflow.com/questions/48906552/using-java-stream-to-get-a-sum-average-and-sort#48906784
return grades.stream().mapToInt(Integer::intValue).average().getAsDouble();
}
void printAllAverageGrades(List<Student> students) {
students.forEach(student -> System.out.println("Name: " + student.name + "\tAvg Grade:" + getAvgGrade(student.grades));
}
}
class CleanNests {
public static void main(String[] args) {
Student a = new Student();
Student b = new Student();
a.name = "A";
a.grades = Arrays.asList(1, 2, 3, 4);
b.name = "B";
b.grades = Arrays.asList(6, 7, 4, 4);
List<Student> students = Arrays.asList(a, b);
System.out.println("BadGrader");
BadGrader bg = new BadGrader();
bg.printAllAverageGrades(students);
System.out.println("BetterGrader");
BetterGrader beg = new BetterGrader();
beg.printAllAverageGrades(students);
System.out.println("GoodGrader");
GoodGrader gg = new GoodGrader();
gg.printAllAverageGrades(students);
System.out.println("GoodShorterGrader");
GoodShorterGrader gsg = new GoodShorterGrader();
gsg.printAllAverageGrades(students);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment