清理垃圾
TL;DR:消除未使用的函数、常量和“以防万一”代码。
死代码
以防万一代码
可维护性降低
锚船
认知负荷
确保您的代码具有良好的功能覆盖率。
通过检查代码或使用静态分析工具来识别未使用的函数和常量。
分析添加的推测代码,以防万一。
删除任何不必要或未使用的内容。
对您的代码执行全面的回归测试。
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 |
该图片由 Peter H 在 Pixabay上
本文是重构系列的一部分。
以上是重构 - 删除死代码的详细内容。更多信息请关注PHP中文网其他相关文章!