Why use Camel Case for JS and Snake Case for your DB?

10,640

Solution 1

For 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 common 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.

Solution 2

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.

Solution 3

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 }
      ]
    }
  */

Solution 4

I think the main pain we experience is that we try to follow the conventions that are established within a language community, 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.

Share:
10,640
P0lska
Author by

P0lska

Updated on June 19, 2022

Comments

  • P0lska
    P0lska almost 2 years

    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?

  • P0lska
    P0lska over 8 years
    Thanks for the detailed answer. So I'll use camelCase in my front end. Does this mean I should be converting the json "get" requests to camelCase with JS? I'm really having trouble figuring out how to do that in AngularJS
  • Nikos
    Nikos about 8 years
    I feel its not worth the hastle if you will be deploying on linux. Who wants to manually convert the db data to camel case for JS?