Underscores or camel case?

I like to use underscores in my JSON attribute names. But I get the sense that I’d be better off going with camel case, especially since the grpc tooling is going to use camelcase anyway.

From https://developers.google.com/protocol-buffers/docs/reference/javascript-generated:

Note that the generated accessors always use camel-case naming, even if the field name in the .proto file uses lower-case with underscores (as it should).

I think that I know my answer: just make everything camel case. But is that too naive? Anyone have better guidance on this? (This might be a JS/JSON focused question, as well, I recognize.)

1 Like

It depends a bit on your personal and your company’s style.

For Protocol Buffers themselves, the consensus seems to be that:

  • Message names are in PascalCase (CamelCase with an initial capital), as suggested by the gRPC style guide and other style guides like Uber and Buf
  • Field names should be lower snake case (lower_snake_case)

The Google style guide for JavaScript suggests/prescribes that variable names and method names follow CamelCase without an initial capital for their naming, hence the default protobuf compiler for JavaScript follows that approach. Most style guides I found for JSON suggests using CamelCase without an initial capital for element names too, which coincides with the JavaScript style guide.

So following all “best practices” from the style guides, you should indeed go with CamelCase in most cases (except field_names)

However, as I started off the response, the real answer is It depends a bit on your personal and your company's style.

1 Like