RESTful API Design: what about attribute names?
Brian Mulloy
In our continuing discussino aboutr Pragmatic REST API design, let's talk about what happens when a response comes back.
You have an object with data attributes on it. How should you name the attributes?
Here are API responses from a few leading APIs:
"created_at": Thu Nov 03 05:19;38 +0000 2011"
Bing
"DateTime": "2011-10-29T09:35:00Z"
Foursquare
"createdAt": 1320296464
They each use a different code convention. Although the Twitter approach is familiar to me as a Ruby on Rails developer, I think that Foursquare has the best approach.
How does the API response get back in the code? You parse the response (JSON parser); what comes back populates the Object. It looks like this
var myObject = JSON.parse(response);
If you chose the Twitter or Bing approach, your code looks like this. It's not JavaScript convention and looks weird - looks like the name of another object or class in the system, which is not correct.
timing = myObject.created_at;
timing - myObject.DateTime;
Recommendations
Use JSON as default
Follow JavaScript conventions for naming attributes
- Use medial capitalization (aka CamelCase)
- Use uppercase or lowercase depending on type of object
This results in code that looks like the following, allowing the JavaScript developer to write it in a way that makes sense for JavaScript.
"createdAt": 1320296464
timing = myObject.createdAt;