现在看我之前写的一些代码啊,现在自己过来看我都觉得恶心,来给各位看官瞧一瞧,啥是烂代码,之前为了实现个自动化回复工单的程序,我就洋洋洒洒写了近1000行代码,其中有一个方法是用来检查是否有新工单的。他大概是这样的,判断工单是否有更新的情况下拿到标题去判断用户的问题,然后回复客户,老实讲这个程序当时就是随便写写的,写完跑一下,唉,可以用,于是就搞个服务器7*24小时挂着跑。一开始只是想着匹配几个标题去判断,就直接把要回复的内容都写代码里面了。后来加入的东西越来越多,我的妈呀,每次改个代码,看着这一堆中文夹在程序中,我就感到无比的恶心。而且,这个 if 语句判断也写的有问题。
defcheck_ticket_new(self): try: tickets_id = self.views_new_tickets()[0] organization_id = self.views_new_tickets()[1] if tickets_id isnotNone: userlist = self.users_admin_list() foridin tickets_id: comments = self.list_comments(id) auth_name = comments[2] author_id = comments[0] ticket_info = self.ticketid(id)[0] content = comments[-2] check_quota_value = self.check_quota(auth_name) self.kf5_info("check_quota {}".format(str(check_quota_value))) if author_id in userlist: pass else: for organization_id in organization_id:
# 读取回复内容 def read_json_config(self): with open('/opt/kf5/robot.json', 'r') as f: data = json.load(f) status = [(d['status']) for d in data['reply']] source = [(d['source']) for d in data['reply']] rule = [(d['rule']) for d in data['reply']] reply_status = [(d['reply_status']) for d in data['reply']] msg = [(d['msg']) for d in data['reply']] return [status,source,rule,reply_status,msg]
然后重写 check_ticket_new 方法,大概是下面这个样子。对比下看看,是不是好看多了。
# 判断工单是否是客服回复以及是否超过规定时间, SLA 5分钟响应,4小时客户未回复要发送消息通知用户回复工单,24小时客户未回复关闭工单! # 判断新工单是否有人回复,如果没有受理则自动回复用户一条信息,确保 SLA 不超时 defcheck_ticket_new(self): tickets_id = self.views_new_tickets()[0] organization_id = self.views_new_tickets()[1] read_config = self.read_json_config() status = read_config[0] source = read_config[1] rule = read_config[2] reply_status = read_config[3] msg = read_config[4] if tickets_id isNoneand organization_id isNone: return userlist = self.users_admin_list() if userlist isNone: return foridin tickets_id: comments = self.list_comments(id) auth_name = comments[2] author_id = comments[0] ticket_info = self.ticketid(id)[0] content = comments[-2] check_quota_value = self.check_quota(auth_name) if comments and auth_name and author_id and ticket_info and content isNone: return if author_id in userlist: return for i inrange(len(organization_id)): organization_id = organization_id[i] print(organization_id) print(ticket_info) if ticket_info == "确认退余额": pass if organization_id isNone: organization_id = "NOAUTH" if check_quota_value == "LAJI"and re.match(r'.{0,}(配额)', ticket_info): check_quota_value = "LAJI" for info inrange(len(status)): print(info) if check_quota_value == rule[info]: self.put_new_comments(id, reply_status[info],msg[info]) return if re.search(rule[info],ticket_info): self.put_new_comments(id, reply_status[info],msg[info]) return if organization_id == rule[info]: self.put_new_comments(id, reply_status[info], msg[info]) return self.put_new_comments(id, "open", "您好,您的问题已收到,我们正在处理中!")
写代码其实有很多套路的。比如
变量名:要么驼峰式命名,类似这样 agentStatus,要么下划线 agent_status
变量名要有意义,尽量使用英文。
要有注释。
如果层级特别多,把他提炼出一个函数或方法
一个方法或函数只做一件事。
多去 github 读一读优秀的代码。
避免写多层嵌套,例如 下面这段
代码
def do_offline(self,headers=headers):
online_offline_url = "https://yun163.kf5.com/apiv2/kchat/agent/availabilities.json" chats_url = "https://yun163.kf5.com/apiv2/kchat/monitor/chats.json" querystring = {"agent_id": "2775056", "status": "offline"} while 1: chats_response = requests.request("GET", chats_url, headers=headers) if chats_response.status_code == 200: ch_json = chats_response.json() if len(ch_json['chats']) == 0: offline_time = str(time.strftime("%H%M", time.localtime())) print(offline_time) offline_area = [1130, 1730, 2000] if int(offline_time) in offline_area: offline_response = requests.request("PUT", online_offline_url, headers=headers,params=querystring) if offline_response.status_code == 200: offline_json = offline_response.json() offline_name = offline_json['agent']['name'] if offline_json['agent']['webStatus'] == "offline" and offline_json['agent']['appStatus'] == "offline": self.send_message_wchat("通知",offline_name + " Web 端和 APP 端都已下线") if offline_json['agent']['webStatus'] == "offline" and offline_json['agent']['appStatus'] != "offline": self.send_message_wchat("通知",offline_name + " Web 端已下线但是 APP 端未下线,请在手机 APP 操作") time.sleep(10)