Porting style

Werner Koch wk at gnupg.org
Wed Jan 15 09:10:21 CET 2003


On Tue, 14 Jan 2003 11:43:33 -0600, David Champion said:

> I thought Werner was ruling out this model, too. (He didn't mention
> anything about frequency or location of ifdefs, just that they were
> "generally unacceptable".) There certainly are other ways besides ifdefs

No.  I see it the same way David explained it.

> to to machine-dependent code, but they seem messier yet.

I recently did a cleanup for Mutt, where some parts are hard to read
due to nested #ifdef HAVE_PGP and HAVE_SIME.  At most place it was
easy to do it like this:

- #ifdef HAVE_PGP
-   if (foo)
-      bar ();
- #endif

+   if (HAVE_PGP && foo)
+      bar ();

Given that HAVE_PGP is defined to 0 or 1.  Because HAVE_PGP is a
constant, the compiler will either eliminate the entire construct or
eliminate the "HAVE_PGP &&" term.  It is obvious that the code is
easier readable when you use these constructs with heavily nested
conditions.  The compiler is far better in optimizing than a human in
such situations.

Well, it does of course not work when there are functions which are
not available on some OSes - a silly stub function would do though.


Salam-Shalom,

   Werner






More information about the Gnupg-devel mailing list