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

More of Jerry’s original articles can be found in “Wang Zixi” :