0% found this document useful (0 votes)
22 views

SOLID ABAP

Solid ABAPSolid ABAPSolid ABAP Solid ABAPSolid ABAPSolid ABAP Solid ABAPSolid ABAPSolid ABAP Solid ABAPSolid ABAPSolid ABAP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

SOLID ABAP

Solid ABAPSolid ABAPSolid ABAP Solid ABAPSolid ABAPSolid ABAP Solid ABAPSolid ABAPSolid ABAP Solid ABAPSolid ABAPSolid ABAP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

The SOLID Pathway to

Robust ABAP Code


APRIL 2024

EPAM Proprietary & Confidential. 1


BaibaBlumberga
10+ years of profound ABAP experiece

I have seen SAPScripts


Have done some «dirty code» that always hunts you down
Clean code with nice design makes me happy

EPAM Proprietary & Confidential. 2


What Is The Problem?
Do We Really Need To Change The Way We Are Doing Things?

EPAM Proprietary & Confidential. 3


WHAT IS PROBLEM?

Rotting Design
Symptoms:

Rigidity Fragility
the tendency of the software to be difficult to the tendency of the software to break in many places
change every time it is changed

Immobility Viscosity
the inability to reuse the software from other it is easy to do the wrong thing (the hack) but hard to
projects do the right one (preserve the design)

Design Principles and Design Patterns (2000) by Robert C. Martin (Uncle Bob)
EPAM Proprietary & Confidential. 4
WHAT IS PROBLEM?

What Makes It Rot

Incomplete
requirements

Changing requirements
Dependencies
Fast-changing
world

Rotting design

Design Principles and Design Patterns (2000) by Robert C. Martin (Uncle Bob)
EPAM Proprietary & Confidential. 5
WHAT IS PROBLEM?

How To Prevent Rotting

Design Patterns

Design Principles
Creational

Object-Oriented Concepts
Fundamental
Structural

Inheritance Encapsulation Polymorphism Abstraction


SOLID
Behavioral

EPAM Proprietary & Confidential. 6


Design Principles
Fundamental Guidelines That Guide Your Class Structure And Relationships

EPAM Proprietary & Confidential. 7


DESIGN PRINCIPLES

Design Principles

Encapsulate what varies Single Responsibility Principle

Open/Closed Principle
Favor composition
Liskov Substitution Principle
Program to interface
Interface Segregation Principle
Loose coupling Dependency Inversion Principle

Design Principles and Design Patterns (2000) by Robert C. Martin (Uncle Bob)
EPAM Proprietary & Confidential. 8
DESIGN PRINCIPLES

Fundamental Principles

Encapsulate what varies


Isolate the aspects of a code that are prone to
change, harnessing them within separate classes
or modules.

EPAM Proprietary & Confidential. 9


FUNDAMENTAL PRINCIPLES

Encapsulate What Varies

METHOD main.

IF order_type = 'Cash’
DATA(order) = NEW zcash_sale_order( ).
ELSEIF order_type = 'Third_party’
order = NEW zthird_party_order( ). Code that is prone to changes.
ELSEIF order_type = 'Rush’
order = NEW zrush_order( ).
ENDIF.

order.prepare( ).
order.run_schedule_lines( ).
order.run_pricing( ).

ENDMETHOD.

EPAM Proprietary & Confidential. 10


FUNDAMENTAL PRINCIPLES

Encapsulate What Varies

CLASS z_order_factory DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
CLASS-METHODS create_order METHOD main.
IMPORTING order_type TYPE string DATA(order) = z_order_factory=>create_order( order_type ).
RETURNING VALUE(order) TYPE REF TO z_sales_order.
PROTECTED SECTION.
order.prepare( ).
PRIVATE SECTION. order.run_schedule_lines( ).
ENDCLASS. order.run_pricing( ).
ENDMETHOD.
CLASS z_order_factory IMPLEMENTATION.
METHOD createorder.
IF order_type = 'Cash’
order = NEW zcash_sale_order( ).
ELSEIF order_type = 'Third_party’
order = NEW zthird_party_order( ).
ELSEIF order_type = 'Rush’
order = NEW zrush_order( ).
ENDIF.
ENDMETHOD.
ENDCLASS.

EPAM Proprietary & Confidential. 11


DESIGN PRINCIPLES

Fundamental Principles

Favor composition over


inheritance
Instead of IS-A use HAS-A.
Identify the aspects of your application that vary
and separate them from what stays the same.

