Performance: Route naming conventions
This affects how quickly the server can match incoming URLs to route handlers, impacting response time and user experience.
from flask import Flask app = Flask(__name__) @app.route('/user/profile') def user_profile(): return 'User Profile' @app.route('/user/profile/edit') def edit_user_profile(): return 'Edit Profile'
from flask import Flask app = Flask(__name__) @app.route('/userprofile') def user_profile(): return 'User Profile' @app.route('/userprofile/edit') def edit_user_profile(): return 'Edit Profile'
| Pattern | Route Matching Checks | Response Delay | Code Clarity | Verdict |
|---|---|---|---|---|
| Inconsistent route names | Many checks due to ambiguity | Higher delay | Low clarity | [X] Bad |
| Consistent hierarchical routes | Minimal checks | Lower delay | High clarity | [OK] Good |
/user-profile uses lowercase letters and hyphens, which is the recommended style. Options B and D use uppercase letters, which is not standard. /user_profile uses underscores, which is less preferred than hyphens in URLs.<int:id> to define dynamic parts.<int:id>. @app.route('/user/{id}') uses curly braces which is invalid in Flask. @app.route('/user/:id') uses colon syntax from other frameworks, not Flask. @app.route('/user/') reverses type and variable name, which is incorrect./product/42 given this Flask code?@app.route('/product/')
def show_product(product_id):
return f"Product ID: {product_id}"/product/<int:product_id> captures the integer 42 and passes it as product_id to the function.@app.route('/order/')
def order(order_id):
return f"Order {order_id}"<int:order_id> and the function parameter is order_id, which matches correctly./blog-post/ is preferred over underscores or camelCase.<string:slug> to specify a string variable. @app.route('/blog-post/') uses both correct route naming and dynamic segment syntax.Pass the test and this pattern moves unknown