Policy to change clang-format brace placement #75956

Closed
opened 2020-04-21 10:01:17 +02:00 by Campbell Barton · 12 comments

Note, this proposal is for when we switch to clang-format 10, which is probably not something that's expected to happen soon.


This task proposes to use clang-format with multi-line brace placement.

This was used for Blender's code-style before using clang-format, however clang-format didn't support differentiating between multi-line and single-lines.

With the release of clang-format 10, this is now supported.

Motivation

Better readability by separating checks from if statements and loops with the body of code block.


Example:

Before:

  if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE) ||
      /* if we started dragging, progress on any event */
      (data->multi_data.init == BUTTON_MULTI_INIT_SETUP)) {
    if (ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER) &&
        ELEM(data->state, BUTTON_STATE_TEXT_EDITING, BUTTON_STATE_NUM_EDITING)) {
      /* initialize! */
      if (data->multi_data.init == BUTTON_MULTI_INIT_UNSET) {
        /* --- snip --- */
      }
    }
  }

After:

  if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE) ||
      /* if we started dragging, progress on any event */
      (data->multi_data.init == BUTTON_MULTI_INIT_SETUP))
  {
    if (ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER) &&
        ELEM(data->state, BUTTON_STATE_TEXT_EDITING, BUTTON_STATE_NUM_EDITING))
    {
      /* initialize! */
      if (data->multi_data.init == BUTTON_MULTI_INIT_UNSET) {
        /* --- snip --- */
      }
    }
  }

Before:

    if (!_PyArg_ParseTupleAndKeywordsFast(args,
                                          kw,
                                          &_parser,
                                          &id,
                                          &id_len,
                                          &name,
                                          &description,
                                          PyC_ParseBool,
                                          &def,
                                          &PySet_Type,
                                          &pyopts,
                                          &PySet_Type,
                                          &pyopts_override,
                                          &PySet_Type,
                                          &py_tags,
                                          &pysubtype,
                                          &update_cb,
                                          &get_cb,
                                          &set_cb)) {
      return NULL;
    }

After:

    if (!_PyArg_ParseTupleAndKeywordsFast(args,
                                          kw,
                                          &_parser,
                                          &id,
                                          &id_len,
                                          &name,
                                          &description,
                                          PyC_ParseBool,
                                          &def,
                                          &PySet_Type,
                                          &pyopts,
                                          &PySet_Type,
                                          &pyopts_override,
                                          &PySet_Type,
                                          &py_tags,
                                          &pysubtype,
                                          &update_cb,
                                          &get_cb,
                                          &set_cb))
    {
      return NULL;
    }

Before:

        if (CustomData_data_equals(
                lwc->type, lwc->data_ref, BM_ELEM_CD_GET_VOID_P(l_other, lwc->cd_layer_offset))) {
          bm_loop_walk_data(lwc, l_other);
        }

After:

        if (CustomData_data_equals(
                lwc->type, lwc->data_ref, BM_ELEM_CD_GET_VOID_P(l_other, lwc->cd_layer_offset)))
        {
          bm_loop_walk_data(lwc, l_other);
        }

Tradeoff's

Pros

  • Improved readability for blocks of code which often end up being poorly separated in cases where the multi-line statements are already becoming hard to follow.

Cons

  • It's a disruptive change for branches.
  • Uses more vertical space.

Proposal

Enable this option once we've updated to clang-format version 10.

Change to .clang-format (with surrounding values omitted).

BraceWrapping: {
    AfterControlStatement: 'MultiLine' 
}

