Re: should i learn c?
On Mar 11, 5:15 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
Jorgen Grahn wrote:
In fact, if I were to learn C today, I'd go for C99. Having to
declare all variables at the top of the block is (for some reason) the
main thing that drives me nuts when I have to maintain C89 code. The
'inline' keyword is also nice when you worry about efficiency.
Sun's guidelines for Java suggest that you declare the
variables at the top of the block, even though the language
doesn't require it.
The only justification I can think of is that, like C++, Java's
syntax (like that of C++) doesn't really effectively distinguish
between a definition and an expression statement. Which makes
recognizing the definitions in a block of code somewhat
problematic. The obvious solution is to use some arbitrary
formatting element to make them identifiable---I use internal
spacing in a definition for this, for example. In Java, it's
probably not as bad defining everything at the top, since on one
hand, the only things you can define all have reasonable default
initializations which can be used if you don't know what else to
use, and the language itself requires "definite assignment": the
compiler is required to do a minimum of flow analysis in order
to verify that the variable is initialized before use. But it
still seems like a case of two wrongs trying to make a right.
Java also doesn't support inline. It helps me to think of
"setting up stack frames," rather than "declaring variables,"
even when I realize that the function I'm writing will
probably be inlined automatically, and therefore not require
its own stack frame at all.
I agree with your implied point that one probably shouldn't be
defining variables before one is ready to initialize them.
However, if you find yourself declaring variables in the
middle of a block, it may be worthwhile to factor that portion
of the block into a separate function, anyway.
Yes. Still, I can see cases where you might have a (very)
little code before defining a variable. Something like:
operator new( std::size_t n )
n += myOverhead ;
void* result = malloc( n ) ;
But on the whole, if you find a lot of declarations showing up
other than at the top of the function, you're probably doing too
much in the function.
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34