Očistite smeće
TL;DR: Eliminirajte neiskorištene funkcije, konstante i kod "za svaki slučaj".
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.
Možete izvesti male korake i ukloniti nepotrebni kod u iteracijama.
Ovo refaktoriranje je sigurno ako temeljito testirate svoju aplikaciju nakon promjena. Alati za statičku analizu mogu osigurati da ne uklonite ništa što je još u upotrebi.
Poboljšavate jasnoću i smanjujete složenost uklanjanjem neiskorištenih elemenata.
Vaš kod postaje lakši za razumijevanje i održavanje.
Smanjenje spekulativnog koda također zadržava vaš fokus na trenutnim, stvarnim zahtjevima.
Mrtvi kod i spekulativni elementi prekidaju Bijection između vašeg softvera i modela stvarnog svijeta.
Uklanjanje ovih elemenata osigurava da vaš kod točno predstavlja vaš MAPPER , čineći ga čišćim i bližim stvarnosti.
Uklanjanje mrtvog koda zahtijeva uvjerenje da se doista ne koristi.
Ovaj se proces oslanja na statičku analizu ili temeljito poznavanje baze koda, što može biti sklono pogreškama bez robusnih alata.
Bez odgovarajućih uputa | S posebnim uputama |
---|---|
Ovaj je članak dio serije Refactoring.