r/ProgrammingLanguages • u/Clorofilla • Jan 02 '26
Quirky idea: could the "Nothing value" save beginners from dealing (too much) with exceptions?
CONTEXT
I think the context is essential here:
An high-level language, designed to teach high-level programming ideas to non-mathematically oriented people, as their first introduction to programming.
So worries about performances or industry standard syntax/patterns are secondary.
The language is designed with real time canvas drawing/animation in mind (as it's a good learning/feedback context). It pushes more the idea of a playful programming game rather than a system to craft safe and scalable apps.
The language aims at reducing the problem space of programming and to place certain concepts in the background (e.g. you can care about them when you become good enough with the basics).
On this note, one big design issue is the famous choice: should my language be more like a bad teacher that fails you too soon, or a cool uncle who helps you in ruining your life?
Restrictive and verbose language which complains a lot in the editor (validation)
VS
Expressive and concise language which complains a lot in the runtime (exceptions)
Is not all black and white, and there are compromises. Static (and inferred) types with a sprinkle of immutability in the right places can already remove many exceptions without heavy restrictions. Again, think beginner, not the need for crazy polymorphic data structures with super composability and so on.
But even with those things in place, there is still a black hole of dread and confusion which is intrinsic to expressiveness of all programming languages:
Operations may fail. Practically rarely, but theoretically always.
- Accessing a variable or property may fail
- Math may fail
- Parsing between types may fail
- A function may fail
- Accessing a dictionary may fail
- Accessing a list (or string char) may fail
1 is fixed by static typing.
2 is rare enough that we can accept the occasional `Invalid Number` / `Not A Number` appearing in the runtime.
3, 5 are usually solved with a `null` value, or better, a typed null value like `Option<Type>` or similar and then some defensive programming to handle it. But it doesn't feel like a talk you want to have day-1 with beginners, so it feels weird letting the compiler having the talk with them. I want to push this "need to specify how to handle potential exceptions" more in the background.
4 one could use some try-catch syntax, or even simpler, always make a function successfully return, but you just return the typed-null value mentioned above (also assume that functions are required to always return the same type / null-type).
6 could be solved like 5 and 3, but we can go a bit crazier (see the extra section).
The idea: The Nothing value
(or better: the reabsorb-able type-aware nothing value!)
So the language has a nothing value, which the user cannot create literally but which may be created accidentally:
var mylist = [1, 2, 3]
var myNum = myList[100] + 1As you can see, myNum is assumed to be of type Number because myList is assumed to be a list of Numbers, but accessing a list is an operation which can actually returns Number or NothingNumber, not just Number. Okay, so the compiler could type this, and run-time could pass around a nothing value... but practically, to help the user, what we should do?
We could:
- throw an exception at runtime.
- throw an error at compile time, asking the user to specify a default or a guard.
- allow the operation and give myNum the value of nothing as well (this would be horrible because nothing would then behave like a silent error-virus, propagating far away from it source and being caught by the user at a confusing time).
I propose a 4th option: re-absorption
Each operator will smartly reabsorb the nothing value in the least damaging way possible, following the mental model that doing an operation with nothing should result in "nothing changes". Remember, we know the type associated with nothing (BoolNothing, NumberNothing, StringNothing, ...) so all this can be type aware and type safe.
For example (here I am writing a nothing literal for brevity, but in the language it would not be allowed):
1 + nothing // interpreted as: 1 + 0
1 * nothing // interpreted as: 1 * 1
"hello" + nothing // interpreted as: "hello" + ""
true and nothing // interpreted as: true and true
if nothing { } // interpreted as: if false { }
5 == nothing // interpreted as: 5 == -Infinity
5 > nothing // interpreted as: 5 > -Infinity
5 < nothing // interpreted as: 5 < -Infinity
[1, 2] join [1] // interpreted as: [1, 2] join []
var myNum = 5
myNum = nothing // interpreted as: myNum = myNumAs you can see sometimes you need to jiggle the mental model a bit (number comparison, boolean operations), but generally you can always find a relatively sensible and intuitive default. When you don't, then you propagate the nothing:
myList[nothing] // will evaluate to nothing
var myValue = nothing // will assign nothingYou might be wondering if this wouldn't still result in weird behaviors that are hard to catch and if this type of errors wouldn't potentially result in data corruption and so on (meaning, it would be safer to just throw).
You are generally right, but maybe not for my use case. It's a learning environment, so the running app is always shown alongside the source code. Each accidental runtime nothing creation can still be flagged directly in the source code as a minor warning.
Also, the user can still chose to manually handle the nothing value themselves with things like (pseudocode):
if isNothing(myVal) {
// ...
}or
var newNum = dangerousNumber ?? 42Finally the code is not meant to be safe, it's meant to allow real-time canvas play. If people (one day) will want to use the language to create safer app, they can have a "strict mode" where the runtime will throw on a nothing operation rather than doing the smart reabsorb. Or even flag all potential nothing creation as compile errors and require handling.
EXTRA
Basically my idea is "automatic defensive coding with implicit defaults". In the same vein we could cut one problem at the root for lists and strings indexing. As they are ordered they may have more intuitive defaults:
var mylist = [1, 2, 3]
myList[10]
// could be interpreted a looping index:
// myList[((index % mylist.length) + mylist.length) % mylist.length]
// or as a clamped index
// mylist[max(0, min(index, mylist.length - 1))]Like this we would remove two big sources of nothing generation! The only one remaining would be parsing (num <-> str), which could have it's own defaults and generic failing functions.
CONCLUSION
So tell me people. Am I high?
Part of me feel this could go horribly wrong, and yet I feel there is something in it.
58
u/MattiDragon Jan 02 '26
To me it seems like you reinvented null/undefined, but with more confusing behavior.
The main issue I see here is that it obscures where the broken code is. Take for example out of bounds list access. If I accidentally index out of bounds and then proceed to draw graphics with that I end up with nothing on screen or at best an error at the moment I draw. A runtime error of some kind is the only way to tell the user the exact line they messed up on.
Imo exceptions are the most reasonable choice for a beginner language. If you use them wisely there's really no reason to make beginners catch them. You could also do something akin to rusts panics where they're impossible to catch, but there are versions of functions that return optional results.
24
u/N-partEpoxy Jan 02 '26
Yeah, no.
20
u/baby_shoGGoth_zsgg Jan 02 '26
This sounds like if you took type coercion from dynamic languages and made it only slightly fancier and only work for essentially null. Fans of dynamic languages would be cool with it (but think it is kind of underwhelming), but this sort of thing has very much fallen out of fashion in the modern programming language landscape.
Also: âtrue and nothingâ -> true, while âif nothing {}â -> if false⌠even fans of dynamic type coercion will curse your name over âif true and nothing {}â and âif nothing {}â having opposite effects.
13
u/BothWaysItGoes Jan 02 '26
Yes, the third option is silent failure, which is common in dynamic scripting languages; and which nowadays seems to be a source of many problems and a bad solution in general. You basically want to ignore the lessons the profession has learned in the last 20 years.
9
u/0jdd1 Jan 02 '26 edited Jan 02 '26
This sounds like another reincarnation of the tired old DWIM (Do What I Mean) approach, featuring the dangerous assumption that, by default, everything the programmer types must surely mean somethingâŚ. History shows that while this may sometimes work very nicely in the small, it can fail horribly in the large. Your design argument would be strengthened by explaining how your version avoids this trap. (Your focus on beginning programmers sounds like a start, but you should emphasize it more strongly/repeatedly.)
7
u/fixermark Jan 02 '26
This approach tilts far in the direction of "Just keep running, I don't care if the result is usable."
In some contexts, this is what you want. But in most contexts, the rule-of-thumb is "Fail early and fail often." Indeed, Google's approach in-house is that most things that would be exceptions in other contexts should noisily crash and log (and then their in-house precursor Kubernetes, Borg, detects the crash, couriers the logs to where they should be, and restarts the offending node).
Your solution is very similar to how IEEE-754 floating-point math works regarding NaN, but floating-point is working around needing to be a low-level system that didn't have the luxury of a mechanism in its protocol to signal the CPU "My calculation was bad." In general, I think most developers don't want that and would actually have preferred that, for example,
float(5) / float(0)would have been a fault instead of infinities or NaNs.6
u/evincarofautumn Jan 02 '26 edited Jan 02 '26
My feeling is that
nothingshould propagate like anull/NaN, but like an exception, it should keep a trace of source locations and operations applied to it, like ânothing, frombeans + nothingat (line) in (file), frombears / 0at (line) in (file)âIf youâre going to have defaults, you really need to consider your laws â things are easier to predict and understand when they follow simple rules that are easy to explain
Specifically, I think itâs okay for
nothingto behave like the identity element of a monoid, if such an element exists, where the operation is closed, mapping two elements of the same type to a result of the same type, and associative, so that bracketing is irrelevant to the resultSo these examples I think are fine:
0for numeric+1for numeric*""for string+and[]for arrayjointruefor logicalandfalsefor logicalorThe cases where I foresee trouble are those where the operation isnât associative, possibly because it isnât closed, or where the operation is unclear to begin with
For example,
if nothingisnât really safe to interpret asif false, because you donât know the intent behind itif A, isAmeant as âif any of these holdâ or âif all of these holdâ? For âif anyâ, an empty condition is trivially false, while for âif allâ, itâs vacuously trueif A B, isBmeant as the happy path / rule / main idea, or the sad path / exception / early exit? You certainly donât want to just skip the body for tests like âif unsafe then stopâif A B else C, is this meant as a series of cases followed by a default, or is it meant as two disjoint casesif A Bandif not A C? For the former, whenAisnothing, we should doC, while for the latter we should do neitherBnorCFor that matter,
nothas the same problem asif, where you donât know whether itâs implicitly ânot allâ or ânot anyâ, although you could certainly distinguish those cases with different operatorsI donât think itâs sensible to interpret
nothingnumerically as-Infinityindiscriminately==isnât a closed operation if it returns a boolean, at first glance I would expect it to behave like âall of these operands are equalâ, following how a chain of equalities A = B = C = ⌠behaves in mathematics5 == nothing,5 > nothing,5 < nothingshould all be trueEssentially, regardless of whether you pick
trueorfalse, this will lead to either logical inconsistency or loss of fundamental laws like transitivity(A == B) and (B == C) implies (A == C), so itâs better in these cases to be conservative and propagate some form ofnothingFinally, I think you should allow
nothingto be written explicitly in a program somehow, if only as a standard-library function likenothing(), so you can show how it works within the language using real runnable code that people can try for themselvesIf there isnât a standard way to obtain a
nothingvalue, people will come up with an idiom for it â for example, in JavaScript, because the nameundefinedcan be reassigned, thereâs a defensive idiomvoid 0for reliably getting the undefined value, which is less clear than if it were just built in likenull4
u/AustinVelonaut Admiran Jan 02 '26 edited Jan 02 '26
I like the idea of "automatic defensive coding", but maybe expose an explicit default instead of the implicit one, e.g. Haskell's
findWithDefaultfor Map. This exposes the student to the idea that they have to handle the exceptional case, but without a lot of mess or boilerplate dealing with catch/throw orOptional/Maybetypes. I'm not sure of a good syntax for dealing with an explicit default value on an array indexing operation, though.4
u/L8_4_Dinner (â Ecstasy/XVM) Jan 02 '26
Congratulations! You have just invented Javascipython!
4
u/PurpleYoshiEgg Jan 02 '26
Hello again, Maybe monad. Welcome back!
1
u/Calavar Jan 02 '26
Isn't your system just a weakly typed version of this? The programmer is going to have the same issue of "spooky action at a distance" where they find abnormal behavior far away from the site of the actual error. Except it will be even worse: With silent
nothingpropagation the programmer can at least follow thenothingvalues back to the site of the original error like a trail of breadcrumbs, but when you add silent type coercion like1 + nothing => 1you can't even do that.3
u/amarao_san Jan 02 '26
If expression above is evaluated to false, congrats, you've just invented NaN, and everyone hate you.
2
u/WillisBlackburn Jan 03 '26
I'm not convinced that supporting "nothing" in the way that you describe makes sense for a learning language. You don't want a learning language to take a "eh, whatever" approach to typing and error-handling because they are intrinsic to software development. If a new programmer tries to retrieve the 11th element of a 10-element array, are you really helping them by returning 0 or nothing? It's just going to show up as an error or incorrect result downstream, when it's a lot less obvious what the source of the problem is. Or the program might work by coincidence despite the bug, depriving the programmer of an opportunity to learn from their mistake. You suggested that the system could flag each nothing creation as a "minor warning" but it seems anything but minor to introduce an unexpected value into the program logic.
1
u/clarkcox3 Jan 02 '26
This is basically how Objective-C treats NULL/nil values. You can send any message you want to a nil object, and the result is always nil (converted to the appropriate type: nil for pointers, 0 for integer or floating point values).
1
u/tobega Jan 02 '26
Some of this reminds me of SQL where NULL is mostly just ignored and all comparisons are false. It can be confusing but lots of non-programmers seem to handle it.
In my Tailspin language I do have nothing values, but there it works because it is always pipeline processing. https://tobega.blogspot.com/2021/05/the-power-of-nothing.html
1
u/brucejbell sard Jan 02 '26
The problem is that you're providing ad hoc
Nothingsemantics for all your operations. This kind of thing is a major problem with SQL. They decided to provide anullvalue, and provided what must have seemed reasonable semantics, but nownullis the source of most of the hairy behavior in SQL.So, yes: your beginning programmers won't need to keep "there might be an exception at any point" in their mind at all times. Instead, they will need to to keep "there might be a
Nothingvalue" in mind at all times, and memorize (or look up each time) your chosenNothingsemantics for every value.Hiding error behavior is not a mercy -- it is a curse. Especially for beginners!
1
u/Ronin-s_Spirit Jan 02 '26
I come from JavaScript, and I like the language, but I'm begging you to do errors that hit you in the face like a bus. None of that "return error values" bullshit like in GO (they can't even decide on syntax sugar for error handling lmao), none of that IEEE-754 "broken number virus" where everything becomes a
NaNorInfinity.Epxplode the program when something is clearly wrong - like division by zero.
Tracking implicit bugs created by such subversive ideology is one of the biggest reasons to quit, because beginners don't yet have a lot of experience in the "language backend". Requiring a huge time investment while initially fooling people with easy semantics is an antipattern.
1
u/Norphesius Jan 03 '26
Given your stated goal of having a language designed to teach beginners, I think this "nothing value" approach is a bad idea, regardless of its soundness on its own as a feature.
First, for the most part, a language that is specifically designed to teach people how to code shouldn't stray too far from common features of the more established languages that would likely be learned next. "Nothing" being novel is hindering beginners by getting them accustomed to unique functionality that doesn't have an analogue practically in the real world.
Second, the Operations may fail list kind of hides a lot of different failures under "function may fail". Not saying it needed to be exhaustive, but failing to open a file, failing to parse a file, and failing to allocate more memory for a new object created from the file data have massively different implications for the immediate future of your program. Tossing a "nothing" out for all cases seems far too general and non-descriptive of a solution, and I'm not sure it really teaches anything. Being able to understand and appropriately handle different errors in different ways is a critical skill for programmers to develop, so they need to be exposed to actual errors.
I think a better solution here is (since I agree, exceptions are kinda crappy to teach and hard to reason about, especially for new people), for whatever feature could result in an error, to present that error as fast and as clearly as possible. If you can catch it at compile time, do that. If it can occur at runtime, have it be guarded obviously with an Option<Type> or Result<Type, Error> (exact syntax may vary), so the students know exactly what errors can come from where, and how they can be handled (or just ignored to crash, for quick and dirty coding). If its something that's too awkward to have a monad for, like divide by zero or out of memory errors, just have the program crash immediately, with a helpful error message pointing out exactly what went wrong.
"Nothing" seems like complex papering over an uncomfortable, but crucial, part of learning to code. The exposure to compile and runtime errors sooner will help them be less intimidated by them faster.
1
u/Motor_Fudge8728 Jan 03 '26
Take a look at how haskell and scala do error handling with maybe and either
1
u/GiveMe30Dollars Jan 04 '26
I'm going to be honest: this seems like a more dangerous version of null.
The idea of doing "nothing" would nessecitate defining a "nothing" value for most operations, like
+(0) or*(1) or<(is 5 less than nothing? Is -5 less than nothing?? Isn't nothing 0?). These identity values also depend on the type of the argument passed. This is frankly very time-consuming, or plain impossible (see<above. Especially for things that aren't numbers but for which ordering/partial ordering still makes sense).It also doesn't solve the biggest problems with implicit null: it can crop up anywhere, regardless of the type of the function, and the only way to account for that is to treat all callsites to external functions as possible failstates, and program defensively. This also applies to exceptions to a certain extent. The only way to know whether a function can fail (through null or exceptions) is either through documentation or trial-by-fire. Neither of these are desirable for a beginner.
I do think the idea of shielding beginners from failstates is misguided. Something has went wrong, and continuing execution may not be desirable, especially if you can't tell that something went wrong. Even with IEEE754 (and nobody is diverging from that standard anytime soon), it's tedious to track down where a
NaNis produced in a series of calculations, that only makes itself known after you try using that value to, say, color a pixel.There's a reason modern languages either have an explicit notion for exceptions that shows up in the type signature, or do not have exceptions to begin with, and usually make null an explicit type (like Rust or Haskell's unit type
()).1
u/Positive_Total_4414 Jan 04 '26 edited Jan 04 '26
There's indeed something in it, but the other comments have already explained why it's a bad idea the way it's described.
But what I want to say is that the idea itself is not totally senseless. The point of it is that you're looking for a mechanism to handle runtime errors in a more flexible way, preventing the whole program from crashing, and making the rest of it still run, but at the same time retaining the full information about the error.
So what you're looking for is algebraic effects. Your particular described usecase for them is a paradigm of effects, probably implemented as a library called something like "newbie-language-tutorial-sandbox", in a language that fully supports them on all levels of execution. You could try prototyping and testdriving something like this in a language that supports algebraic effects, like Koka or any other one.
And it might be bad for newbies the way it's described, actually, that it softens their brains. But as a program formalization approach this is well related to the motivation behind algebraic effects.