Skip to content

Prefer New to Create Object #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* Standard functions in Prefer IS NOT to NOT IS (#338)
* In the profile feature, you can add all the missing checks (#346)

Expand Down
1 change: 1 addition & 0 deletions docs/check_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions docs/checks/prefer-new-to-create-object.md
Original file line number Diff line number Diff line change
@@ -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` over `CREATE OBJECT` as it avoids needlessly longer statements.

### How to solve the issue?

Preferably, use `NEW` for creating new objects/instances.

### 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)
45 changes: 45 additions & 0 deletions src/checks/y_check_prefer_new_to_crt_obj.clas.abap
Original file line number Diff line number Diff line change
@@ -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.
68 changes: 68 additions & 0 deletions src/checks/y_check_prefer_new_to_crt_obj.clas.testclasses.abap
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions src/checks/y_check_prefer_new_to_crt_obj.clas.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOCLASS>
<CLSNAME>Y_CHECK_PREFER_NEW_TO_CRT_OBJ</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>Prefer New to Create Object</DESCRIPT>
<STATE>1</STATE>
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
</VSEOCLASS>
</asx:values>
</asx:abap>
</abapGit>
7 changes: 7 additions & 0 deletions src/examples/y_demo_failures.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down