在循环中,一次循环做了两件事情,将循环分解,重复这个循环,每次只做一件事情
潜在问题:性能问题,如果遇到性能问题,先让代码清晰可读,让你更快找到性能优化点,再做优化
本次重构涉及的基本重构较多,主要有:
Student类是一个哑对象,表示一个学生,有年龄(age)和成绩(grade)两个属性
package split.loop; public class Student { /** * 年龄 */ private int age; /** * 成绩 */ private int grade; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { double averageAge = 0; // 平均年龄 int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { averageAge += students[i].getAge(); totalGrade += students[i].getGrade(); } averageAge = averageAge / students.length; System.out.println(averageAge); System.out.println(totalGrade); } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { double averageAge = 0; // 平均年龄 int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { averageAge += students[i].getAge(); } for (int i = 0; i < students.length; i++) { totalGrade += students[i].getGrade(); } averageAge = averageAge / students.length; System.out.println(averageAge); System.out.println(totalGrade); } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { double averageAge = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { averageAge += students[i].getAge(); } averageAge = averageAge / students.length; int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { totalGrade += students[i].getGrade(); } System.out.println(averageAge); System.out.println(totalGrade); } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { double averageAge = averageAge(); int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { totalGrade += students[i].getGrade(); } System.out.println(averageAge); System.out.println(totalGrade); } private double averageAge() { double averageAge = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { averageAge += students[i].getAge(); } averageAge = averageAge / students.length; return averageAge; } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { double averageAge = averageAge(); int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { totalGrade += students[i].getGrade(); } System.out.println(averageAge); System.out.println(totalGrade); } private double averageAge() { double result = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { result += students[i].getAge(); } result = result / students.length; return result; } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { double averageAge = averageAge(); int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { totalGrade += students[i].getGrade(); } System.out.println(averageAge); System.out.println(totalGrade); } private double averageAge() { double result = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { result += students[i].getAge(); } double result1 = result / students.length; return result1; } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { double averageAge = averageAge(); int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { totalGrade += students[i].getGrade(); } System.out.println(averageAge); System.out.println(totalGrade); } private double averageAge() { double result = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { result += students[i].getAge(); } return result / students.length; } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { int totalGrade = 0; // 总成绩 for (int i = 0; i < students.length; i++) { totalGrade += students[i].getGrade(); } System.out.println(averageAge()); System.out.println(totalGrade); } private double averageAge() { double result = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { result += students[i].getAge(); } return result / students.length; } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { int totalGrade = totalGrade(); System.out.println(averageAge()); System.out.println(totalGrade); } private double averageAge() { double result = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { result += students[i].getAge(); } return result / students.length; } private int totalGrade() { int result = 0; // 总成绩 for (int i = 0; i < students.length; i++) { result += students[i].getGrade(); } return result; } }
package split.loop; public class SomeClass { private Student[] students; void printValues() { System.out.println(averageAge()); System.out.println(totalGrade()); } private double averageAge() { double result = 0; // 平均年龄 for (int i = 0; i < students.length; i++) { result += students[i].getAge(); } return result / students.length; } private int totalGrade() { int result = 0; // 总成绩 for (int i = 0; i < students.length; i++) { result += students[i].getGrade(); } return result; } }
本重构虽然很简单,一些很小的改动,能够使代码可读性更强,更易维护,希望本实践对你有益,希望大家和我一起动手做重构,也希望你提出宝贵意见或反馈