Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snake_case or camelCase in node.js [closed]

So I have node.js application that exposes some API to clients and connects to some SQL database. First let introduce some premises:

  1. Clients must use API with snake_case (keys in JSON object sent in request body are in snake_case).
  2. Database column names are also snake_case.

Problem is because javascript standard/practice is using camelCase which I would like to keep.

So as I see there are 3 solutions:

  1. Transform data from snake(API) to camel (application) to snake (database) on the fly as needed. This would introduced some new layers to system and all of it would be little complicated than other solutions.
  2. Use snake_case in node.js app. But no matter if I use snake all other dependencies I am using will still be in camel which doesn't solve whole problem.
  3. Mix snake_case and camelCase in node.js app. This is something I would like to avoid :)

So what do you suggest? Mine favorite is first solutions but if you have some other smart ideas not mentioned here please feel free to tell.

like image 409
Ivan Longin Avatar asked Feb 01 '14 20:02

Ivan Longin


People also ask

Is node A CamelCase?

Besides, the only reason Node uses camelcase is because Javascript's own default API (from the 1990s) uses it.

Should I use CamelCase in JS?

camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries. Do not start names with a $ sign. It will put you in conflict with many JavaScript library names.

Does JavaScript use snake case or CamelCase?

Snake case (also referred to as underscore case) is when all the letters of the word are lower case but delimited by an underscore. We seldom use the snake case coding convention in C-style languages like Java, JavaScript, and TypeScript.

Which is better CamelCase or snake case?

Screaming snake case is used for variables. Scripting languages, as demonstrated in the Python style guide, recommend snake case in the instances where C-based languages use camel case.


2 Answers

As Occam's razor principle says, the simplest solution is the best one.

If you have client's requirements on the one hand and node.js code style guides on the other hand. I would take the simplest solution satisfying the both. External API should be snake case and internally you can use camel case. If you stick to consistency you can use snake case everywhere, as I assume client's requirements have higher priority.

I would really avoid any additional layers for converting cases as it introduces additional complexity to the final solution, it can have additional errors, and it will require additional maintenance in the future.

like image 130
sergej shafarenka Avatar answered Sep 18 '22 21:09

sergej shafarenka


Personally, when I have to deal with snake case in javascript, I use it only with square brackets:

data['user_id'] = 1; 

so, technically I don't violate any style rules: these are just strings, not identifiers

like image 26
vkurchatkin Avatar answered Sep 21 '22 21:09

vkurchatkin