POV-Ray : Newsgroups : povray.programming : cleaning source code from warnings troubles : Re: cleaning source code from warnings troubles Server Time
28 Jul 2024 14:31:24 EDT (-0400)
  Re: cleaning source code from warnings troubles  
From: Christopher James Huff
Date: 2 Oct 2002 15:46:40
Message: <chrishuff-7DF213.15434602102002@netplex.aussie.org>
In article <Xns### [at] 204213191226>,
 Philippe Lhoste <Phi### [at] GMXnet> wrote:

> 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.

And I usually avoid this kind of code. Sapphire doesn't allow it, and I 
probably won't add it.


> 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'm just too lazy to keep typing " != 0" or " != NULL"...it hasn't 
caused me trouble yet, but for reading I'd prefer it because of its 
unambiguity.


> 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?

Besides, it doesn't always work in C++...a function can return a 
reference.

myCam.Location() = blah;

is perfectly valid code. Not a good design in most cases, but valid.


> 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.

If f is used later, then yes. Otherwise the larger amount of code 
outweighs the clearer wording, in my opinion. "if(Foo() != 0)" or 
"if(Foo())" are clearer.


> There was a time were I admired C's compactness (coming from Basic and 
> Pascal worlds), but experience changed that :-)

Well...being excessively verbose is bad too. For example, I much prefer 
"{}" braces to a "begin...end" style, it is easier to recognize the 
symbols when mixed in with lots of other words and nicely short to type, 
but languages like Python which use white space to define blocks of code 
drive me nuts.
And I doubt anyone would prefer "add", "multiply", etc for 
operators...think of how huge simple expressions would get. However, I 
do prefer "and" and "or" to "&&" and "||". And I've never used the "?:" 
operator.

-- 
Christopher James Huff <cja### [at] earthlinknet>
http://home.earthlink.net/~cjameshuff/
POV-Ray TAG: chr### [at] tagpovrayorg
http://tag.povray.org/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.