清理垃圾
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中文網其他相關文章!