Policy for style guide: document use of 'e' prefixed enums in C/C++ #98553

Open
opened 2022-06-02 09:00:24 +02:00 by Campbell Barton · 12 comments

Currently most typedef'd enums use an e prefix, although this isn't mentioned in the code style guide.

As this isn't noted as a convention it's not clear if/when they should be used if it's reasonable to request them in code-review for example.

I'd propose to include this as part of Blender code-style https://wiki.blender.org/wiki/Source/Code_Style - mainly because its following what's already being done (for the most-part), and it can be a useful hint.


When used, it's obvious the name is an enum type which are often passed and copied by value and don't need to be declared const in the header for e.g. Where as all other types we normally pass by pointer or reference and declare const where possible.


From a quick search in source/:

  • typedef enum e[A-Z] + ^enum e[A-Z] -> 817.
  • typedef enum [A-Z] + ^enum [A-Z] -> 185.

This misses some indented declarations but I don't think it makes a big difference.

Currently most typedef'd `enums` use an `e` prefix, although this isn't mentioned in the code style guide. As this isn't noted as a convention it's not clear if/when they should be used if it's reasonable to request them in code-review for example. I'd propose to include this as part of Blender code-style https://wiki.blender.org/wiki/Source/Code_Style - mainly because its following what's already being done (for the most-part), and it can be a useful hint. ---- When used, it's obvious the name is an `enum` type which are often passed and copied by value and don't need to be declared `const` in the header for e.g. Where as all other types we normally pass by pointer or reference and declare `const` where possible. ---- From a quick search in `source/`: - `typedef enum e[A-Z]` + `^enum e[A-Z]` -> 817. - `typedef enum [A-Z]` + `^enum [A-Z]` -> 185. *This misses some indented declarations but I don't think it makes a big difference.*
Author
Owner

Added subscriber: @ideasman42

Added subscriber: @ideasman42
Campbell Barton changed title from Policy for style guide: use of 'e' prefixed enums to Policy for style guide: use of 'e' prefixed enums in C/C++ 2022-06-02 09:05:23 +02:00
Campbell Barton changed title from Policy for style guide: use of 'e' prefixed enums in C/C++ to Policy for style guide: document use of 'e' prefixed enums in C/C++ 2022-06-02 09:31:48 +02:00
Member

Added subscriber: @HooglyBoogly

Added subscriber: @HooglyBoogly
Member

Personally I would prefer not so use the e prefix for enums, for a few reasons:

  • In practice it's almost always clear that something is an enum anyway, either because of context, or words like Type, Mode, Flag etc. in the enum name.
  • "e" makes the enum names harder to say, which is a really important way to improve code readability IMO.
  • A bit more fuzzy: it makes the code look less like "normal language", which makes it more intimidating.
  • An extra letter-- makes code longer
  • Lack of first letter capital CamelCase for types (nice for consistency)

For the pass-by-value/const things meantioned in the task, I'm not sure this is much different from other custom types that we also pass by value (Span, IndexMask, etc.)
Avoiding enum names that look like struct names makes sense, but personally I don't think this is the best way to do it, and I could see it even making enum names worse.


It doesn't change much, but since typedef isn't usually used in C++, I think the searches above misses some examples:

enum [A-Z] -> 188
enum class [A-Z] -> 47

Personally I would prefer not so use the `e` prefix for enums, for a few reasons: - In practice it's almost always clear that something is an enum anyway, either because of context, or words like `Type`, `Mode`, `Flag` etc. in the enum name. - "e" makes the enum names harder to say, which is a really important way to improve code readability IMO. - A bit more fuzzy: it makes the code look less like "normal language", which makes it more intimidating. - An extra letter-- makes code longer - Lack of first letter capital CamelCase for types (nice for consistency) --- For the pass-by-value/const things meantioned in the task, I'm not sure this is much different from other custom types that we also pass by value (`Span`, `IndexMask`, etc.) Avoiding enum names that look like struct names makes sense, but personally I don't think this is the best way to do it, and I could see it even making enum names worse. --- It doesn't change much, but since `typedef` isn't usually used in C++, I think the searches above misses some examples: `enum [A-Z]` -> 188 `enum class [A-Z]` -> 47

Added subscriber: @Sergey

Added subscriber: @Sergey

Something we've been talking with Campbell in the blender chat. I share the thoughts with Hans here.

To not repeat what is already mentioned, the gist would be: to me the important part is to make the "meaningful" part of the type name clear. Using Flag / Type whenever the enum is a flag or a type of something I find to be more important.

Something we've been talking with Campbell in the blender chat. I share the thoughts with Hans here. To not repeat what is already mentioned, the gist would be: to me the important part is to make the "meaningful" part of the type name clear. Using `Flag` / `Type` whenever the enum is a flag or a type of something I find to be more important.

Added subscriber: @mont29

Added subscriber: @mont29

Not really convinced here, don't really see the added value to that rule, not a big fan of adding type hints in names in general... and would rather avoid yet another giant cleanup messing up with tens of header files.

So no hard no from me, but a 'would rather not' ;)

Not really convinced here, don't really see the added value to that rule, not a big fan of adding type hints in names in general... and would rather avoid yet another giant cleanup messing up with tens of header files. So no hard no from me, but a 'would rather not' ;)
Member

Added subscriber: @JacquesLucke

Added subscriber: @JacquesLucke
Member

Generally agree with Hans, Sergey and Bastien here. Don't have another argument to add.

Generally agree with Hans, Sergey and Bastien here. Don't have another argument to add.

Added subscriber: @dr.sybren

Added subscriber: @dr.sybren

Same here, I agree with Hans, Sergey, and Bastien. For me, personally, these enum-related things are much more important for understandability of the code:

  • Better documentation of short and int as this-should-be-an-enum-type in DNA, so that it's clear which enum is used (this is sometimes already there, but also sometimes not).
  • IMO using int and short for enum values in non-DNA code should be forbidden.
  • Distinction between "enum of bit flags that can be ORed into a single variable" vs. "actual enum that can take only a single one of those values".

IMO if you need an e prefix for an enum, it's a sign that the declaration is too far from the use, and that the code is likely getting too complex. That's not a problem to be solved with a prefix.

PS: the above is not intended to change the scope of this patch. Just to elaborate a bit on my vision on the use of enums in our code.

Same here, I agree with Hans, Sergey, and Bastien. For me, personally, these enum-related things are much more important for understandability of the code: - Better documentation of `short` and `int` as this-should-be-an-enum-type in DNA, so that it's clear which enum is used (this is sometimes already there, but also sometimes not). - IMO using `int` and `short` for enum values in non-DNA code should be forbidden. - Distinction between "enum of bit flags that can be ORed into a single variable" vs. "actual enum that can take only a single one of those values". IMO if you need an `e` prefix for an enum, it's a sign that the declaration is too far from the use, and that the code is likely getting too complex. That's not a problem to be solved with a prefix. PS: the above is not intended to change the scope of this patch. Just to elaborate a bit on my vision on the use of enums in our code.
Author
Owner

As the replies here prefer not to use the e prefix I've created a counter proposal #99080 (Policy for style guide: document enum suffix use for C/C++).

As the replies here prefer not to use the `e` prefix I've created a counter proposal #99080 (Policy for style guide: document enum suffix use for C/C++).
Philipp Oeser removed the
Interest
Core
label 2023-02-09 14:42:50 +01:00
Iliya Katushenock removed the
Status
Needs Triage
label 2023-08-24 17:38:27 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#98553
No description provided.