I have been told to use snake case in my DB and never camelCase. JSON API says to use dashes between words for object keys. Javascript, everyone uses camelCase.
If I have a RAILS server getting data from an SQL database, sending it as json and being used in javascript, do I really need to convert between these notations?
Why not just use snake case everywhere?
I have been told to use snake case in my DB and never camelCase. JSON API says to use dashes between words for object keys. Javascript, everyone uses camelCase.
If I have a RAILS server getting data from an SQL database, sending it as json and being used in javascript, do I really need to convert between these notations?
Why not just use snake case everywhere?
Share Improve this question asked Oct 13, 2015 at 4:56 P0lskaP0lska 4711 gold badge7 silver badges18 bronze badges 2- 4 If you are programming for yourself then use whatever you are fortable with. If you are programming for your employeer you use whatever they require. – user2417483 Commented Oct 13, 2015 at 5:00
-
do I really need to convert between these notations
-someValue
!=some_value
!=some-value
- so, something has to convert between the three formats if you are trying to access some value . P.S. I see nothing in the JSON spec that says to use dashes. But then, I don't know what you really mean by JASON API – Jaromanda X Commented Oct 13, 2015 at 5:05
4 Answers
Reset to default 6For JavaScript it is up to you (or the guidelines of the project) if you would like to use camel case or not. But because almost every majaor library and the core API uses camel cases it is a good idea to do the same, because then you wont need to think about if you need to use the one or the other.
For DBMS it depends on the operating system and DBMS because you might get technical problems when using mixed letters. E.g. if you have a MySQL database running on a Windows-System, then MySQL would not care about the case of the table names, because the file system is case insensitive. As of that the default MySQL configuration on Windows will automatically convert all table names to lowercases. As long as your DBMS will stay on the windows machine this won't be a problem, but as soon as you decide to switch to a Linux base server then you will get huge problems to migrate your data from the windows to the linux box.
To be not dependent on those problems, it is mon to only use lowercase letters with DBMS, no matter if the DBMS would have problems with uppercase letters or not. With lowercase letters you will definitely have no problems and with uppercase letters you need to do investigations if it can be a problem.
I think the main pain we experience is that we try to follow the conventions that are established within a language munity, but when JSON is serialized from one language and then parsed into a map for another language, there is some annoyance when the variables or object keys now don't follow the same convention. That's one of the reasons why I might stop following the camel-case convention in the frontend and go back to snake-case everywhere since it's less cognitive overhead for me if there is only one style throughout this project's codebase. Of course this is a personal project, and something I guarantee would experience a lot of push back at work.
You really do not need to convert it as per standards. Naming conventions are followed in order for a group of developers to work on same application.
It is a best practice to follow naming conventions defined by a organization for better code understanding among developers.
You can use this library to convert between the two: case-converter It converts snake_case to camelCase and vice versa
const caseConverter = require('case-converter')
const snakeCase = {
an_object: {
nested_string: 'nested content',
nested_array: [{ an_object: 'something' }]
},
an_array: [
{ zero_index: 0 },
{ one_index: 1 }
]
}
const camelCase = caseConverter.toCamelCase(snakeCase);
console.log(camelCase)
/*
{
anObject: {
nestedString: 'nested content',
nestedArray: [{ anObject: 'something' }]
},
anArray: [
{ zeroIndex: 0 },
{ oneIndex: 1 }
]
}
*/