Skip to content

Commit 6843137

Browse files
Add style guide note that if you prefix enum values, that the names with the prefix stripped should be legal and style-conformant names.
PiperOrigin-RevId: 839806167
1 parent d21b2ed commit 6843137

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

content/programming-guides/style.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,28 @@ some languages don't support an enum being defined inside a "struct" type,
187187
preferring prefixed values ensures a consistent approach across binding
188188
languages.
189189

190+
When prefixing enum values, the remainder of the name with the prefix stripped
191+
should still be a legal and style-conformant enum name. For example, avoid the
192+
following:
193+
194+
```
195+
enum DeviceTier {
196+
DEVICE_TIER_UNKNOWN = 0;
197+
DEVICE_TIER_1 = 1;
198+
DEVICE_TIER_2 = 2;
199+
}
200+
```
201+
202+
Instead, use a value name like `DEVICE_TIER_TIER1`, where the `DEVICE_TIER_`
203+
portion is viewed as scoping the enum value rather than as part of the
204+
individual enum value name. Some Protobuf implementations automatically strip
205+
the prefix that matches the containing enum name where it is safe to do so, but
206+
could not in this example since a bare `1` is not a legal enum value name.
207+
208+
A future Edition will add support for scoped enums, which will eliminate the
209+
need to manually prefix each enum value and enable this to be written succinctly
210+
as `TIER1 = 1`.
211+
190212
## Services {#services}
191213

192214
Use TitleCase for service names and method names.

0 commit comments

Comments
 (0)