Giter Site home page Giter Site logo

CHECK vs. RETURN about styleguides HOT 9 OPEN

nomssi avatar nomssi commented on June 11, 2024
CHECK vs. RETURN

from styleguides.

Comments (9)

HrFlorianHoffmann avatar HrFlorianHoffmann commented on June 11, 2024 7

While the statement's name CHECK does describe that it evaluates a condition, it unfortunately does not hint at what happens if the condition fails.

Previous discussions suggested that the ABAP community would prefer a name like:

RETURN IF <condition>.

from styleguides.

ilyakaznacheev avatar ilyakaznacheev commented on June 11, 2024 1

CHECK is a beautifully self-describing statement - it does exactly that - checks some condition.
From my perspective, it's ok to use it to execute some initial consistency checks, like empty parameters, validation, etc.

But it's not ok to interrupt the method in other cases.

from styleguides.

fabianlupa avatar fabianlupa commented on June 11, 2024

I like CHECK as the first statement in BAdI implementations. For example to check if the implementation should be called in the current client.

from styleguides.

nomssi avatar nomssi commented on June 11, 2024

To make my point clear: I promote the use of CHECK in a functional method. In this use case, it should not be the first Statement of the method.

from styleguides.

marcellourbani avatar marcellourbani commented on June 11, 2024

IMHO depends on context

A check in the middle of a very long method is frowned upon because it interrupts the flow and is hard to spot.
I agree, but that's mostly a consequence of the method being too long.
In a short one is not a big deal IMHO
At any rate, I will always favour

check a is not initial.

over

if a is initial.
  exit. " or return when equivalent
endif.

The argument about the statement name is not an issue for ABAP developers. Has some merit when a colleague looks at our code

from styleguides.

drakth avatar drakth commented on June 11, 2024

While CHECK is useful in some cases, as people said before, I usually suggest to avoid using it inside LOOPs where the CHECK is used but could be replaced with a LOOP WHERE.

from styleguides.

nomssi avatar nomssi commented on June 11, 2024

I see this In the context of methods, outside LOOPs as in "Replace Nested Conditional with Guard Clauses" (cf. Refactoring (c) 1999 by Martin Fowler:

conditional expressions have different intentions: The first form is a check whether either course is part of the normal behavior. The second (called a guard clause) is for a situation in which one answer .. is normal behavior, the other an unusual condition.

from styleguides.

f4abap avatar f4abap commented on June 11, 2024

Check is something which should never be used in my eyes. Doesn't make sense to me. When I have methods and other stuff done very small (3 to 5 lines only) where is the argument to use a check... a if-condition works always fine.. as stated by @HrFlorianHoffmann a few comments above.

from styleguides.

nomssi avatar nomssi commented on June 11, 2024

CHECK outside of LOOP is mostly used as the first statement in a method. This is an established pattern to enforce the method contract. The extended syntax check will not complain in this case.

Now another usage of CHECK outside LOOPs, verbatim from ABAP Scheme:

CLASS lcl_lisp_number DEFINITION INHERITING FROM lcl_lisp ABSTRACT.
    PUBLIC SECTION.
      METHODS is_exact RETURNING VALUE(result) TYPE REF TO lcl_lisp.
      METHODS is_inexact RETURNING VALUE(result) TYPE REF TO lcl_lisp.
      DATA exact TYPE flag READ-ONLY.
  ENDCLASS.

  CLASS lcl_lisp_number IMPLEMENTATION.

    METHOD is_exact.
      result = false.
      CHECK exact EQ abap_true.
      result = true.
    ENDMETHOD.

    METHOD is_inexact.
      result = true.
      CHECK exact EQ abap_true.
      result = false.
    ENDMETHOD.

  ENDCLASS.

from styleguides.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.