July 27th, 2024

Don't use null objects for error handling

The article critiques using null objects for error handling in programming, arguing it misleads users and propagates errors. It advocates for immediate error handling and context-based strategies instead.

Read original articleLink Icon
Don't use null objects for error handling

The article critiques the practice of using null objects for error handling in programming, particularly in lower-level languages like C. It argues that returning a harmless default value, such as zero, when an operation fails can lead to misleading results and a poor user experience. The author emphasizes that this approach, often referred to as Zero Is Initialization (ZII), fails to address the underlying error and instead allows the program to continue operating with potentially invalid data. An example is provided where a function, `find_bone`, returns zero for an invalid input, which is treated as a valid output, leading to further erroneous calculations. The author suggests that true error handling should involve recording the error and aborting the erroneous task rather than propagating a null object through the system.

The article outlines various strategies for handling errors, including restricting input sets, using assertions, and expanding output sets. It advocates for immediate error handling by aborting the task that caused the error and suggests that programmers should choose the appropriate error handling strategy based on the context of the function's use. The author concludes that while null objects may seem convenient, they often mask errors rather than resolve them, leading to garbage data and a subpar user experience.

Related

The many faces of undefined in JavaScript

The many faces of undefined in JavaScript

JavaScript's handling of undefined values, including null and falsy values, can confuse developers. TypeScript introduces nuances with function arguments and void type. Recommendations suggest treating undefined as null to avoid issues.

Initialization in C++ is Seriously Bonkers Just Start With C

Initialization in C++ is Seriously Bonkers Just Start With C

Variable initialization in C++ poses challenges for beginners compared to C. C requires explicit initialization to prevent bugs, while C++ offers default constructors and aggregate initialization. Evolution from pre-C++11 to C++17 introduces list initialization for uniformity. Explicit initialization is recommended for clarity and expected behavior.

Six Dumbest Ideas in Computer Security

Six Dumbest Ideas in Computer Security

In computer security, common misconceptions like "Default Permit," "Enumerating Badness," and "Penetrate and Patch" hinder effective protection. Emphasizing a "Default Deny" policy and proactive security design is crucial.

What to do if you don't want a default constructor?

What to do if you don't want a default constructor?

The blog post discusses the implications of not having a default constructor in C++, highlighting design considerations and technical challenges. Solutions like dummy values, std::optional, and std::variant are proposed to address initialization issues.

Sentinel-free schemas: a thought experiment

Sentinel-free schemas: a thought experiment

The article explores eliminating NULLs and sentinel values in database design for clarity and accuracy. It suggests using separate tables per attribute and explicitly defining missing data reasons. This approach enhances data integrity and readability.

Link Icon 1 comments