EPAM Proprietary & Confidential. 12


FUNDAMENTAL PRINCIPLES

Favor Composition Over Inheritance

As the requirements
increase, inheritance can
expand both horizontally
and vertically, which can
potentially lead to increased
code rigidity.

EPAM Proprietary & Confidential. 13


FUNDAMENTAL PRINCIPLES

Favor Composition Over Inheritance

With composition, code


becomes more flexible to
future requirements.

EPAM Proprietary & Confidential. 14


DESIGN PRINCIPLES

Fundamental Principles

Loose Coupling
Components should be built independently with
minimal dependencies on each other to ensure
flexibility and maintainability, as well as decrease
the risk of potential ripple effects during
modifications.

EPAM Proprietary & Confidential. 15


FUNDAMENTAL PRINCIPLES

Loose Coupling

Tightly Coupled
The order depends on the
concrete implementation of the
Printer class.

EPAM Proprietary & Confidential. 16


FUNDAMENTAL PRINCIPLES

Loose Coupling

Loose Coupling
The order does not know
concrete implementations, it
works with the Output interface.

EPAM Proprietary & Confidential. 17


DESIGN PRINCIPLES

Fundamental Principles

Program to Interface,
not Implementation
Recommendation to code against abstract
interfaces or base classes instead of specific
classes or implementations, to maximize
flexibility and ensure interchangeability of
objects

EPAM Proprietary & Confidential. 18


FUNDAMENTAL PRINCIPLES

Program To Interface, Not Implementation

The report works against the


concrete implementation of
the ALV table class.

EPAM Proprietary & Confidential. 19


FUNDAMENTAL PRINCIPLES

Program To Interface, Not Implementation

The report usage is more


flexible without changing the
controller code.

EPAM Proprietary & Confidential. 20


DESIGN PRINCIPLES

SOLID Principles

Single Responsibility
Principle
The class should have just one specific
responsibility or reason to change, ensuring
simplicity and improved maintainability in
software design.

EPAM Proprietary & Confidential. 21


SOLID PRINCIPLES

Single Responsibility Principle

CLASS lcl_document_handler DEFINITION.

PUBLIC SECTION.
METHODS prepare.

ENDCLASS. The document class is


also responsible for
CLASS lcl_document_handler IMPLEMENTATION.
authentification object
METHOD prepare. creation.
DATA(authenticator) = new z_authenticator( 'Z_DOC' ).
authenticator->is_prepare_allowed( ).
"do document preparation
ENDMETHOD.

ENDCLASS.

EPAM Proprietary & Confidential. 22


SOLID PRINCIPLES

Single Responsibility Principle


CLASS lcl_document_handler DEFINITION.
PUBLIC SECTION.
METHODS constructor
IMPORTING autheticator TYPE REF TO zif_authenticator.
METHODS prepare.
Authentication object
PRIVATE SECTION. creation is taken out of
DATA authenticator TYPE REF TO zif_authenticator
the document class, the
ENDCLASS. document class works
CLASS lcl_document_handler IMPLEMENTATION. against the interface and
is only responsible for
METHOD constructor.
me->authenticator = authenticator. document preparation.
ENDMETHOD.

METHOD prepare.
authenticator->is_prepare_allowed( ).
"do document preparation
ENDMETHOD.

ENDCLASS.

EPAM Proprietary & Confidential. 23


DESIGN PRINCIPLES

SOLID Principles

Open/Closed Principle
The class should be open for extension but
closed for modification, thus promoting code
extensibility without risking the existing
functionality

EPAM Proprietary & Confidential. 24


SOLID PRINCIPLES

Open/Closed Principle

CLASS lcl_salesorder DEFINITION.


PUBLIC SECTION.
METHODS: create
IMPORTING document_type TYPE auart.
ENDCLASS.

CLASS lcl_salesorder IMPLEMENTATION. Any new requirement of


METHOD create.
" Common logic for a sales document the existing document
CASE document_type.
WHEN 'OR’. type or a new document
" Code for Standard Order specific process
WHEN 'RE’.
type would require a
" Code for Returns specific process change in the create
WHEN 'CM’.
" Code for Credit memo specific process method.
" And possibly more WHEN clauses as more document types are introduced
ENDCASE.
ENDMETHOD.
ENDCLASS.

EPAM Proprietary & Confidential. 25


SOLID PRINCIPLES

Open/Closed Principle CLASS lcl_standard_order DEFINITION INHERITING


