From 475b6242380fffbc757c36ef82ae221b778d9a74 Mon Sep 17 00:00:00 2001 From: Lucas Borin <5233413+lucasborin@users.noreply.github.com> Date: Mon, 5 Apr 2021 21:16:04 +0200 Subject: [PATCH 1/7] implements #283 --- .../y_check_prefer_new_to_crt_obj.clas.abap | 45 ++++++++++++ ...refer_new_to_crt_obj.clas.testclasses.abap | 68 +++++++++++++++++++ .../y_check_prefer_new_to_crt_obj.clas.xml | 17 +++++ 3 files changed, 130 insertions(+) create mode 100644 src/checks/y_check_prefer_new_to_crt_obj.clas.abap create mode 100644 src/checks/y_check_prefer_new_to_crt_obj.clas.testclasses.abap create mode 100644 src/checks/y_check_prefer_new_to_crt_obj.clas.xml diff --git a/src/checks/y_check_prefer_new_to_crt_obj.clas.abap b/src/checks/y_check_prefer_new_to_crt_obj.clas.abap new file mode 100644 index 00000000..9a280d9b --- /dev/null +++ b/src/checks/y_check_prefer_new_to_crt_obj.clas.abap @@ -0,0 +1,45 @@ +CLASS y_check_prefer_new_to_crt_obj DEFINITION PUBLIC INHERITING FROM y_check_base CREATE PUBLIC . + PUBLIC SECTION. + METHODS constructor . + + PROTECTED SECTION. + METHODS inspect_tokens REDEFINITION. + +ENDCLASS. + + + +CLASS y_check_prefer_new_to_crt_obj IMPLEMENTATION. + + + METHOD constructor. + super->constructor( ). + + settings-pseudo_comment = '"#EC PREF_NEW' ##NO_TEXT. + settings-disable_threshold_selection = abap_true. + settings-threshold = 0. + settings-prio = c_warning. + settings-documentation = |{ c_docs_path-checks }prefer_new_to_create_object.md|. + + set_check_message( 'Prefer NEW to CREATE OBJECT' ). + ENDMETHOD. + + + METHOD inspect_tokens. + CHECK get_token_abs( statement-from ) = 'CREATE' + AND get_token_abs( statement-from + 1 ) = 'OBJECT'. + + DATA(check_configuration) = detect_check_configuration( statement ). + + IF check_configuration IS INITIAL. + RETURN. + ENDIF. + + raise_error( statement_level = statement-level + statement_index = index + statement_from = statement-from + error_priority = check_configuration-prio ). + ENDMETHOD. + + +ENDCLASS. diff --git a/src/checks/y_check_prefer_new_to_crt_obj.clas.testclasses.abap b/src/checks/y_check_prefer_new_to_crt_obj.clas.testclasses.abap new file mode 100644 index 00000000..da741938 --- /dev/null +++ b/src/checks/y_check_prefer_new_to_crt_obj.clas.testclasses.abap @@ -0,0 +1,68 @@ +CLASS ltc_create_object DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. + PROTECTED SECTION. + METHODS get_cut REDEFINITION. + METHODS get_code_with_issue REDEFINITION. + METHODS get_code_without_issue REDEFINITION. + METHODS get_code_with_exemption REDEFINITION. +ENDCLASS. + +CLASS ltc_create_object IMPLEMENTATION. + + METHOD get_cut. + result ?= NEW y_check_prefer_new_to_crt_obj( ). + ENDMETHOD. + + METHOD get_code_with_issue. + result = VALUE #( + ( ' REPORT y_example. ' ) + + ( ' CLASS y_example DEFINITION. ' ) + ( ' PUBLIC SECTION. ' ) + ( ' METHODS example. ' ) + ( ' ENDCLASS. ' ) + + ( ' CLASS y_example IMPLEMENTATION. ' ) + ( ' METHOD example.' ) + ( ' DATA prefer_new_to_crt_obj TYPE REF TO y_check_prefer_new_to_crt_obj. ' ) + ( ' CREATE OBJECT prefer_new_to_crt_obj. ' ) + ( ' ENDMETHOD.' ) + ( ' ENDCLASS. ' ) + ). + ENDMETHOD. + + METHOD get_code_without_issue. + result = VALUE #( + ( ' REPORT y_example. ' ) + + ( ' CLASS y_example DEFINITION. ' ) + ( ' PUBLIC SECTION. ' ) + ( ' METHODS example. ' ) + ( ' ENDCLASS. ' ) + + ( ' CLASS y_example IMPLEMENTATION. ' ) + ( ' METHOD example.' ) + ( ' DATA(prefer_new_to_crt_obj) = NEW y_check_prefer_new_to_crt_obj( ). ' ) + ( ' ENDMETHOD.' ) + ( ' ENDCLASS. ' ) + ). + ENDMETHOD. + + METHOD get_code_with_exemption. + result = VALUE #( + ( ' REPORT y_example. ' ) + + ( ' CLASS y_example DEFINITION. ' ) + ( ' PUBLIC SECTION. ' ) + ( ' METHODS example. ' ) + ( ' ENDCLASS. ' ) + + ( ' CLASS y_example IMPLEMENTATION. ' ) + ( ' METHOD example.' ) + ( ' DATA prefer_new_to_crt_obj TYPE REF TO y_check_prefer_new_to_crt_obj. ' ) + ( ' CREATE OBJECT prefer_new_to_crt_obj. "#EC PREF_NEW' ) + ( ' ENDMETHOD.' ) + ( ' ENDCLASS. ' ) + ). + ENDMETHOD. + +ENDCLASS. diff --git a/src/checks/y_check_prefer_new_to_crt_obj.clas.xml b/src/checks/y_check_prefer_new_to_crt_obj.clas.xml new file mode 100644 index 00000000..ea4a0e74 --- /dev/null +++ b/src/checks/y_check_prefer_new_to_crt_obj.clas.xml @@ -0,0 +1,17 @@ + + + + + + Y_CHECK_PREFER_NEW_TO_CRT_OBJ + E + Prefer New to Create Object + 1 + X + X + X + X + + + + From cc948f832856bea61c06f3a9995b97fcf5582b98 Mon Sep 17 00:00:00 2001 From: Lucas Borin <5233413+lucasborin@users.noreply.github.com> Date: Mon, 5 Apr 2021 16:19:52 -0300 Subject: [PATCH 2/7] Update check_documentation.md --- docs/check_documentation.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/check_documentation.md b/docs/check_documentation.md index 056659b6..7afa1dec 100644 --- a/docs/check_documentation.md +++ b/docs/check_documentation.md @@ -40,6 +40,7 @@ - [Number of Output Parameter](checks/number-output-parameter.md) - [Prefer CASE to ELSEIF](checks/prefer-case-to-elseif.md) - [Prefer IS NOT to NOT IS](checks/prefer-is-not-to-not-is.md) +- [Prefer NEW to CREATE OBJECT](checks/prefer-new-to-create-object.md) - [Pseudo Comment Usage](checks/pseudo-comment-usage.md) - [Omit Optional EXPORTING](checks/omit-optional-exporting.md) - [Optional Parameters](checks/optional-parameters.md) From 7a2d2db198c7fa71d0025323782e756a708e0b6f Mon Sep 17 00:00:00 2001 From: Lucas Borin <5233413+lucasborin@users.noreply.github.com> Date: Mon, 5 Apr 2021 21:21:06 +0200 Subject: [PATCH 3/7] minor fixes --- src/checks/y_check_prefer_new_to_crt_obj.clas.abap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checks/y_check_prefer_new_to_crt_obj.clas.abap b/src/checks/y_check_prefer_new_to_crt_obj.clas.abap index 9a280d9b..e30d43b2 100644 --- a/src/checks/y_check_prefer_new_to_crt_obj.clas.abap +++ b/src/checks/y_check_prefer_new_to_crt_obj.clas.abap @@ -19,9 +19,9 @@ CLASS y_check_prefer_new_to_crt_obj IMPLEMENTATION. settings-disable_threshold_selection = abap_true. settings-threshold = 0. settings-prio = c_warning. - settings-documentation = |{ c_docs_path-checks }prefer_new_to_create_object.md|. + settings-documentation = |{ c_docs_path-checks }prefer-new-to-create-object.md|. - set_check_message( 'Prefer NEW to CREATE OBJECT' ). + set_check_message( 'Prefer NEW to CREATE OBJECT!' ). ENDMETHOD. From 77abc17624fe022cd85770c585b6fac7622aee23 Mon Sep 17 00:00:00 2001 From: Lucas Borin <5233413+lucasborin@users.noreply.github.com> Date: Mon, 5 Apr 2021 16:27:31 -0300 Subject: [PATCH 4/7] Create prefer-new-to-create-object.md --- docs/checks/prefer-new-to-create-object.md | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 docs/checks/prefer-new-to-create-object.md diff --git a/docs/checks/prefer-new-to-create-object.md b/docs/checks/prefer-new-to-create-object.md new file mode 100644 index 00000000..fdaf7a10 --- /dev/null +++ b/docs/checks/prefer-new-to-create-object.md @@ -0,0 +1,44 @@ +[code pal for ABAP](../../README.md) > [Documentation](../check_documentation.md) > [Prefer New to Create Object](prefer_new_to_create_object.md) + +## Prefer New to Create Object + +### What is the Intent of the Check? + +Prefer `NEW` to `CREATE OBJECT` as it avoids needlessly longer statements. + +### How to solve the issue? + +Preferably, use a `NEW`. + +### What to do in case of exception? + +In exceptional cases, you can suppress this finding by using the pseudo comment `"#EC PREF_NEW`: + +```abap + DATA prefer_new_to_crt_obj TYPE REF TO y_check_prefer_new_to_crt_obj. + CREATE OBJECT prefer_new_to_crt_obj. "#EC PREF_NEW +``` + +### Example + +Before the check: + +```abap + DATA prefer_new_to_create_object TYPE REF TO y_check_prefer_new_to_crt_obj. + CREATE OBJECT prefer_new_to_create_object. +``` + +After the check: + +```abap + DATA(prefer_new_to_create_object) = NEW y_check_prefer_new_to_crt_obj( ). +``` + +```abap + DATA prefer_new_to_create_object TYPE REF TO y_check_prefer_new_to_crt_obj. + prefer_new_to_create_object = NEW #( ). +``` + +### Further Readings & Knowledge + +* [ABAP Styleguides on Clean Code](https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-new-to-create-object) From 82d73b6c38a2557939052e92fa108ca0181d3c00 Mon Sep 17 00:00:00 2001 From: Lucas Borin <5233413+lucasborin@users.noreply.github.com> Date: Mon, 5 Apr 2021 16:28:03 -0300 Subject: [PATCH 5/7] Update changelog.txt --- changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.txt b/changelog.txt index 0b020966..e31f1602 100644 --- a/changelog.txt +++ b/changelog.txt @@ -14,6 +14,7 @@ Whenever you upgrade code pal for ABAP, it is highly recommended to execute the 2021-04-** v.1.14.0 ------------------ ++ Prefer NEW to CREATE OBJECT (#283) * In the profile feature, you can add all the missing checks (#346) 2021-04-05 v.1.13.1 From 21cf2461c7f5620f8bc48ca134b411bf8f71ff69 Mon Sep 17 00:00:00 2001 From: estevao-schultz-neto-SAP <63100656+estevao-schultz-neto-SAP@users.noreply.github.com> Date: Tue, 6 Apr 2021 10:54:00 +0200 Subject: [PATCH 6/7] Update prefer-new-to-create-object.md --- docs/checks/prefer-new-to-create-object.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/checks/prefer-new-to-create-object.md b/docs/checks/prefer-new-to-create-object.md index fdaf7a10..e61e8c20 100644 --- a/docs/checks/prefer-new-to-create-object.md +++ b/docs/checks/prefer-new-to-create-object.md @@ -4,11 +4,11 @@ ### What is the Intent of the Check? -Prefer `NEW` to `CREATE OBJECT` as it avoids needlessly longer statements. +Prefer `NEW` over `CREATE OBJECT` as it avoids needlessly longer statements. ### How to solve the issue? -Preferably, use a `NEW`. +Preferably, use `NEW` for creating new objects/instances. ### What to do in case of exception? From fd04531a82401e4cae64686bb0279630535f9ebb Mon Sep 17 00:00:00 2001 From: Lucas Borin <5233413+lucasborin@users.noreply.github.com> Date: Tue, 6 Apr 2021 13:39:45 +0200 Subject: [PATCH 7/7] adding example --- src/examples/y_demo_failures.clas.abap | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/examples/y_demo_failures.clas.abap b/src/examples/y_demo_failures.clas.abap index 2c3d2447..4bb4d4b1 100644 --- a/src/examples/y_demo_failures.clas.abap +++ b/src/examples/y_demo_failures.clas.abap @@ -114,6 +114,7 @@ CLASS y_demo_failures DEFINITION PUBLIC FINAL CREATE PUBLIC. VALUE(age) TYPE i. "#EC RET_NAME #EC BOOL_PARAM "#EC OPTL_PARAM METHODS prefer_is_not_to_not_is. METHODS prefer_case_to_elseif. + METHODS prefer_new_to_create_object. METHODS deprecated_classes. METHODS scope_of_variable. @@ -402,6 +403,12 @@ CLASS Y_DEMO_FAILURES IMPLEMENTATION. ENDMETHOD. + METHOD prefer_new_to_create_object. + DATA demo_failures TYPE REF TO y_demo_failures. + CREATE OBJECT demo_failures. + ENDMETHOD. + + METHOD prefer_is_not_to_not_is. IF NOT attribute_1 IS INITIAL. attribute_1 = attribute_2.