Naming is hard but important
Naming is hard but important

Naming is hard but important

Tags
Technology
Description
The hardest thing in programming?
Published
Published August 19, 2022
The beginning of wisdom is to call things by their proper name - Confucius

Disambiguation

  • Page endpoint: Endpoint serving html HTTP response
  • API endpoint: Endpoint serving json/xml/html partial HTTP response
Β 
Directory structure similar to tab vs space is an "unsolvable problem". It always raises incessant debates that emphasize personal preferences while neglecting about what's the problem to solve. In general, there're 2 popular ways of doing this, i.e. grouping by file types (functionality) or grouping by features. Or if you embrace minimalism, a single index.php can be a reasonable choice as well.
Β 
In fact, what's more important might be how you decide to name stuff. Naming is hard, probably the hardest thing in software engineering. I get confused when seeing directory structure like this.
notion image
I have no question about storage or util since they fall under the β€œgrouping by file types” category, but everything under the web/** isn't as clear as above. There're 3 terms "web", "api" and "handler" each may have very different meaning under different context. In combining "web" with "handler", I got "web handler" (a.k.a "controllers") which is self explanatory. But how about "web api" (Web API??) and "web webapi"?
Β 
After working with the codebase for a while, I eventually figure out that all stuffs within the web folder are web handlers for different purposes. i.e. web/handler is for page endpoint, web/api for api endpoint, web/webapi for api endpoint only accessed by browser or mobile. It's clear that this isn't an idiomatic way of naming things and professionals can do better than that. But how could it be named like this? Obviously there're some background stories.
  • Then there's the pressure of getting things done. Everyone knows that business values velocity, and it's not a bad thing. Especially for tasks with little to none return of investment like migration, the rush to get it done and move into the next more interesting stuff is very tentative (I'm in the same group πŸ˜…). However, if we can zoom out a bit and evaluate the cost of time wasted to understand and maintain code full of arbitrary names, we might be willing to put a few extra minutes going through list like this and come up with more thoughtful names instead
  • Also, people tend to assume others understand their assumptions. It’s a common cognitive bias called curse of knowledge. E.g. in naming the folder as "web/webapi", there is an assumption that the API is browser/mobile facing is a shared consensus, while I can't see why the same thing can't be applied to "web/api" either. Simple changes like below will help clarify this a lot
    • β”œβ”€β”€ handlers β”œβ”€β”€ page β”œβ”€β”€ api β”‚Β Β  β”œβ”€β”€ protected
Β 
You might ask "what would be the idiomatic way of doing this then?" Let's turn to the community for some inspiration, since it's usually smart not to reinvent the wheel unless for "good" reason. Use two convention over configuration web frameworks as example.
The Rails (Ruby) skeleton
β”œβ”€β”€ Gemfile β”œβ”€β”€ Gemfile.lock β”œβ”€β”€ README.md β”œβ”€β”€ Rakefile β”œβ”€β”€ app β”œβ”€β”€ assets β”‚Β Β  β”œβ”€β”€ config β”‚Β Β  β”œβ”€β”€ images β”‚Β Β  └── stylesheets β”œβ”€β”€ channels β”‚Β Β  └── application_cable β”œβ”€β”€ controllers β”‚Β Β  β”œβ”€β”€ application_controller.rb β”‚Β Β  └── concerns β”œβ”€β”€ helpers β”‚Β Β  └── application_helper.rb β”œβ”€β”€ javascript β”‚Β Β  β”œβ”€β”€ channels β”‚Β Β  └── packs β”œβ”€β”€ jobs β”‚Β Β  └── application_job.rb β”œβ”€β”€ mailers β”‚Β Β  └── application_mailer.rb β”œβ”€β”€ models β”‚Β Β  β”œβ”€β”€ application_record.rb β”‚Β Β  └── concerns └── views └── layouts β”œβ”€β”€ babel.config.js β”œβ”€β”€ bin β”œβ”€β”€ config β”œβ”€β”€ config.ru β”œβ”€β”€ db β”œβ”€β”€ lib β”œβ”€β”€ log β”œβ”€β”€ node_modules β”œβ”€β”€ package.json β”œβ”€β”€ postcss.config.js β”œβ”€β”€ public β”œβ”€β”€ storage β”œβ”€β”€ test β”œβ”€β”€ tmp β”œβ”€β”€ vendor └── yarn.lock
The Phoenix (Elixir) skeleton
β”œβ”€β”€ _build β”œβ”€β”€ assets β”œβ”€β”€ config β”œβ”€β”€ deps β”œβ”€β”€ lib β”‚ β”œβ”€β”€ hello β”‚ β”œβ”€β”€ hello.ex β”‚ β”œβ”€β”€ hello_web β”œβ”€β”€ controllers β”‚Β Β  └── page_controller.ex β”œβ”€β”€ templates β”‚Β Β  β”œβ”€β”€ layout β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.html.heex β”‚Β Β  β”‚Β Β  β”œβ”€β”€ live.html.heex β”‚Β Β  β”‚Β Β  └── root.html.heex β”‚Β Β  └── page β”‚Β Β  └── index.html.heex β”œβ”€β”€ views β”‚Β Β  β”œβ”€β”€ error_helpers.ex β”‚Β Β  β”œβ”€β”€ error_view.ex β”‚Β Β  β”œβ”€β”€ layout_view.ex β”‚Β Β  └── page_view.ex β”œβ”€β”€ endpoint.ex β”œβ”€β”€ gettext.ex β”œβ”€β”€ router.ex └── telemetry.ex β”‚ └── hello_web.ex β”œβ”€β”€ priv └── test
Not to say they are the best practice to follow, but generic terms like "web" and "api" are either avoided or used with caution, plus we can always fallback to google for more detailed explanations thanks to their popularity.