From `CREATE OBJECT` to `NEW`: Modernizing Your ABAP Code
For years, ABAP developers have relied on the trusty CREATE OBJECT
statement to bring their classes to life. It's reliable, it's familiar, but with the evolution of ABAP towards a more modern, expressive syntax (starting with ABAP 7.40), a new and more powerful constructor operator has taken center stage: the NEW
operator.
If you're looking to write cleaner, more concise, and more readable code, understanding and adopting the NEW
operator is a crucial step. Let's dive deep into what it is, how it works, and why you should be using it.
The Old Way: CREATE OBJECT
Let's first look at the traditional way of instantiating an object. This typically involves two distinct steps: declaring the reference variable and then creating the object instance.
The Modern Way: The NEW
Operator
The NEW
operator streamlines this process significantly. It allows you to create an object instance and assign it to a reference variable in a single, fluid statement. The most powerful feature is its ability to be used with an inline declaration (DATA(...)
), which infers the data type automatically.
Notice how two lines (and sometimes more) are condensed into a single, easy-to-read line. This is the core advantage of the NEW
operator.
In-Depth Usage and Examples
The NEW
operator is more than just a shortcut. It enables several powerful programming patterns.
1. Passing Parameters to the Constructor
With CREATE OBJECT
, you passed parameters to the constructor using the EXPORTING
addition. With NEW
, it feels much more like calling a regular method, making the code more intuitive.
Imagine our ZCL_VEHICLE
class has a constructor that accepts the number of wheels.
2. Inline Instantiation in Method Calls
You can create a new object instance directly within a method's parameter list. This is extremely useful for passing newly created objects to other methods without needing an intermediate variable.
3. Method Chaining
One of the most elegant features enabled by NEW
is method chaining. You can instantiate an object and immediately call one of its methods in the same statement. The result of the method call can then be assigned to a variable.
Let's say our ZCL_VEHICLE
class has a method GET_DESCRIPTION( )
that returns a string.
4. Creating Tables of Objects
The NEW
operator can also be used with the VALUE
operator to construct internal tables of objects in a very clean way.
Conclusion: Why You Should Make the Switch
Adopting the NEW
operator is a clear win for any ABAP developer working on a modern SAP system. The benefits are undeniable:
- Conciseness: It drastically reduces the number of lines needed to instantiate objects.
- Readability: The syntax is more intuitive and aligns with modern object-oriented languages.
- Power: It enables powerful patterns like method chaining and streamlined table construction.
- Efficiency: Combined with inline declarations (
DATA()
), it lets the compiler do the work of type inference, leading to faster development.
While CREATE OBJECT
still works and isn't going away, the NEW
operator is the modern, preferred, and superior way to create objects in ABAP. Make it a part of your daily coding practice to write cleaner, more effective, and more maintainable programs.
No comments:
Post a Comment