home | documentation | download | source
The following types of programming errors have been identified as frequently occurring among beginning programmers on Stack Overflow:
5 / 2
and not expecting integer divisionif (x = 0)
when if (x == 0)
is intendedwhile (condition);
if a == b or c
(in Python) to test whether a
is equal to either b
or c
Scanner
or C scanf('%c')
^
to mean exponentiation in C or JavaMost languages use IEEE 754 binary floating point for non-integer calculations. Although this is well-defined, it is often counterintuitive for beginners. Consider the following Python session:
This happens because 0.2
cannot be repesented exactly in binary floating point.
To resolve this problem, Neon uses the decimal128 floating point type, which matches the base 10 that humans use to read and write numbers.
5 / 2
and not expecting integer divisionConsider the following C statements:
Beginners rightly assume that c
will be 2.5
as the result of the division.
However, the C language definition states that /
will be integer division if both operands are integers.
So, the result in c
is 2
.
To resolve this problem, the only number type in Neon is decimal64 floating point (called Number
).
In contexts such as array indexing where integers are expected, values are checked for the presence of a fractional part before trying to use them.
if (x = 0)
when if (x == 0)
is intendedIn C and derived languages, x = 0
is an expression with the result 0.
Some compilers raise a warning if an expression like (x = 0)
is used in a conditional statement, as it is not likely to be what is intended.
However, this is not prohibited by the language.
To resolve this problem, the assignment operator in Neon is :=
and assignment cannot appear within an expression.
In many common systems languages (eg. C, C++, Java, C#), a pointer may hold a “null” value which is a runtime error to dereference. Tony Hoare has called the introduction of the null reference in ALGOL his “billion-dollar mistake”.
To resolve this problem, Neon introduces the idea of a “valid” pointer. A valid pointer is one that has been checked against NIL
(the null reference) using a special form of the IF
statement. The resulting valid pointer can be dereferenced without causing a null pointer exception.
TYPE Node IS CLASS
next: POINTER TO Node
value: String
END CLASS
VAR node: POINTER TO Node := NIL
IF VALID node AS p THEN
print(p->value)
END IF
while (condition);
In C and derived languages, sometimes a loop or conditional is mistakenly written as:
The trailing ;
on the while
statement is in fact an empty loop body and the loop is an infinite loop.
To resolve this problem, Neon requires an explicitly terminated block in every compound statement:
VAR x: Number := 0
WHILE x < 5 DO
print(x.toString())
INC x
END WHILE
if a == b or c
(in Python) to test whether a
is equal to either b
or c
In Python, beginners find it natural to write code like:
This is valid because the "Jill"
is automatically treated as a boolean expression (with value True
) and combined with the name == "Jack"
condition using the or
operator.
To resolve this problem, values in Neon cannot be automatically converted from one type to another (in particular, not to Boolean).
Languages with complex exception hierarchies (eg. C++, Java, C#, Python) allow the program to catch all types of exceptions using a construct such as catch (...)
(C++) or except:
(Python).
This generally has the unintended effect of masking exceptions that may not be among those expected by the programmer.
Neon does not have an exception hierarchy, and the exception handling always uses explicitly named exceptions (there is no way to catch all types of exceptions).
Most programming languages allow names declared in a nested scope to shadow names declared in an enclosing scope (such as the global scope). For example, in C:
This can lead to confusion due to unexpectedly using the wrong variable.
In Neon, it is an error for a declaration to shadow an outer declaration.
In C++, it is possible to return a reference (or pointer) to a local variable:
This is undefined behaviour because the reference returns to memory that has been deallocated as soon as the function returns.
This is resolved in Neon by not having references.
Scanner
or C scanf('%c')
The Java Scanner
class and C scanf
functions treat their input as a stream.
However, when used with interactive terminal input, they do not return a value until an entire line has been typed at the console.
This causes confusion when the user types more than what is expected and the remainder of what the user typed is held in the input buffer until the next time input is requested.
At that time, the contents of the buffer are used without waiting for user input.
This is resolved in Neon by only providing line oriented input for text in traditional tty mode, or single character input via curses.
^
to mean exponentiation in C or JavaSometimes beginners expect the ^
operator to mean exponentiation (eg. dist = sqrt(a^2 + b^2)
).
However, ^
means bitwise XOR in many languages, which is not expected.
In Neon, ^
means exponentiation.
Many programming languages permit a function that returns a value to be called without actually using the return value. This is a frequent source of bugs because the return value may indicate an error condition, which is then ignored.
In Neon, it is an error to call a function that returns a value without using that value in some way.