Gabriel Sechan wrote:
> ----------------------------------------
>
>> Date: Mon, 7 Jan 2008 01:14:40 -0800
>> From: [EMAIL PROTECTED]
>> To: [email protected]
>> Subject: Re: Introducing Cobra
>>
>> Gabriel Sechan wrote:
>>
>>> It is? I've never heard that. In fact, I frequently hear (and agree with)
>>> the exact opposite- put every declaration at the top so that you can
>>> easily find the declarations of all variables.
>>>
>> You are working with a different definition of "modern languages". That
>> is thinking is still very strong in the C community, although even they
>> stopped forcing that thinking with the advent of C99. The modern
>> thinking is that you should declare a variable around when it is first
>> used, so that the declaration is right by the code that actually uses it.
>>
>
> Yes, I've heard this before. I've also heard it pretty much universally
> panned. It makes code so much harder to read, when you have to search around
> for the type.
>
It's about what people are used to. You shouldn't have to search around
for the type at all, because it is declared exactly in the first place
that you should care about its type, and the last place you should care
about its type shouldn't be too far away either.
> There's also a difference between bad style and a difference of opinion-
> I've heard people arguing (wrongly) that declare at use is better, I've never
> heard declare at top considered bad.
>
Yeah, I think you are spending too much time around C programmers. ;-)
>>> As a bonus, you can read the variables used and their types at the
>>> begining.
>>>
>> ...and as an extra bonus, you have almost no context for understanding
>> each of their roles until you start reading. This means you have to
>> remember what all the variables are and/or constantly refer back to them
>> while reading the code, even if some of them may not yet be relevant.
>>
>>
> You have an odd definition of constantly. Maybe once per variable, if its
> use in the code doesn't make it intuitively obvious and you don't remember
> it. More realisticly, once per function. The benefits of knowing the data
> to be used up front outweight it by so far its laughable. In fact, I find
> myself refering back to get a type far more in the embedded declaration style.
>
It depends on how your mind works. I find it horribly difficult to
remember details until things "fit" together (i.e. the purpose is
clear). When you combine the tendency to have short variable names and
type names in C with the "declare variables at the top" bit, it really
gets ugly. Interestingly, I've found that this style also seems to
encourage the *reuse* of variables (hey, I've already declared a
variable of type Foo, and I'm not using it anymore... so why add another
one of the same type right next to it?), which just really messes with
my head.
>>> This is honestly the first time I've *ever* heard of it being considered
>>> bad style.
>>>
>>>
>> Java, C++, C#, most scripting languages, etc....
>>
>>
>
> 99% of the C++ and Java code I've read, and 100% of the good C++ and Java,
> rarely if ever makes non top of block declarations. This isn't even an
> exageration- I honestly can't think of a piece of code written the other way
> that wasn't a buggy piece of shit.
>
Well, like I said, to the extent that it makes much of a difference one
way or the other is a sign you are in trouble, *but* if you are reading
C++ and Java code that looks like this:
Bar foo(....) {
SomeIteratorType iter;
/* a bunch of code that figures out the answer */
.....
/* cleanup time */
cleanup:
while (iter = baz.getSomething(); iter != baz.getSomethingEnd();
iter.next()) {
iter.close(someFlag);
}
return answer;
}
I'd argue you are still hanging out with C programmers. ;-) Some of the
good ones might surround the while loop with a block so they can declare
iter down where it is used, but at that point your introducing a block
purely for namespacing and that's just silly.
Indeed, in the early days of Java (and C++ for that matter) the compiler
*didn't* detect cases where iter was used without being initialized and
the results were... spectacular. As such people were loathe to *ever*
declare a local variable before it was ready to be initialized.
Honestly, it is not worth debating. Rest assured this point of view is
far from an original one, and a very practical one, particularly if your
user defined types are more powerful than found in C, but you still are
likely to have long functions. You may not feel it is a matter of taste,
and you may not know anyone who disagrees with you, but such people do
exist in large numbers and this is more a symptom that you live/work in
a homogeneous programming environment more than anything else.
--Chris
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg