views:

199

answers:

3

If you are wrapping an API which has different coding style guidelines to the programming languages you are using, do you create an interface that complies with the languages style guidelines or expose the implementation to the user of the API wrapper.

A specific example is for ruby and python variables are usually all lowercase and use underscores

my_variable = 'Hello World'

while in Java/C# camel case is the standard

myVariable = 'Hello World'

for instance the web service has a method to create a contact in json it would be

{contact: {contactId: 1, firstName: 'John', lastName: 'Smith', emailAddress: '[email protected]'}}

for instance do you create a class with the same variable names (in camel case)

class Contact:
    contactId = 1
    firstName = 'John'
    lastName = 'Smith'
    emailAddress = '[email protected]'

or in a method

def create_contact(contactId, firstName, lastName, emailAddress): 
    # make remote request

or do you follow style guidelines and then covert variables names when making a request

def create_contact(contact_id, first_name, last_name, email_address):
    # convert variable names
    # make remote request
+1  A: 

When you are creating a wrapper for an existing API, the part that you expose to consumers of the wrapper should be in the style of the language you are writing the wrapper in.

Matt Bridges
A: 

I would go back to your language's coding standards as fast as possible - this way, all the code in your system looks the same, no matter where something came from some web API or not. I believe that you'll quickly find objects with camelcased keys (in your case) travelling all through your system, which is veyr annoying and badly structured.

If you do not have mad performance requirements, you could however think of making a generic API Caller class or module that auto-transforms camelCased keys to underscored keys. Of course there will be situations where this goes wrong (a key called APIKey would typically become a_p_i_key), but as long as you know this you can fix up those specific cases in a hardcoded manner in the API wrapper. This way, your headache will be minimal and your code style will still be consistent

skrebbel
A: 

Often if you are using JSON it makes sense to use your language's equivalent to the JSON structure directly, eg dict and lists in Python. This doesn't always work, but it is often the right approach.

That will mean you use camel case eg Python {'contactId': 1, 'firstName': 'Justin'} etc, but these are just identifiers not variables, so I would not worry too much.

Creating functions and classes is much too verbose when you can handle the structures directly.

XML APIs are another matter, but the point of JSON is to be lightweight and map to native language constructs.

Justin Cormack