重构 - 删除死代码
清理垃圾
TL;DR:消除未使用的函数、常量和“以防万一”代码。
解决的问题
死代码
以防万一代码
可维护性降低
锚船
认知负荷
相关代码异味

代码气味 09 - 死代码
马克西·孔蒂耶里 ・2020 年 10 月 28 日

代码气味 54 - 锚船
马克西·孔蒂耶里 ・21 年 1 月 6 日

代码气味 148 - 待办事项
马克西·孔蒂耶里 ・ 22 年 7 月 13 日
步骤
确保您的代码具有良好的功能覆盖率。
通过检查代码或使用静态分析工具来识别未使用的函数和常量。
分析添加的推测代码,以防万一。
删除任何不必要或未使用的内容。
对您的代码执行全面的回归测试。
示例代码
前
from flask import Flask, jsonify, make_response app = Flask(__name__) HTTP_100_CONTINUE = 100 HTTP_202_ACCEPTED = 202 # Not used HTTP_204_NO_CONTENT = 204 # Not Used HTTP_302_FOUND = 302 # Not Used HTTP_400_BAD_REQUEST = 400 # Not Used HTTP_401_UNAUTHORIZED = 401 # Not Used HTTP_403_FORBIDDEN = 403 HTTP_404_NOT_FOUND = 404 HTTP_410_GONE = 410 HTTP_500_INTERNAL_SERVER_ERROR = 500 HTTP_501_NOT_IMPLEMENTED = 501 probe_telemetry = { "temperature": {"solar_panels": 150, "instrument_1": 50}, "position": {"x": 1000000, "y": 2000000, "z": 3000000, "velocity": {"vx": 100, "vy": 200, "vz": 300}}, "status": {"power_level": 95, "communication_status": "OK"} } @app.route('/api/v1/probe/telemetry', methods=['GET']) def get_telemetry(): return jsonify(probe_telemetry), HTTP_200_OK # The following function is not invoked # and not implemented # It is a dead placeholder @app.route('/api/v1/probe/send_command', methods=['POST']) def send_command(): return jsonify({"message": "Command endpoint not implemented yet."}), HTTP_501_NOT_IMPLEMENTED @app.route('/api/v1/probe/data', methods=['GET']) def get_data(): return jsonify({"message": "Data not found"}), HTTP_404_NOT_FOUND @app.route('/api/v1/probe/redirect', methods=['GET']) def redirect_endpoint(): response = make_response(jsonify({"message": "Redirecting..."}), HTTP_301_MOVED_PERMANENTLY) response.headers['Location'] = '/api/v1/probe/telemetry' return response @app.route('/api/v1/probe/not_modified', methods=['GET']) def not_modified_endpoint(): response = make_response(jsonify({"message": "Not Modified"}), HTTP_304_NOT_MODIFIED) response.headers['ETag'] = 'some_etag' return response @app.route('/api/v1/probe/gone', methods=['GET']) def gone_endpoint(): return jsonify({"message": "Resource permanently gone"}), HTTP_410_GONE
后
# 1. Ensure your code has good functional coverage. from flask import Flask, jsonify, make_response from http import HTTPStatus app = Flask(__name__) # 2. Identify unused functions and constants # by reviewing your code or using static analysis tools. HTTP_200_OK = HTTPStatus.OK HTTP_301_MOVED_PERMANENTLY = HTTPStatus.MOVED_PERMANENTLY HTTP_304_NOT_MODIFIED = HTTPStatus.NOT_MODIFIED HTTP_404_NOT_FOUND = HTTPStatus.NOT_FOUND HTTP_410_GONE = HTTPStatus.GONE HTTP_501_NOT_IMPLEMENTED = HTTPStatus.NOT_IMPLEMENTED probe_telemetry = { "temperature": {"solar_panels": 150, "instrument_1": 50}, "position": {"x": 1000000, "y": 2000000, "z": 3000000, "velocity": {"vx": 100, "vy": 200, "vz": 300}}, "status": {"power_level": 95, "communication_status": "OK"} } @app.route('/api/v1/probe/telemetry', methods=['GET']) def get_telemetry(): return jsonify(probe_telemetry), HTTP_200_OK # 3. Analyze the added speculative code, just in case. @app.route('/api/v1/probe/send_command', methods=['POST']) def send_command(): return jsonify({"message": "Command endpoint not implemented yet."}), HTTP_501_NOT_IMPLEMENTED @app.route('/api/v1/probe/data', methods=['GET']) def get_data(): return jsonify({"message": "Data not found"}), HTTP_404_NOT_FOUND # 4. Remove anything unnecessary or unused. # 5. Perform comprehensive regression testing on your code.
类型
[X] 半自动
安全
如果您在更改后彻底测试您的应用程序,则此重构是安全的。静态分析工具可以帮助确保您不会删除仍在使用的任何内容。
为什么代码更好?
通过删除未使用的元素可以提高清晰度并降低复杂性。
您的代码变得更容易理解和维护。
减少推测性代码还可以让您专注于当前的实际需求。
它如何改善双射?
死代码和推测元素会破坏软件和现实世界模型之间的双射。
删除这些元素可确保您的代码准确地代表您的
MAPPER,使其更干净、更接近现实。
局限性
删除死代码需要确信它确实未被使用。
此过程依赖于静态分析或全面的代码库知识,如果没有强大的工具,这可能很容易出错。
用人工智能重构
Without Proper Instructions | With Specific Instructions |
---|---|
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
Gemini | Gemini |
标签
- 浮肿
相关重构

重构003 - 提取常量
马克西·孔蒂耶里 ・22 年 1 月 2 日
制作人员
该图片由 Peter H 在 Pixabay上
本文是重构系列的一部分。

如何通过简单的重构来改进代码
马克西·孔蒂耶里 ・2022 年 10 月 24 日
以上是重构 - 删除死代码的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...