FROM lcl_sales_order.
PROTECTED SECTION.
METHODS: check_data REDEFINITION,
post_document REDEFINITION,
INTERFACE lif_sales_doc. update_status REDEFINITION.
METHODS: create. ENDCLASS.
ENDINTERFACE. CLASS lcl_standard_order IMPLEMENTATION.
METHOD check_data.
CLASS lcl_sales_order DEFINITION ABSTRACT. " Code for Standard Order specific process
PUBLIC SECTION. ENDMETHOD.
INTERFACES lif_sales_doc.
PROTECTED SECTION.
METHOD post_document.
" Code for Standard Order specific process The create method
ENDMETHOD.
METHODS: check_data ABSTRACT,
post_document ABSTRACT, METHOD update_status. remains the same for all
" Code for Standard Order specific process
ENDCLASS.
update_status ABSTRACT.
ENDMETHOD. document types and is
ENDCLASS.
CLASS lcl_sales_order IMPLEMENTATION.
easy to extend with new
CLASS lcl_returns DEFINITION INHERITING
METHOD lif_sales_doc~create.
" Common logic for a sales document
FROM lcl_sales_order. document types.
PROTECTED SECTION.
check_data( ). METHODS: check_data REDEFINITION,
post_document( ). post_document REDEFINITION,
update_status( ). update_status REDEFINITION.
ENDMETHOD. ENDCLASS.
ENDCLASS. CLASS lcl_returns IMPLEMENTATION.
METHOD check_data.
" Code for Return Order specific process
ENDMETHOD.
METHOD post_document.
" Code for Return Order specific process
ENDMETHOD.
METHOD update_status.
" Code for Return Order specific process
ENDMETHOD. EPAM Proprietary & Confidential. 26
ENDCLASS.
DESIGN PRINCIPLES

SOLID Principles

Liskov Substitution
Principle
A subtype must be substitutable for its base type
without altering the correctness of the program
while ensuring consistency.

EPAM Proprietary & Confidential. 27


SOLID PRINCIPLES

Liskov Substitution Principle

EPAM Proprietary & Confidential. 28


SOLID PRINCIPLES

Liskov Substitution Principle


CLASS lcl_material DEFINITION.
PUBLIC SECTION.
METHODS: save
RETURNING VALUE(result) type string.
ENDCLASS.

CLASS lcl_material IMPLEMENTATION.


METHOD save.
result = 'Standard Material Saved.’.
ENDMETHOD.
In this case, the
ENDCLASS. substitute class has
CLASS lcl_haz_material DEFINITION INHERITING FROM lcl_material.
PUBLIC SECTION.
additional behavior –
METHODS: save REDEFINITION. exception, what is not
PRIVATE SECTION.
DATA HazardousMaterialChecked TYPE c. expected from the
ENDCLASS.

CLASS lcl_haz_material IMPLEMENTATION.


base class.
METHOD save.
IF HazardousMaterialChecked = 'X’.
result = 'Hazardous Material Saved.’.
ELSE.
RAISE EXCEPTION TYPE cx_abap_invalid_value
EXPORTING value = 'Hazardous Material Not Checked Properly.’.
ENDIF.
ENDMETHOD.
ENDCLASS.

EPAM Proprietary & Confidential. 30


SOLID PRINCIPLES

Liskov Substitution Principle

METHOD if_oo_adt_classrun~main.
out->write(
EXPORTING
data = NEW lcl_material( )->save( )
name = 'Material’
).
ENDMETHOD.
Since both classes yield
different results, it
indicates that they are
METHOD if_oo_adt_classrun~main.
not substitutable.
out->write(
EXPORTING
data = NEW lcl_haz_material( )->save( )
name = ‘HazMaterial’
).
ENDMETHOD.

EPAM Proprietary & Confidential. 31


DESIGN PRINCIPLES

SOLID Principles

Interface Segregation
Principle
Clients should depend on client-specific
interfaces with only the set of operations they
require.

EPAM Proprietary & Confidential. 32


SOLID PRINCIPLES

Interface Segregation Principle

INTERFACE lif_material_master.
Even for materials that
METHODS: create, don't need certain
read,
update, methods, they would
delete, still be forced to
calculate_weight,
depreciate. implement them,
ENDINTERFACE. potentially providing
CLASS lcl_material DEFINITION. consumers with
PUBLIC SECTION.
INTERFACES: lif_material_master.
unnecessary features
ENDCLASS. or functionality.

EPAM Proprietary & Confidential. 33


SOLID PRINCIPLES

Interface Segregation Principle


INTERFACE lif_material_master_crud.
METHODS: create,
read,
update,
delete.
ENDINTERFACE.

INTERFACE lif_material_master_calculate.
The operations are split
METHODS: calculate_weight. into more focused/cohesive
ENDINTERFACE.
interfaces. Class does not
INTERFACE lif_material_master_depreciate.
METHODS: depreciate. depend on methods it does
ENDINTERFACE.
not use.
CLASS lcl_material DEFINITION.
PUBLIC SECTION.
INTERFACES: lif_material_master_crud.
ENDCLASS.

CLASS lcl_special_material DEFINITION.


PUBLIC SECTION.
INTERFACES: lif_material_master_crud,
lif_material_master_calculate,
lif_material_master_depreciate.
ENDCLASS.

EPAM Proprietary & Confidential. 34


DESIGN PRINCIPLES

SOLID Principles

Dependency Inversion
Principle
High-level modules should not depend on low-
level modules; instead, both should depend on
abstractions.

EPAM Proprietary & Confidential. 35


SOLID PRINCIPLES

Dependency Inversion Principle


CLASS lcl_email DEFINITION.
PUBLIC SECTION.
METHODS: send IMPORTING recipient TYPE string
subject TYPE string
body TYPE string.
ENDCLASS.

CLASS lcl_email IMPLEMENTATION.


METHOD send.
" code to send an email
The higher-level
ENDMETHOD. employee class is
ENDCLASS.
directly dependent on
CLASS lcl_employee DEFINITION.
PUBLIC SECTION. the lower-level email
METHODS: notify IMPORTING message TYPE string.
PRIVATE SECTION. class.
DATA: email TYPE REF TO lcl_email.
ENDCLASS.

CLASS lcl_employee IMPLEMENTATION.


METHOD notify.
email->send( recipient = '[email protected]
subject = 'Notification’
body = message ).
ENDMETHOD.
ENDCLASS.

EPAM Proprietary & Confidential. 36


SOLID PRINCIPLES

Dependency Inversion Principle


INTERFACE lif_notification.
METHODS: send IMPORTING recipient TYPE string
subject TYPE string
message TYPE string.
ENDINTERFACE.

CLASS lcl_email DEFINITION.


PUBLIC SECTION.
INTERFACES: if_notification. In this scenario, both
ENDCLASS.
the employee class and
CLASS lcl_email IMPLEMENTATION.
METHOD if_notification~send. email class depend on
" code to send an email.
ENDMETHOD. the notification
ENDCLASS.
interface. It allows
CLASS lcl_employee DEFINITION.
PUBLIC SECTION. greater flexibility and
METHODS: notify IMPORTING message TYPE string.
PRIVATE SECTION. easier testing.
DATA: notification TYPE REF TO lif_notification.
ENDCLASS.

CLASS lcl_employee IMPLEMENTATION.


METHOD notify.
notification->send( recipient = '[email protected]
subject = 'Notification’
message = message ).
ENDMETHOD.
ENDCLASS.
EPAM Proprietary & Confidential. 37
SOLID PRINCIPLES

Dependency Inversion Principle

Dependencies are inverted.

EPAM Proprietary & Confidential. 38


Costs
Does It Cost Too Much To Invest Time In Design?

EPAM Proprietary & Confidential. 39


COSTS

Technical Debt
The implied cost of future reworking is required when choosing an easy but limited solution instead of a better
approach that can take more time /Wikipedia/

Actual CoC
Cost Of Change (CoC)

Technical
Debt

Ideal CoC

Time

EPAM Proprietary & Confidential. 40


COSTS

Cost Of Defect In Software Lifecycle


30X

15X

7X

3X

1X
Development
Requirments Design / Coding Testing Deployment cycle
Architecture

The Economic Impacts of Inadequate Infrastructure for Software Testing, National Institute of Standards and Technology, 2022

EPAM Proprietary & Confidential. 41


COSTS

Relative Cost Of Fixing Defects


100X

15X

6.5X

1X
Development
Design Implemantation Testing Maintenance cycle
IBM System Science Institute Relative Cost of Fixing Defects
EPAM Proprietary & Confidential. 42
”Any fool can write code that a
computer can understand. Good
programmers write code that
humans can understand.”
Martin Fowler

EPAM Proprietary & Confidential. 43


Baiba Blumberga
[email protected]

EPAM Proprietary & Confidential. 44

You might also like