[fpc-devel] BOOL / Comparing booleans - bad style

DaWorm daworm at gmail.com
Mon Dec 15 14:43:59 CET 2014


Pascal is about readability.  A sentence like "if this variable is true do
that" reads better than "if this variable do that".  The danger is only
when the variable type and the constant representing TRUE are not
compatible.  In C, since TRUE is usually an untyped #define, that can be
hard to determine by the compiler or parser.  In Pascal, it should be easy
to detect when the two arguments aren't defined as identical types, even if
the types are assignable to each other.  But as long as True and the
affirmative type of Boolean match, there should be no need for a warning.
Only if True is Boolean, and the variable is ByteBool, or WordBool, or BOOL
or some other type that doesn't use the same True/False values as Boolean
should a warning be needed.

In other words, it isn't a style issue, it is a type compatibility issue.

Jeff

On Mon, Dec 15, 2014 at 7:46 AM, Jasper Neumann <jn at sirrida.de> wrote:
>
> Hello folks!
>
> MS>>> But the silly C programmer did "if (a == TRUE)".
>
> JN>> I have seen this kind of nonsense way too often,
> >> even in Pascal code.
> >>May I sincerely ask to include a hint/warning feature such as
> >>  "Comparing booleans - bad style"
> >>in order to notify the programmer to think twice.
> >>This warning should be also active when using booleans
> >> as case expression.
>
> Marco van de Voor wrote:
> > This thread is about C style booleans.
> > For normal, safe behaviour, use Pascal booleans.
>
> Well, yes, but...
> I probably should better have changed the subject.
> My aim was to cure this kind of practice as it occurs on the Pascal side,
> although it might concern C compilers as well. I should propose this
> hint/warning for e.g. LLVM or GCC also.
>
> Best regards
> Jasper
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20141215/e983c012/attachment.html>


More information about the fpc-devel mailing list