*Note, this proposal is for when we switch to clang-format 10, which is probably not something that's expected to happen soon.* ---- This task proposes to use clang-format with multi-line brace placement. This was used for Blender's code-style before using clang-format, however clang-format didn't support differentiating between multi-line and single-lines. With the release of clang-format 10, this is now supported. **Motivation** Better readability by separating checks from `if` statements and loops with the body of code block. ---- Example: Before: ``` if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE) || /* if we started dragging, progress on any event */ (data->multi_data.init == BUTTON_MULTI_INIT_SETUP)) { if (ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER) && ELEM(data->state, BUTTON_STATE_TEXT_EDITING, BUTTON_STATE_NUM_EDITING)) { /* initialize! */ if (data->multi_data.init == BUTTON_MULTI_INIT_UNSET) { /* --- snip --- */ } } } ``` After: ``` if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE) || /* if we started dragging, progress on any event */ (data->multi_data.init == BUTTON_MULTI_INIT_SETUP)) { if (ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER) && ELEM(data->state, BUTTON_STATE_TEXT_EDITING, BUTTON_STATE_NUM_EDITING)) { /* initialize! */ if (data->multi_data.init == BUTTON_MULTI_INIT_UNSET) { /* --- snip --- */ } } } ``` Before: ``` if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &id, &id_len, &name, &description, PyC_ParseBool, &def, &PySet_Type, &pyopts, &PySet_Type, &pyopts_override, &PySet_Type, &py_tags, &pysubtype, &update_cb, &get_cb, &set_cb)) { return NULL; } ``` After: ``` if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &id, &id_len, &name, &description, PyC_ParseBool, &def, &PySet_Type, &pyopts, &PySet_Type, &pyopts_override, &PySet_Type, &py_tags, &pysubtype, &update_cb, &get_cb, &set_cb)) { return NULL; } ``` Before: ``` if (CustomData_data_equals( lwc->type, lwc->data_ref, BM_ELEM_CD_GET_VOID_P(l_other, lwc->cd_layer_offset))) { bm_loop_walk_data(lwc, l_other); } ``` After: ``` if (CustomData_data_equals( lwc->type, lwc->data_ref, BM_ELEM_CD_GET_VOID_P(l_other, lwc->cd_layer_offset))) { bm_loop_walk_data(lwc, l_other); } ``` **Tradeoff's** **Pros** - Improved readability for blocks of code which often end up being poorly separated in cases where the multi-line statements are already becoming hard to follow. **Cons** - It's a disruptive change for branches. - Uses more vertical space. ---- **Proposal** Enable this option once we've updated to clang-format version 10. Change to `.clang-format` (with surrounding values omitted). ``` BraceWrapping: { AfterControlStatement: 'MultiLine' } ```
Campbell Barton self-assigned this 2020-04-21 10:01:17 +02:00
Author
Owner

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Author
Owner

Added subscriber: @ideasman42

Added subscriber: @ideasman42

Added subscriber: @Sergey

Added subscriber: @Sergey

I see this as a bandage on top of a improperly structured code, and the improved readability is subjective here. The more readable version would be if (need_initialize_multi_data(...)) and move all those levels of nesting into a function, sue early returns, keep indentation low, describe conditions as needed. And keep the current formatting, so that developers are disguised and are forced to type properly readable code ;)

To me this is a disruptive change, which doesn't solve the claimed root issue.

I see this as a bandage on top of a improperly structured code, and the improved readability is subjective here. The more readable version would be `if (need_initialize_multi_data(...))` and move all those levels of nesting into a function, sue early returns, keep indentation low, describe conditions as needed. And keep the current formatting, so that developers are disguised and are forced to type properly readable code ;) To me this is a disruptive change, which doesn't solve the claimed root issue.

Multi-post spam, yay!

Had a quick chat with Dalai about how to make such reviews don't feel difficult for any of the sides. To come to the consensus, here is my statement: I don't find this proposal great, but I can accept it.

Multi-post spam, yay! Had a quick chat with Dalai about how to make such reviews don't feel difficult for any of the sides. To come to the consensus, here is my statement: I don't find this proposal great, but I can accept it.

Added subscriber: @mont29

Added subscriber: @mont29

Big +1 from me, that was one of the main issues I had when we switched to Clang-format.

Big +1 from me, that was one of the main issues I had when we switched to Clang-format.
Author
Owner

+1, even though the code that reads poorly could be refactored in some cases. There is enough of this that can't/wont be easily changed.

+1, even though the code that reads poorly could be refactored in some cases. There is enough of this that can't/wont be easily changed.

Added subscriber: @brecht

Added subscriber: @brecht

I have no preference, either is fine.

I have no preference, either is fine.

I agree with @Sergey, both on the 'this should be refactored' front and the 'I can live with this' front.

I agree with @Sergey, both on the 'this should be refactored' front and the 'I can live with this' front.
Author
Owner

Applied:

Applied: - `.clang-format`: 391f86bc387d745de897503c36d11cd65691b84c - `make format`: 6859bb6e67031765e79e525ae62bf2ebf4df2330 - manual cleanup afterwards: a0db0a55804eb396d4a19dd5f692dd7e9eebb81d
Blender Bot added
Status
Archived
and removed
Status
Confirmed
labels 2023-05-02 03:01:51 +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
5 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#75956
No description provided.