Sub topic 5
Anti-patterns that break the core
This chapter assumes you know what the clean core principle is and what a released API is, both covered earlier in this tutorial. It also assumes you can read basic ABAP, JSON, and Node.js. It covers the practices that make an extension fragile, the failures they cause, and how to find and replace them.
Why anti-patterns matter
An anti-pattern here is a practice that works today but ties your extension to something SAP has not promised to keep stable. SAP changes internal tables, function modules, and screens between releases without notice, because they are not part of any contract. Released APIs and released extension points carry a stability promise.
The costs show up at upgrade time:
- A custom report that reads an internal table fails when SAP restructures that table.
- A modified standard object needs manual adjustment after every update.
- A side-by-side app that calls an internal service stops working when the service changes.
In a cloud edition with frequent updates, these failures arrive often and with little warning. In a private cloud or on-premise system they accumulate until a conversion project becomes expensive.
Anti-pattern 1: modifying SAP standard objects
A modification changes an object that SAP delivered, such as a standard program, class, or data dictionary element. Typical forms are:
- Editing a standard include with a repair or access key.
- Adding code through implicit enhancement spots that SAP never released.
- Changing a standard table by appending fields in ways SAP does not support for that object.
- Patching delivered screens, texts, or authorization checks.
Every upgrade must then reconcile your change with SAP's new version. In classic systems this is the SPAU and SPDD adjustment work. Where modification is not possible at all, as in SAP S/4HANA Cloud Public Edition, you cannot do it in the first place.
The fix is to use an extension point SAP released for the purpose, such as a released BAdI, a custom field through key user tools, or a custom business object. If no extension point exists for your requirement, build the logic outside the core and connect through a released API or event.
Anti-pattern 2: using unreleased objects
Calling or reading an object that SAP has not released is the most common anti-pattern, and it is easy to commit. The object is visible, it works in testing, and no tool stops you in classic ABAP.
Common cases:
SELECTstatements on standard tables such asKNA1orVBAK.- Calls to function modules or classes that have no release contract.
- Use of internal CDS views whose names look public but are not released.
- Calls to BAPIs or RFCs that have not been released for your purpose.
The next two code blocks show the difference. The first reads the customer master table directly. In an ABAP Cloud development environment, the syntax check rejects this, because KNA1 is not released.
CLASS zcl_customer_name_bad DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_customer_name_bad IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
" Anti-pattern: direct access to an unreleased standard table
SELECT SINGLE name1 FROM kna1
WHERE kunnr = '0000000001'
INTO @DATA(name).
out->write( name ).
ENDMETHOD.
ENDCLASS.The second reads the same kind of information through the released CDS view I_BusinessPartner, which has a stability contract.
CLASS zcl_partner_name DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_partner_name IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
SELECT SINGLE BusinessPartnerFullName
FROM I_BusinessPartner
WHERE BusinessPartner = '0000000001'
INTO @DATA(name).
out->write( name ).
ENDMETHOD.
ENDCLASS.The business partner ID 0000000001 is a placeholder. Replace it with a value that exists in your system.
Anti-pattern 3: copying standard code
A "Z-copy" is a custom copy of a standard program, class, or transaction that you then change. It avoids modifying the original, but the copy still depends on the unreleased objects the original used. It also stops receiving SAP's fixes, so legal and tax updates, security patches, and new features bypass it. Copied code often carries a hidden dependency on dozens of internal objects.
If you need different behavior, look for a released extension point first. If none exists, write a new implementation against released APIs only.
Anti-patterns in side-by-side extensions
Moving code to SAP Business Technology Platform (BTP) does not make an extension clean. The same mistakes appear in different forms.
| Anti-pattern | Why it breaks the core | Cleaner alternative |
|---|---|---|
| Calling internal OData services (for example, the backend of a Fiori app) | These services are built for one UI and can change with it | Consume a released OData API |
| Reading the S/4HANA database directly through a database connection | Bypasses authorization, business logic, and API stability | Call a released API or use a released data extraction method |
| Copying large parts of core data into an extension database | Creates duplicate data that drifts out of sync and raises compliance questions | Read on demand through a remote service, and store only extension-owned data |
| Scraping the UI or simulating user sessions | Breaks with any screen change | Use an API |
| Embedding a URL and a password in application code | Leaks secrets and makes landscape changes a code change | Use a BTP destination |
| One technical user with broad authorizations for all calls | Over-privileged, and the audit trail loses the real user | Use a least-privilege communication arrangement and propagate the user where needed |
The last two rows are easy to demonstrate in a CAP project. This configuration hardcodes the host and credentials:
{
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"kind": "odata-v2",
"model": "srv/external/API_BUSINESS_PARTNER",
"credentials": {
"url": "https://my-s4.example.com/sap/opu/odata/sap/API_BUSINESS_PARTNER",
"authentication": "BasicAuthentication",
"username": "SUPERUSER",
"password": "Welcome1"
}
}
}
}
}Each environment needs a code change, the password sits in source control, and the user probably has far more rights than the app needs. The version from the earlier chapters on destinations replaces the url, username, and password entries with a destination name and a path. The host and credentials then live in the BTP Destination service, where an administrator manages them per environment.
How to detect anti-patterns
You do not have to find these by reading code. Use these checks:
- Release status. Check each API or object in SAP's API documentation and in the ABAP development tools. Look for a release state and a release contract.
- ABAP Test Cockpit (ATC). Run the cloud readiness and clean core check variants. They flag use of unreleased objects, with findings tied to the clean core level you target.
- ABAP language version. Developing in ABAP for Cloud Development makes the compiler enforce released-only access, so most violations fail at syntax check time.
- Code review of side-by-side projects. Search configuration and code for hardcoded hosts, embedded credentials, and service paths that are not in the released API list.
The following diagram gives a quick triage for any dependency you find.
Fixing existing anti-patterns
Treat remediation as a backlog, not a single project:
- Rank findings by upgrade risk and business criticality. Modifications and unreleased objects in central processes come first.
- Replace each one with the nearest released API, extension point, or side-by-side pattern.
- If no released alternative exists, record the exception with an owner and a review date. Check whether SAP has since released a replacement.
- Add the ATC check to your delivery pipeline so new violations fail the build.
The goal is not zero custom code. The goal is that every dependency of your custom code is on a released contract.
Exercise
A team builds a CAP app on BTP. It reads sales order data by calling the OData service behind a standard Fiori app, because that service already returns the fields they need. The destination and authentication are set up correctly. Is this clean core compliant? Explain, and say what the team should do.
Show answer
No. Correct connectivity does not make the dependency clean. The service behind a Fiori app is built for that UI and has no stability contract, so SAP can change it in an update and break the CAP app. The team should look for a released OData API that covers the fields, such as a released sales order API, and consume it through the destination. If the fields are missing, they should request an API enhancement from SAP or use a released extension to expose a custom field through a released API, not keep the internal service.