Python で学ぶリファクタリング (基本編)

# 関数の抽出 (Extract Function)

```before.py
import math

def main():
    radius = int(input())
    circumference = math.pi * radius * 2
    print(circumference)

if __name__ == '__main__':
    main()
```

```after.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

# 関数のインライン化 (Inline Function)

```before.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def print_circumference(circumference):
    print(circumference)

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print_circumference(circumference)

if __name__ == '__main__':
    main()
```

```after.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

# 変数の抽出 (Extract Variable)

```before.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    print(get_circumference(int(input())))

if __name__ == '__main__':
    main()
```

```after.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

# 変数のインライン化 (Inline Variable)

```before.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius_str = input()
    radius_int = int(radius_str)
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

```after.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

# 関数宣言の変更 (Change Function Declaration)

```before.py
import math

def crcm(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = cr(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

```after.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

# 変数のカプセル化 (Encapsulate Variable)

```before.py
import math

def get_diameter(radius):
    return radius * 2

def get_circumference(radius):
    return math.pi * radius * 2

def get_area(radius):
    return math.pi * radius * radius

def main():
    radius = int(input())

    diameter = get_diameter(radius)
    circumference = get_circumference(radius)
    area = get_area(radius)

    print(radius)
    print(diameter)
    print(circumference)
    print(area)

if __name__ == '__main__':
    main()
```

```after.py
import math

class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

def main():
    radius = int(input())

    circle = Circle(radius)

    print(circle.get_radius())
    print(circle.get_diameter())
    print(circle.get_circumference())
    print(circle.get_area())

if __name__ == '__main__':
    main()
```

# 変数名の変更 (Rename Variable)

```before.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    hoge = input()
    piyo = get_circumference(hoge)
    print(piyo)

if __name__ == '__main__':
    main()
```

```after.py
import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = input()
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()
```

# パラメータオブジェクトの導入 (Introduce Parameter Object)

```before.py
import math

def print_param(radius, diameter, circumference, area):
    print('radius: {}'.format(radius))
    print('diameter: {}'.format(diameter))
    print('circumference: {}'.format(circumference))
    print('area: {}'.format(area))

def main():
    radius = int(input())
    diameter = radius * 2
    circumference = math.pi * radius * 2
    area = math.pi * radius * radius

    print_param(radius, diameter, circumference, area)

if __name__ == '__main__':
    main()
```

```after.py
import math

class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

def print_param(circle):
    radius = circle.get_radius()
    diameter = circle.get_diameter()
    circumference = circle.get_circumference()
    area = circle.get_area()

    print('radius: {}'.format(radius))
    print('diameter: {}'.format(diameter))
    print('circumference: {}'.format(circumference))
    print('area: {}'.format(area))

def main():
    radius = int(input())
    circle = Circle(radius)
    print_param(circle)

if __name__ == '__main__':
    main()
```

# 関数群のクラスへの集約 (Combine Functions into Class)

```before.py
import math

class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

def print_param(circle):
    radius = circle.get_radius()
    diameter = circle.get_diameter()
    circumference = circle.get_circumference()
    area = circle.get_area()

    print('radius: {}'.format(radius))
    print('diameter: {}'.format(diameter))
    print('circumference: {}'.format(circumference))
    print('area: {}'.format(area))

def main():
    radius = int(input())
    circle = Circle(radius)
    print_param(circle)

if __name__ == '__main__':
    main()
```

```after.py
class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

    def print_param(self):
        print('radius: {}'.format(self, radius))
        print('diameter: {}'.format(self, diameter))
        print('circumference: {}'.format(self, circumference))
        print('area: {}'.format(self, area))

def main():
    radius = int(input())
    circle = Circle(radius)
    circle.print_param()

if __name__ == '__main__':
    main()
```

# 関数群の変換への集約 (Combine Functions into Transform)

N/A

# フェーズの分離 (Split Phase)

N/A