Hacker News new | ask | show | jobs
by gioi 4385 days ago
Am I the only one who thinks that routes should be defined near views, just like default Flask does?
4 comments

You're not the only one; this is also my experience. However, I'm prepared to believe others' claims that there are drawbacks involved with doing this on "large" apps. I wonder whether they mostly value this "all routes in one place" technique for the purpose of documentation, and perhaps a simple Flask helper function that printed out all registered routes would be just as useful for them?
It already exists. app.url_map contains the mappings from paths to endpoints.

Personally I treat the flask web as a really thin layer around my other more complex libraries (the core of the application). I use blueprints to group things together but I still try to make sure each of those is very simple, say - routing, data marshalling and session management - but anything more complex lives somewhere else.

Each function inside your views just gets a little data together and proxies it down to the real application.

As you say, maybe it gets more complex as the app grows in size, but to me that sounds like the app is trying to do too much. Over time I've learnt that thinking of your app as a 'flask application' (django, rails, whatever) means that it will grow in a way that's hard to maintain later.

Nope, same here! I personally prefer the routes being near the views as they belong together and most times I can't be bothered looking the route up in a different file like in Django or Rails even. But I guess it all comes down to personal preferences.
For small apps, urls should definitely be defined near views. For larger apps, I would have to disagree. Here's why: Django's url routing system might be cumbersome when you just want to get going with a few routes. However, it acts as an "index" for all your app's urls when you've got tens (or hundreds) of route definitions. So, instead of opening every view file, you can "tree search" for a specific url path. It is very flexible (which can be a plus and a minus).
What is the rationale? It isn't obvious to me that it's better either way.