[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gnulib] coding practices
From: |
Bruno Haible |
Subject: |
Re: [Bug-gnulib] coding practices |
Date: |
Tue, 14 Jan 2003 20:40:29 +0100 (CET) |
Paul Eggert writes:
> a method that I now prefer in software that I help maintain.
> Instead of this:
>
> return table[(unsigned char) E];
>
> I write something like this:
>
> unsigned char c = E;
> return table[c];
>
> Avoiding casts entirely catches a few more errors. E.g., if E is
> actually a pointer you'll get a compile-time error if you avoid the
> cast, but typically an unchecked run-time error if you use the cast.
The problem with this idiom is that a commonly used, commonly
correctness-preserving modification
type var = E;
return func(var);
<-->
return func(E);
now introduces a bug. Or, in the case of characters, one often changes
the type from 'unsigned char' to 'int' (so that c can then also
accomodate the value EOF).
In both cases, your cast-less style introduces a bug. In other words,
the code as you write it is correct, but the maintainers that come
after you are not likely to understand the subtlety of
unsigned char c = E;
and therefore it presents a pitfall for them.
Bruno