Conditions | 14 |
Total Lines | 66 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like school_api.client.api.place_schedule.PlaceSchedule.get_schedule() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # -*- coding: utf-8 -*- |
||
19 | def get_schedule(self, |
||
20 | campus_list=None, |
||
21 | building_list=None, |
||
22 | classroom_type_list=None, |
||
23 | classroom_name_list=None, |
||
24 | filter_campus_list=None, |
||
25 | filter_building_list=None, |
||
26 | filter_classroom_type_list=None, |
||
27 | **kwargs): |
||
28 | ''' |
||
29 | 课表信息 获取入口 |
||
30 | 生成器一旦报错则将退出:https://codeday.me/bug/20180125/124136.html |
||
31 | 除了解析异常其他报错均不抛出 |
||
32 | |||
33 | :param campus_list: 校区列表 |
||
34 | :param building_list: 楼号列表 |
||
35 | :param classroom_type_list: 教室类别列表 |
||
36 | :param classroom_name_list: 教室名称列表 |
||
37 | |||
38 | :param filter_campus_list: 排除校区列表 |
||
39 | :param filter_building_list: 排除楼号列表 |
||
40 | :param filter_classroom_type_list: 排除教室类别列表 |
||
41 | :param kwargs: requests 参数 |
||
42 | :yield: 生成器 |
||
43 | ''' |
||
44 | |||
45 | self.schedule_url = self.school_url['PLACE_SCHEDULE_URL'] + \ |
||
46 | urlparse.quote(self.user.account.encode('gb2312')) |
||
47 | |||
48 | if not self._update_payload(**kwargs): |
||
49 | yield {'error': "获取教学场地课表失败"} |
||
50 | else: |
||
51 | # 遍历校区 |
||
52 | for campus in self.payload['campus_list']: |
||
53 | if self._is_skip(campus["name"], campus_list, filter_name_list=filter_campus_list): |
||
54 | continue |
||
55 | if not self._update_payload(campus, **kwargs): |
||
56 | continue |
||
57 | |||
58 | # 遍历楼号 |
||
59 | for building in self.payload['building_list']: |
||
60 | kwargs['building'] = building |
||
61 | if self._is_skip(building["name"], building_list, filter_name_list=filter_building_list): |
||
62 | continue |
||
63 | if not self._update_payload(campus, **kwargs): |
||
64 | continue |
||
65 | |||
66 | # 遍历教室类别 |
||
67 | for classroom_type in self.payload['classroom_type_list']: |
||
68 | kwargs['classroom_type'] = classroom_type |
||
69 | if self._is_skip(classroom_type["name"], classroom_type_list, |
||
70 | filter_name_list=filter_classroom_type_list): |
||
71 | continue |
||
72 | if not self._update_payload(campus, **kwargs): |
||
73 | continue |
||
74 | |||
75 | # 遍历教室名称 |
||
76 | for classroom_name in self.payload['classroom_name_list']: |
||
77 | if self._is_skip(classroom_name["name"], classroom_name_list): |
||
78 | continue |
||
79 | |||
80 | kwargs['classroom_name'] = classroom_name |
||
81 | # 请求接口获取课表数据 |
||
82 | data = self._get_result(campus, **kwargs) |
||
83 | if data: |
||
84 | yield data |
||
85 | |||
186 |