Void safety

Last updated

Void safety (also known as null safety) is a guarantee within an object-oriented programming language that no object references will have null or void values.

Contents

In object-oriented languages, access to objects is achieved through references (or, equivalently, pointers). A typical call is of the form:

x.f(a, ...)

where f denotes an operation and x denotes a reference to some object. At execution time, however, a reference can be void (or null). In such cases, the call above will be a void call, leading to a run-time exception, often resulting in abnormal termination of the program.

Void safety is a static (compile-time) guarantee that a void call will never arise.

History

In a 2009 talk, Tony Hoare traced the invention of the null pointer to his design of the ALGOL W language and called it a "mistake":

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. [1]

Bertrand Meyer introduced the term "void safety". [2]

In programming languages

An early attempt to guarantee void safety was the design of the Self programming language.

The Eiffel language is void-safe according to its ISO-ECMA standard; the void-safety mechanism is implemented in EiffelStudio starting with version 6.1 and using a modern syntax starting with version 6.4.

The Spec# language, a research language from Microsoft Research, has a notion of "non-nullable type" addressing void safety. The F# language, a functional-first language from Microsoft Research running on .NET framework, is void-safe except when interoperating with other .NET languages. [3]

Null safety based in union types

Since 2011 several languages support union types and intersection types, which can be used to detect possible null pointers at compiling time, using a special class Null of which the value null is its unique instance.

The null safety based in types appeared first in the Ceylon, followed soon by TypeScript.

The C# language implements compile-time null safety check since version 8. However, to stay compatible with older versions of the language, the feature is opt-in on a per project or per file basis. [4]

The Google's Dart language implements it since its version 2.0, in August 2018 [5] [6]

Other languages that use null-safe types by default include JetBrains' Kotlin, [7] Rust, [8] and Apple's Swift.

See also

Related Research Articles

Eiffel is an object-oriented programming language designed by Bertrand Meyer and Eiffel Software. Meyer conceived the language in 1985 with the goal of increasing the reliability of commercial software development; the first version becoming available in 1986. In 2005, Eiffel became an ISO-standardized language.

Pascal is an imperative and procedural programming language, designed by Niklaus Wirth as a small, efficient language intended to encourage good programming practices using structured programming and data structuring. It is named after French mathematician, philosopher and physicist Blaise Pascal.

<span class="mw-page-title-main">Design by contract</span> Approach for designing software

Design by contract (DbC), also known as contract programming, programming by contract and design-by-contract programming, is an approach for designing software.

<span class="mw-page-title-main">Tony Hoare</span> British computer scientist

Sir Charles Antony Richard Hoare is a British computer scientist who has made foundational contributions to programming languages, algorithms, operating systems, formal verification, and concurrent computing. His work earned him the Turing Award, usually regarded as the highest distinction in computer science, in 1980.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

In computer programming, run-time type information or run-time type identification (RTTI) is a feature of some programming languages that exposes information about an object's data type at runtime. Run-time type information may be available for all types or only to types that explicitly have it. Run-time type information is a specialization of a more general concept called type introspection.

<span class="mw-page-title-main">Dangling pointer</span> Pointer that does not point to a valid object

Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations. More generally, dangling references and wild references are references that do not resolve to a valid destination.

In computing, a null pointer or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.

In computer programming, a nested function is a function which is defined within another function, the enclosing function. Due to simple recursive scope rules, a nested function is itself invisible outside of its immediately enclosing function, but can see (access) all local objects of its immediately enclosing function as well as of any function(s) which, in turn, encloses that function. The nesting is theoretically possible to unlimited depth, although only a few levels are normally used in practical programs.

<span class="mw-page-title-main">C Sharp (programming language)</span> Programming language

C# is a general-purpose high-level programming language supporting multiple paradigms. C# encompasses static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

Programming languages are used for controlling the behavior of a machine. Like natural languages, programming languages follow rules for syntax and semantics.

In type theory, a theory within mathematical logic, the bottom type of a type system is the type that is a subtype of all other types.

In computer programming, a trait is a concept used in programming languages which represents a set of methods that can be used to extend the functionality of a class.

S-algol is a computer programming language derivative of ALGOL 60 developed at the University of St Andrews in 1979 by Ron Morrison and Tony Davie. The language is a modification of ALGOL to contain orthogonal data types that Morrison created for his PhD thesis. Morrison would go on to become professor at the university and head of the department of computer science. The S-algol language was used for teaching at the university at an undergraduate level until 1999. It was also the language taught for several years in the 1980s at a local school in St. Andrews, Madras College. The computer science text Recursive Descent Compiling describes a recursive descent compiler for S-algol, implemented in S-algol.

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while in dynamically typed languages, equivalent behavior is provided by having a single null value.

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior. The null object design pattern, which describes the uses of such objects and their behavior, was first published as "Void Value" and later in the Pattern Languages of Program Design book series as "Null Object".

<span class="mw-page-title-main">Rust (programming language)</span> General-purpose programming language

Rust is a multi-paradigm, general-purpose programming language that emphasizes performance, type safety, and concurrency. It enforces memory safety—ensuring that all references point to valid memory—without requiring the use of a garbage collector or reference counting present in other memory-safe languages. To simultaneously enforce memory safety and prevent data races, its "borrow checker" tracks the object lifetime of all references in a program during compilation. Rust borrows ideas from functional programming, including static types, immutability, higher-order functions, and algebraic data types. It is popular for systems programming.

In object-oriented programming, the safe navigation operator is a binary operator that returns null if its first argument is null; otherwise it performs a dereferencing operation as specified by the second argument.

In programming language theory, flow-sensitive typing is a type system where the type of an expression depends on its position in the control flow.

References

  1. C.A.R. Hoare: Null References: The Billion Dollar Mistake, abstract of a talk at QCon London, 9–12 March 2009, available online
  2. Bertrand Meyer: Attached Types and their Application to Three Open Problems of Object-Oriented Programming, in ECOOP 2005 (Proceedings of European Conference on Object-Oriented Programming, Edinburgh, 25–29 July 2005), ed. Andrew Black, Lecture Notes in Computer Science 3586, Springer Verlag, 2005, pages 1-32, also available online
  3. Syme, Don. "Quote of the Week: "What can C# do that F# cannot?"" . Retrieved 7 September 2018.
  4. "Nullable reference types | Microsoft Docs" . Retrieved 12 March 2021.
  5. Moore, Kevin (2018-08-07). "Announcing Dart 2 Stable and the Dart Web Platform". Dart. Retrieved 2018-08-08.
  6. "Sound null safety | Dart" . Retrieved 12 March 2021.
  7. "Null safety—Kotlin" . Retrieved 12 March 2021.
  8. "Defining an Enum - The Rust Programming Language". doc.rust-lang.org. Retrieved 2021-06-26.