[Bf-blender-cvs] [a34e7c3e5d8] master: Cleanup (UI): Early-exit rather than having a big-ish conditional body
Julian Eisel
noreply at git.blender.org
Mon Sep 21 15:07:57 CEST 2020
Commit: a34e7c3e5d844fd2b9e628534e93892467a7897e
Author: Julian Eisel
Date: Mon Sep 21 15:03:02 2020 +0200
Branches: master
https://developer.blender.org/rBa34e7c3e5d844fd2b9e628534e93892467a7897e
Cleanup (UI): Early-exit rather than having a big-ish conditional body
It's generally considered a better codestyle to check conditions early
and exit early when they are not met, over having most logic of a
function within a big `if`-block. Otherwise people have to go over the
entire block to see if there's possibly an `else` somewhere, or any
followup logic.
===================================================================
M source/blender/editors/interface/interface.c
===================================================================
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c
index 8b149e2f97b..f0d19c38537 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -3972,41 +3972,44 @@ static uiBut *ui_but_alloc(const eButType type)
*/
uiBut *ui_but_change_type(uiBut *but, eButType new_type)
{
- if (but->type != new_type) {
- size_t alloc_size;
- const char *alloc_str;
- uiBut *insert_after_but = but->prev;
- bool new_has_custom_type, old_has_custom_type;
-
- /* Remove old button address */
- BLI_remlink(&but->block->buttons, but);
-
- ui_but_alloc_info(but->type, NULL, NULL, &old_has_custom_type);
- ui_but_alloc_info(new_type, &alloc_size, &alloc_str, &new_has_custom_type);
-
- if (new_has_custom_type || old_has_custom_type) {
- const void *old_but_ptr = but;
- /* Button may have pointer to a member within itself, this will have to be updated. */
- const bool has_str_ptr_to_self = but->str == but->strdata;
- const bool has_poin_ptr_to_self = but->poin == (char *)but;
-
- but = MEM_recallocN_id(but, alloc_size, alloc_str);
- but->type = new_type;
- if (has_str_ptr_to_self) {
- but->str = but->strdata;
- }
- if (has_poin_ptr_to_self) {
- but->poin = (char *)but;
- }
+ if (but->type == new_type) {
+ /* Nothing to do. */
+ return but;
+ }
- BLI_insertlinkafter(&but->block->buttons, insert_after_but, but);
+ size_t alloc_size;
+ const char *alloc_str;
+ uiBut *insert_after_but = but->prev;
+ bool new_has_custom_type, old_has_custom_type;
- if (but->layout) {
- const bool found_layout = ui_layout_replace_but_ptr(but->layout, old_but_ptr, but);
- BLI_assert(found_layout);
- UNUSED_VARS_NDEBUG(found_layout);
- ui_button_group_replace_but_ptr(but->layout, old_but_ptr, but);
- }
+ /* Remove old button address */
+ BLI_remlink(&but->block->buttons, but);
+
+ ui_but_alloc_info(but->type, NULL, NULL, &old_has_custom_type);
+ ui_but_alloc_info(new_type, &alloc_size, &alloc_str, &new_has_custom_type);
+
+ if (new_has_custom_type || old_has_custom_type) {
+ const void *old_but_ptr = but;
+ /* Button may have pointer to a member within itself, this will have to be updated. */
+ const bool has_str_ptr_to_self = but->str == but->strdata;
+ const bool has_poin_ptr_to_self = but->poin == (char *)but;
+
+ but = MEM_recallocN_id(but, alloc_size, alloc_str);
+ but->type = new_type;
+ if (has_str_ptr_to_self) {
+ but->str = but->strdata;
+ }
+ if (has_poin_ptr_to_self) {
+ but->poin = (char *)but;
+ }
+
+ BLI_insertlinkafter(&but->block->buttons, insert_after_but, but);
+
+ if (but->layout) {
+ const bool found_layout = ui_layout_replace_but_ptr(but->layout, old_but_ptr, but);
+ BLI_assert(found_layout);
+ UNUSED_VARS_NDEBUG(found_layout);
+ ui_button_group_replace_but_ptr(but->layout, old_but_ptr, but);
}
}
More information about the Bf-blender-cvs
mailing list