Split Loop - 分开循环

Split Loop -- 分离循环Split LoopRefactoring contributed by Martin FowlerYou have a loop that is doi

Split Loop -- 分离循环

Split Loop

Refactoring contributed by Martin Fowler

You have a loop that is doing two things

一个循环做了两件事情。

Duplicate the loop

复制循环。

Split Loop - 分开循环

void printValues() {System.out.println(averageAge());System.out.println(totalSalary());}private double averageAge() {double result = 0;for (int i = 0; i < people.length; i++) {result += people[i].age;}return result / people.length;}private double totalSalary() {double result = 0;for (int i = 0; i < people.length; i++) {result += people[i].salary;}return result;}