<<< Date Index >>>     <<< Thread Index >>>

Re: Little code cleanup



On Tue, Mar 04, 2008 at 12:26:23AM +0100, Vincent Lefevre wrote:
> On 2008-03-03 17:42:01 +0100, Sébastien Hinderer wrote:
> > 2. It is true that testing pointers may avoid segfaults, but they don't
> > avoid problems. 

That really depends on whether the code is written such that it knows
whether the pointer should ever be null, and how the programmer decides
to deal with it when it is...  What you say is true sometimes, but
untrue often (if the code is well-written).  If the programmer can
know, for instance, that a null pointer in a particular place is
clearly a bug, then he could, for example, log a line to wherever
(stderr, some log file, etc.) which tells you what function was
executing when it happened (though this is tedious to maintain), what
source file and line number (for which there are macros offered by
at least some compilers), and even what the values of certain variables
are.  Though, hopefully, that code is #ifdef'd out by default...

Do you always want to do this?  I certainly hope not.  That would be
some ugly code.  But in various cases, when the code is unusually
tricky or error-prone, it may be called for...  In other instances
just catching the error and printing out a reasonable error message
(that can easily be found in the code) should suffice.  For cases in
the middle, you could use asserts...

> > What I mean is that, if a pointer becomes NULL whereas it should
> > not, having a segfault makes it much easier to debug the problem,
> > IMHO, than having a leading to some fallback behaviour.

But programs are made to be used by users, not just programmers.  Mutt
is a Mail USER agent.  I assert (with 12 years of sysadmin experience
in a wide variety of environments, large and small, to back me up)
that not all users of Mutt are programmers, and not all users of Mutt
are even particularly technical.  But technical or not, meeting a
segfault when using a program leaves a sour taste in one's mouth,
unless perhaps one is concerned about fixing bugs.  The vast majority
of users are not, even if they do happen to know how...  

> But pointers can be null by designed. If I understand correctly,
> some null pointers in Mutt are equivalent to empty strings. IMHO,
> this would be best handled in functions instead of the caller
> (though this may hide bugs in some cases).

Maybe, but I think the answer is again, "It depends."  Most likely it
depends on which part of the code is in a better position to know best
about the data.  It's the programmer's job to make that decision, and
it doesn't always have to be in one place or the other (and after all,
functions are all just functions, including the main() function, which
can be called by others, and sometimes this may even be desireable)...
It isn't always clear where the best place to do something is, even
once the code is written and/or the choices are fully understood.
It's often the case that writing the code one way makes something
easier, but makes some other thing harder, whereas choosing a
different implementation does the same in reverse.

-- 
Derek D. Martin    http://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address.  Replying to it will result in
undeliverable mail due to spam prevention.  Sorry for the inconvenience.

Attachment: pgp20Y2NQlwiG.pgp
Description: PGP signature