Tom St Denis wrote:
) 2) initialized and declared at same time (*)
)
) (*) irks me because people often miss initializing things when
) initializers are all over the place. I like having a block of
) statements that initialize variables before the block of statements
) that make up the logic of the function. It adds up [to simplifying
) the code] when you write a function that has 12 parameters and 15 auto
) variables [like a PKCS #8 encryption routine...].
If you initialize and declare at the same time, how can you more easily
miss initializing things ? I would imagine that people tend to miss stuff
like this when the declarations are all at the top, and the initializing is
done somewhere else. Putting them together would lead to *not* forgetting
it, I would think.
Because it can make it harder to read and figure out which are
initialized. I suppose if your declaration block were really well
organized, lined up, and clean, but for me it's just easier to have
them as distinct blocks. It's not like I'm saying what I prefer is
the only way, but from my experience I have caught bugs in code
written that way (e.g. hard to spot things that are not initialized or
properly initialized).
Just like you don't *have* to use {} for your while loops. But I've
found it to cause problems when people don't. So even for one liners
I tend to.
Sometimes a little investment up front in terms of how you present
your code reaps rewards later when you have to maintain it.
With late declarations, the declaration, initialization, and use are all
close together.
You can't always organize code into nice logical blocks (sometimes
initializing something depends on processing another input first).
But I have yet to find a case where late declarations made sense. I'm
for scope-wise declarations ala
if (...) {
int somevar;
...
}
When you use a variable limited in scope because polluting the larger
scope can be its own problem. But to me when I see code like
for (...) {
...
}
int somevar = somefunc();
if (somevar) { ... }
It just smacks of "oh yeah, let's add this in the middle" and not well
thought out. Like if you knew in advance you had to compute "somevar"
you should have declared it before. I think what I'm saying is that
the style is symptomatic of other problems in the development flow.
It's not like I don't evolve my functions over time and add new
variables, but usually the way I write something complicated is
1. Figure out the goal of the function [output from input]
2. write english pseudo code comments for every logical step of the
algorithm
3. Start writing the auto variables and parameters, choosing logical
names/types to suit the job
4. one-by-one I write code for each pseudo-code comment block
This technique has yet to fail me and always results in code that has
a logical top-to-bottom feeling to it. Here are my variables, here
are my initializations, here is my code, etc. You don't read it
thinking "this part of code between step 3 and 4 was just kinda wedged
in here ad hoc."
Tom