GithubHelp home page GithubHelp logo

Style guide: Thoughts on Enums about buf HOT 3 CLOSED

bufbuild avatar bufbuild commented on May 17, 2024
Style guide: Thoughts on Enums

from buf.

Comments (3)

bufdev avatar bufdev commented on May 17, 2024

This is unique to C++; it works fine in every other language.

This isn't a C++ problem - as we talk about in the Buf docs, the issue will lead to protoc not compiling definitions (regardless of whether it was meant to only protect against C++ issues). Your example above won't compile:

$ protoc -o /dev/null foo.proto
foo.proto:9:3: "UNSPECIFIED" is already defined in "foo".
foo.proto:9:3: Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it.  Therefore, "UNSPECIFIED" must be unique within "foo", not just within "Bar".

We also find that nesting enums leads to issues with forwards compatibility that while not actually breaking compatibility, breaks the spirit, namely that developers typically think an enum only truly applies to their specific message, only later realizing it is a more global concept. This leads to references in other messages for nested enums, which is worse than standardizing on a prefix.

The Google Style Guide also uses it for their example https://developers.google.com/protocol-buffers/docs/style

We'll stick with this recommendation - we find that many people have opinions on Protobuf style, but the one that leads to the least amount of issues down the road, even if more verbose, is usually preferred, especially to help developers of all levels of familiarity with Protobuf.

from buf.

lukesneeringer avatar lukesneeringer commented on May 17, 2024

That sounds reasonable. :-)

from buf.

noahseger avatar noahseger commented on May 17, 2024

@bufdev your comment about nesting is valuable and worth referencing, because the prefix lint rule is the most hated on our team :D Would it make sense to add to https://buf.build/docs/best-practices/style-guide#enums?

from buf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.