Conditions | 12 |
Total Lines | 55 |
Code Lines | 37 |
Lines | 55 |
Ratio | 100 % |
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 build.cogs.spotcog.SpotCog.count_spot() 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.
1 | """ |
||
67 | async def count_spot(self, ctx: SlashContext, monster_type: int): |
||
68 | """ |
||
69 | |||
70 | :param ctx: |
||
71 | :type ctx: |
||
72 | :param monster_type: |
||
73 | :type monster_type: |
||
74 | :return: |
||
75 | :rtype: |
||
76 | """ |
||
77 | with open("./json_files/monster_spots.json", encoding="utf-8") as f: |
||
78 | try: |
||
79 | spots = json.load(f) |
||
80 | except ValueError: |
||
81 | spots = {"users": []} |
||
82 | await ctx.channel.send("monster_spots.json created", delete_after=1) |
||
83 | |||
84 | if monster_type == 4: |
||
85 | for current_user in spots["users"]: |
||
86 | if current_user["id"] == ctx.author.id: |
||
87 | current_user["common_spots"] += 1 |
||
88 | break |
||
89 | spots["users"].append({ |
||
90 | "id": ctx.author.id, |
||
91 | "name": ctx.author.name, |
||
92 | "lege_spots": 0, |
||
93 | "rare_spots": 0, |
||
94 | "total": 0, |
||
95 | "common_spots": 1 |
||
96 | }) |
||
97 | |||
98 | else: |
||
99 | for current_user in spots["users"]: |
||
100 | if current_user["id"] == ctx.author.id: |
||
101 | if monster_type == 0: |
||
102 | current_user["rare_spots"] += 1 |
||
103 | elif monster_type == 1: |
||
104 | current_user["lege_spots"] += 1 |
||
105 | else: |
||
106 | print("Wrong monster type(?)") |
||
107 | |||
108 | current_user["total"] = current_user["lege_spots"] * 5 + current_user["rare_spots"] |
||
109 | break |
||
110 | else: |
||
111 | spots["users"].append({ |
||
112 | "id": ctx.author.id, |
||
113 | "name": ctx.author.display_name, |
||
114 | "lege_spots": 0, |
||
115 | "rare_spots": 0, |
||
116 | "total": 0, |
||
117 | "common_spots": 0 |
||
118 | }) |
||
119 | |||
120 | with open("./json_files/monster_spots.json", "w+") as f: |
||
121 | json.dump(spots, f, indent=4) |
||
122 | |||
177 |