Semantic noise is a difference the semantics being used in the
code and the concept it represents in the problem space. In other
words, it is an indication that the code doesn't mean what a naive
programmer might expect. It is an important metric for Concept Programming. Since
this is a form of noise, it is desirable to reduce it, but it is in
general impossible to completely eliminate it. And also remember that
noise is somewhat subjective, so what is noise to one person may be
music to another.
Here are a few examples of common forms of semantic noise:
- Numbers in computers are limited compared to their mathematical
counterparts. The range of integers is not infinite, and real
numbers don't have infinite precision, to cite two simple
examples. This creates many classical forms of semantic noise. For
instance, using 16-bit signed computations, (32767+3)/2 is
generally not 16385. The result depends on the
language. In C, it is a negative value (-16383). In Ada, a
NUMERIC_ERROR or RANGE_ERROR exception will be raised
depending on the implementation. In that case, Ada makes the
situation where noise exists even noisier, to make sure it is noticed.
Similar issues exist for floating
point numbers, where the value 1+x-1 is often 0
rather than x for very small values of x (because
1+x has been rounded to available precision). As an amusing
aside, in C, the types representing real numbers are not called
real but float and double. Both names indicate
an implementation detail (floating-point and double
precision)... but not the same one!
- C uses the << operator to indicate left bit
shifts, so in C out<<3 shifts the integral value
out left by 3 bit positions (multiplying it by 8).
C++ introduces the possibility to overload operators, and the C++
standard library uses the << operator with a totally
different meaning, to output something to a
stream. So now out<<3 may mean writing 3 into a file or on
the console. There is a difference between the meaning a naive C
programmer expects and the actual meaning. This is only a
relatively minor problem with this notation, because it affects only
C programmers and the compiler will detect misuses. Two more serious forms
of semantics noise with C++ I/Os are discussed here
(inability to be made thread-safe, and incorrect model of what a
text line is).
concept programming analysis of existing languages
for examples combining syntactic noise and semantic noise.
Semantic noise complicates maintenance of programs, and makes the
learning curve steeper. It can cause bugs, because programmers tend
to use their knowledge of the problem space as a model for the structure of
the code, and semantic noise introduces a difference between the
two structures. Concept casts are a
frequent source of semantic noise.