|
|
Warp <war### [at] tagpovrayorg> wrote in news:3d9a4b7e@news.povray.org:
> ABX <abx### [at] abxartpl> wrote:
> I also disagree with the idea that a compiler should not issue any
> warnings about code which is correct according to the C++ syntax
> definition. The fact that a piece of code is syntactically correct does
> not mean that it works ok. That's exactly what warnings are for: To
> inform you that the piece of code you just wrote might not work as you
> want. The warning might be irrelevant in some cases, but it still can be
> of great aid in many cases.
I agree. A simple case is the line:
if (a = b)
It is perfectly legal in C, but at some warning level, the compiler howls...
And I agree with it. I may wanted to check if b is non-null, but most of the
case, I just failed to type two equal signs.
If I want to do:
if (f = Foo())
to check if Foo is not returning an error status, I should instead write:
if ((f = Foo()) != 0)
which is uglier, but less prone to errors or ambiguity.
I know that some coders prefer to write:
if (Foo() == f)
because if they forget an equal, an error will be thrown. But I don't like
much this form, habits, you know?
BTW, I write now:
f = Foo();
if (f != 0)
which is more elegant, easier to read, and probably as efficient.
Some may object it wastes space (see the hot discussion about soft braces
placement...) but I now prefer a nice layout to a compact one.
There was a time were I admired C's compactness (coming from Basic and
Pascal worlds), but experience changed that :-)
BTW, removing warnings is not a futile task.
Lua programmers take careful steps to maintain strict ANSI C compliance, so
their code is very portable. Sometime, when compiling with VC++, I get some
warnings. Most of the time, they correct the code (mostly to add some
explicit casts).
It allows:
- to insure no ambiguity (because of default rules) remains;
- to avoid inundating the poor programmer with a lot of harmless warnings,
letting him miss the important ones...
Last note: you can't please everyone, including compilers.
Lua has the following function:
static int io_exit (lua_State *L) {
exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
return 0; /* to avoid warnings */
}
Well, VC++ issues a warning on the return line:
D:\Programmes\Langages\Lua\src\lib\liolib.c(565) : warning C4702:
unreachable code
Both are right, of course. :-)
Note: the function must return an int because it is the signature of all Lua
functions (called by the interpreter).
--
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
Post a reply to this message
|
|