This is Jerry’s 10th article of 2021, and the 281st original article of Wang Zixi’s official account.
Today is Sunday, January 17, 2021, the fifth day of the twelfth lunar month.
Jerry had received a New Year’s gift from the CSDN community, a desk calendar called the Great Programmer,
Tony Hoare, a computer scientist, won the Turing Prize in 1980 for his contributions to the definition and design of programming languages. Inventor of the quicksort algorithm.
If you want to see the source code for the eight sorting algorithms implemented with ABAP, check out Jerry’s previous article:
Jerry’s 2017 May Day Holiday: ABAP implementations of eight classical sorting algorithms
Instead of discussing Hall’s quicksort algorithm, this article introduces another design from Hall that is still widely used by programmers in programming languages today: the NULL reference.
Null references are what Hall calls the “billion dollar mistake” and were introduced by Hall in 1965 when he designed the ALGOL W language.
As mentioned in Java In Action, the use of NULL in Java program development brings a variety of theoretical and practical problems:
- It is the mother of error
- It will inflate your code
- They mean nothing in themselves
- Broke the Philosophy of Java
- Cracked a hole in Java’s type system
Hall’s famous quote:
I call it my “billion dollar mistake” for inventing empty quotes in 1965… I can’t resist putting in an empty reference, simply because it’s so easy to implement.
Programming languages that introduce empty references need to explicitly check whether a reference is valid before they can be accessed.
Java
The print method defined in line 46 below takes a reference of type Integer as an input parameter. Before invoking a reference, you need to determine whether it is an empty reference, or the program will execute with a runtime exception.
ABAP
Use CHECK X IS NOT INITIAL for defense. If X IS an empty reference, the next statement of the CHECK statement will NOT be executed.
On the basis of Professor Holl’s null reference, rigorous Germans devised IS BOUND, IS NOT INITIAL and IS ASSIGNED:
SAP ABAP IS BOUND, IS NOT INITIAL and IS ASSIGNED
JavaScript
Inside the print method on line 10, the short-circuit feature of the && operator is used to detect empty references: if the oPrinter passed is an empty reference, the print call following && is not executed.
TypeScript provides Optional Chaining that elegantly avoids this problem at the language level.
The following TypeScript code constructs an optional chain using question marks. If a is empty, then the expression a? .b returns undefined directly to val without attempting to execute a.b
We can think of TypeScript’s optional chains as syntactic sugar that JavaScript implements with ternary expressions.
To relieve Java programmers of the burden of explicitly checking for non-null references every time they use them, Java 8 introduces a new utility class: Optional.
Optional is just a wrapped class without any business logic, and its value field points to the real business class.
Below is an example of using the Optional utility class, the filter method on line 11, which passes in a filter condition implemented through Lambda Function. AnotherName = anotherName = anotherName = anotherName = anotherName = anotherName = anotherName = anotherName = anotherName = anotherName = anotherName = anotherName = anotherName
The optional. filter method returns Optional regardless of whether the filter criteria are met, facilitating chain calls.
I pass in the Optional string at line 10, which is obviously much longer than 6, so the filter method returns a new Optional object with a null value field.
For the Optional object returned by the filter call, we can continue to call orElse and set a default value. If the value field of the shortName package is empty, the default value passed in by the orElse method is returned.
Java 8’s Optional utility class is not like TypeScript’s Optional chain, which is a language-level feature, but Optional is just a tool class in the development package.
For example, the Optional static method of simply creates a new Optional object that wraps the value reference passed in:
The internal implementation of the orElse method is also a simple ternary expression.
Look at an extreme example:
Class Outer has a field of nested type. The Nested class has a field inner of type inner. The Inner class contains the field foo of type String and value Jerry:
If you want to start with an instance of Outer and write a robust piece of code that prints the foo field buried deep in the Inner class, which one would you prefer, using the Optional method and the regular one in the test1 and test2 methods below?
It is worth noting that there is language-based support for methods like Java Optional.orElse in ABAP.
Here is the new syntax for ABAP 740:
The new syntax above, translated into traditional ABAP code, looks like this:
As a result, the new ABAP inner table read syntax is relatively concise and can write 3 lines less code.
If there is no record of object_ext with the value cl_CRM_PRODIL_BO_names =>gc_prod_root in the IT_data table, execution of the program will be terminated and an exception CX_SY_ITAB_LINE_NOT_FOUND will be raised:
Of course, ABAP also has corresponding solutions for this situation.
The following test code throws an exception on line 17, but not on line 19. Semantically easy to understand: if there is no record with name Spring2 in the inner table LT_data, a structure specified by the developer using the DEFAULT VALUE keyword is returned as the DEFAULT VALUE.
After line 19, the ls3 structure’s name field is SpringInvalid and value is 999.
In this article, we share Jerry’s experience in dealing with null references in his work from professor Hall’s 1965 null references. Thank you for reading.
ABAP project
- Jerry’s ABAP, Java and JavaScript stew
- What should ABAP developers learn in the future
- Jerry’s 2017 May Day holiday:ABAP implementation of 8 classical sorting algorithms
- Jerry’s collection of original ABAP technical articles
- Three hundred lines of ABAP code implement a simplest blockchain prototype
- Call functions in ABAP on-premise system using Java+SAP Cloud Platform +SAP Cloud Connector
- Consume ABAP on-premise OData services in the CloudFoundry environment of SAP cloud platform
- ABAP vs Java, breaststroke vs freestyle
- Talk about C and ABAP
- Use ABAP Channel to develop some small tools to improve daily work efficiency
- All the boring things I’ve done with ABAP
- Don’t like SAP GUI?Try using Eclipse for ABAP development
- Write and activate ABAP Code using Visual Studio Code
- Has your ABAP program been opened to Buddha?Let’s try Jerry’s trick
- Write the first ABAP program in SAP cloud platform ABAP programming environment
- ABAP programming specification issued by SAP
- Do you know the hidden features of ABAP Code Inspector?
- Are you using ABAP for secondary development of SAP products? Learn about this new concept of secondary development
- The parasitic programming language inside ABAP Netweaver
- Start with a blog post on the SAP community to talk about the sentiment behind SAP product naming
- Cloud ABAP Restful service development
- How to expose CDS View as OData service in SAP cloud platform ABAP programming environment
- Use abapGit to transfer code between the ABAP ON-premises system and the SAP cloud platform ABAP environment
- 30 minutes To develop a Fiori application with Restful ABAP Programming model to support add, delete, change, and query
- Jerry walks you through the implementation of Action and Validation, part 2 of the Restful ABAP Programming Model series
- Jerry takes you through this series of Restful ABAP Programming models:Cloud ABAP application debugging
- How to consume third-party services in ABAP programming environment on SAP cloud platform
- It’s time for ABAP developers to go to the cloud – now you can use a free trial version of the ABAP environment for SAP cloud Platform
- The origin and application scenario of ABAP programming environment on SAP cloud platform
- Trident application in SAP cloud platform
- How to develop and deploy a Fiori application that supports add, delete, modify and query based on Restful ABAP Programming model
- SAP 2019 TechEd Key Note: How do SAP practitioners do secondary development in the cloud era?
- What ABAP keywords and syntax won’t work in the ABAP cloud environment?
- The ABAP development environment finally supports automatic formatting of ABAP variable names with camel name
- Complete a real work task using ABAP 740’s new REDUCE keyword
- A shiver of ABAP code
- Yesterday’s Halloween ABAP monster code mystery, revealed the answer
- This paper introduces an efficient copy method of inner table in ABAP kernel state
- A practical example of developing OData using the SAP Cloud Application Programming model
- When ABAP met Prometheus
- Draw scalable vector diagrams using ABAP
- ABAP development environment syntax highlighting things
- Seven weapons for SAP error message debugging:Enable all error messages to be located
- Several methods of using ABAP to operate Excel
- SAP GUI in the favorites transaction code management tool
- SAP GUI and Windows registry
- Can you do bad things with Debug permission? Be careful. Your every move is on the system
- ABAP CCDEF, CCIMP, CCMAC, CCAU, CMXXX
- Three ways to implement ABAP conditional breakpoints
- Use SAT trace to monitor the performance and call stack of SAP applications opened from the browser
- Advice from a 13-year ABAP veteran: Knowing the basics will never hurt ABAP development
- SAP ABAP Netweaver containerization, impossible task?
- SAP Product enhancement Technology Review
- SAP API development methods
- Brief introduction to Java and SAP ABAP static proxy and dynamic proxy, and ABAP tangential programming attempt
- SAP ABAP application server HTTP response Status Code
- Is there a Java List collection utility class in SAP ABAP? CL_OBJECT_COLLECTION take a look
- ABAP interview question series: Write an ABAP procedure that creates a Deadlock
- This section describes how to log in to the SAP ABAP Netweaver server
- SAP ABAP keyword syntax diagram and ABAP Code Composer automatic generation tool
- Alternative use of SAP ABAP SM50 – Detection of database table reads by ABAP worker processes
- SAP ABAP character variable and string variable character number of a knowledge point, and a blood case
- SAP ABAP IS BOUND, IS NOT INITIAL and IS ASSIGNED
- Weakreferences and Soft References in SAP, ABAP and Java
- Introduction to SAP AMDP – ABAP hosted HANA database process
- Tag your ABAP object
More of Jerry’s original articles can be found in “Wang Zixi” :