Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyphen, underscore, or camelCase as word delimiter in URIs?

I'm designing an HTTP-based API for an intranet app. I realize it's a pretty small concern in the grand scheme of things, but: should I use hyphens, underscores, or camelCase to delimit words in the URIs?


Here are my initial thoughts:

camelCase

  • possible issues if server is case-insensitive
  • seems to have fairly widespread use in query string keys (http://api.example.com?**searchQuery**=...), but not in other URI parts

Hyphen

  • more aesthetically pleasing than the other alternatives
  • seems to be widely used in the path portion of the URI
  • never seen hyphenated query string key in the wild
  • possibly better for SEO (this may be a myth)

Underscore

  • potentially easier for programming languages to handle
  • several popular APIs (Facebook, Netflix, StackExchange, etc.) are using underscores in all parts of the URI.

I'm leaning towards underscores for everything. The fact that most of the big players are using them is compelling (see https://stackoverflow.com/a/608458/360570).

like image 812
Josh Avatar asked Apr 24 '12 16:04

Josh


People also ask

Should I use camelCase in URL?

Coming from a programming background, camelCase is a popular choice for naming joint words. But RFC 3986 defines URLs as case-sensitive for different parts of the URL. Since URLs are case sensitive, keeping it low-key (lower cased) is always safe and considered a good standard.

Should API endpoints be camelCase?

If you look at the https://api.example.com/users/1234 link, the 1234 is the userId here. With the above, the user is able to query a user based on their user ID. Now, for the naming convention, it is advisable to use camelCase instead of others.

Should REST endpoints be plural?

In general, using plural nouns is preferred unless the resource is clearly a singular concept (e.g. https://api.example.com/users/admin for the administrative user).

What is RESTful API?

RESTful API is an interface that two computer systems use to exchange information securely over the internet. Most business applications have to communicate with other internal and third-party applications to perform various tasks.


4 Answers

You should use hyphens in a crawlable web application URL. Why? Because the hyphen separates words (so that a search engine can index the individual words), and a hyphen is not a word character. Underscore is a word character, meaning it should be considered part of a word.

Double-click this in Chrome: camelCase
Double-click this in Chrome: under_score
Double-click this in Chrome: hyphen-ated

See how Chrome (I hear Google makes a search engine too) only thinks one of those is two words?

camelCase and underscore also require the user to use the shift key, whereas hyphenated does not.

So if you should use hyphens in a crawlable web application, why would you bother doing something different in an intranet application? One less thing to remember.

like image 171
Neil McGuigan Avatar answered Oct 21 '22 02:10

Neil McGuigan


The standard best practice for REST APIs is to have a hyphen, not camelcase or underscores.

This comes from Mark Masse's "REST API Design Rulebook" from Oreilly.

In addition, note that Stack Overflow itself uses hyphens in the URL: .../hyphen-underscore-or-camelcase-as-word-delimiter-in-uris

As does WordPress: http://inventwithpython.com/blog/2012/03/18/how-much-math-do-i-need-to-know-to-program-not-that-much-actually

like image 25
Al Sweigart Avatar answered Oct 21 '22 04:10

Al Sweigart


Whilst I recommend hyphens, I shall also postulate an answer that isn't on your list:

Nothing At All

  • My company's API has URIs like /quotationrequests/, /purchaseorders/ and so on.
  • Despite you saying it was an intranet app, you listed SEO as a benefit. Google does match the pattern /foobar/ in a URL for a query of ?q=foo+bar
  • I really hope you do not consider executing a PHP call to any arbitrary string the user passes in to the address bar, as @ServAce85 suggests!
like image 31
Nicholas Shanks Avatar answered Oct 21 '22 03:10

Nicholas Shanks


Short Answer:

lower-cased words with a hyphen as separator

Long Answer:

What is the purpose of a URL?

If pointing to an address is the answer, then a shortened URL is also doing a good job. If we don't make it easy to read and maintain, it won't help developers and maintainers alike. They represent an entity on the server, so they must be named logically.

Google recommends using hyphens

Consider using punctuation in your URLs. The URL http://www.example.com/green-dress.html is much more useful to us than http://www.example.com/greendress.html. We recommend that you use hyphens (-) instead of underscores (_) in your URLs.

Coming from a programming background, camelCase is a popular choice for naming joint words.

But RFC 3986 defines URLs as case-sensitive for different parts of the URL. Since URLs are case sensitive, keeping it low-key (lower cased) is always safe and considered a good standard. Now that takes a camel case out of the window.

Source: https://metamug.com/article/rest-api-naming-best-practices.html#word-delimiters

like image 33
Sorter Avatar answered Oct 21 '22 04:10

Sorter