Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #63074

Re: Do variables need to be initialized?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Do variables need to be initialized?
Date 2015-06-08 16:51 +0200
Organization A noiseless patient Spider
Message-ID <ml4a2j$2vf$1@dont-email.me> (permalink)
References (8 earlier) <1blhfwh7i2.fsf@pfeifferfamily.net> <4450dd92-34e2-47fd-a5f7-ad5da054057a@googlegroups.com> <ml446m$bb1$1@dont-email.me> <87mw0a9vz6.fsf@bsb.me.uk> <ml45i8$gmn$1@dont-email.me>

Show all headers | View raw


On 08/06/15 15:34, Bartc wrote:
> On 08/06/2015 14:21, Ben Bacarisse wrote:
>> Bartc <bc@freeuk.com> writes:
>>
>>> On 08/06/2015 13:39, gwowen wrote:
>>>> On Saturday, June 6, 2015 at 10:06:22 PM UTC+1, Joe Pfeiffer wrote:
>>>>
>>>>> If the search is non-trivial, the function is too long.  If the
>>>>> variables aren't all declared together, you still have to scan the
>>>>> 20-line function to figure out where they are.  That search is
>>>>> trivial,
>>>>> but still present.
>>>>
>>>> Nope.  If the function is short, and declarations are at
>>>> point-of-first-use then the majority of time the search is non
>>>> existent - the declaration is right there within a line or two of
>>>> the use.
>>>>
>>>
>>> Which expression is easier to take in:
>>>
>>>   double a,b,c;
>>>
>>>   x=(-b + sqrt(b*b - 4*a*c)) / (2*a);
>>>
>>> or:
>>>
>>>   double x = (- double b + sqrt(b*b - 4 * double a*double c) / (2*a);
>>>
>>> (I know this is not C syntax but it's the same principle of mixing
>>> things up that shouldn't be mixed up.)
>>
>> <jaw drops>  Really?  You support your argument with some made up
>> syntax that has nothing to do with the case being discussed?
> 
> You can't see that both introduce clutter that is going to affect
> readability? If I'm trying to grasp an algorithm expressed by the code,
> then it's going to be easier without arbitrary declarations all over the
> place. (An int a=0; will also move that assignment 4 characters to the
> right, out of the line of the other assignments.)

Putting random illegal identifiers in the middle of an expression
obviously adds clutter.  Putting a perfectly legal declaration at the
start is a common way of writing code:

double x = (-b + sqrt(b*b - 4*a*c)) / (2*a);

Adding that "double" is not "introducing clutter".

And yes, it moves the assignment a bit to the right - but assignments
are not (normally) in line anyway.

It is common practice to declare variables (and functions, types, etc.)
as close as conveniently possible to where you are going to use them.
This is considered good programming practice - you minimise scope and
lifetime of the variables.  And in some languages (notably C++) where
the definition plus assignment is not the same as an initialising
definition, there can be significant differences in where and how you
define the variables.

It is fine that /you/, in your personal opinion, do not like to mix
declarations and statements.  But you are simply wrong in making
sweeping statements about how "awful" this practice is.  Many other
people find it helps when writing clear, concise code.

> 
>>
>>> Take another example:
>>>
>>>   point *p1 *p2, *p3;
>>
>> (missing ',')
>>
>>> Here, I don't really need to write anything further, as that
>>> declaration is sufficient: those p1, p2, and p3 variables are clearly
>>> related. That relationship is lost if the declarations are split up
>>> because each is first used in a different place.

No, the only "clear relationship" between these variables is that they
are defined at the same place in the code.  Since that information is of
no value, nothing is lost by defining them elsewhere in the code.

>>
>> Did anyone say that you *have* to separate related declarations?  Your
>> argument seems to be against making use of the permission to delay
>> declaration until first use, but no one is saying that you must do so in
>> every case.
> 
> Sure, people can use either style. However if there is a possibility
> that there are other declarations buried in the code (someone decided
> they will try a p4, declare it in-place, then forgot about it) then it
> is something you have to consider.

This would only be an issue if you imagined that writing "point *p1,
*p2, *p3" implies that there is no "point *p4".

> 
> So you see a *p3 at the top, see a p3 used in the code, and think that's
> it. Then you discover another p3 in a nested block! Possible of a
> different type too.

There's no problem doing that.  Mixing declarations and code and using
block-local variables helps keep scopes minimal.  This makes it much
easier to be sure of exactly what "p3" is at any time.  It doesn't
matter if there is another "p3" somewhere else in the function - you can
see immediately what /this/ "p3" is, and you don't have to jump around
in the code to find it.

(Normally it's a good idea to keep names separate, but that's just
standard practice of writing clear code.)

> 
> I think most people will use my preferred style, but an awful lot
> apparently think it's OK to declare names in arbitrary, unexpected places.
> 

I think you have strong opinions about your style, because you haven't
really thought about alternatives - and you are generalising your own
ideas to "most people" without any justification.

(I have been guilty of that sin on enough occasions to recognize it here!)

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Do variables need to be initialized? fl <rxjwg98@gmail.com> - 2015-06-01 16:48 -0700
  Re: Do variables need to be initialized? Richard Heathfield <rjh@cpax.org.uk> - 2015-06-02 00:55 +0100
    Re: Do variables need to be initialized? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-06-01 18:22 -0600
      Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-02 08:05 -0700
        Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:07 -0700
          Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-07-11 16:47 -0700
            Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-15 07:42 -0700
      Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:03 -0700
  Re: Do variables need to be initialized? David Brown <david.brown@hesbynett.no> - 2015-06-02 09:15 +0200
    Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:09 -0700
  Re: Do variables need to be initialized? David Kleinecke <dkleinecke@gmail.com> - 2015-06-03 09:30 -0700
    Re: Do variables need to be initialized? gwowen <gwowen@gmail.com> - 2015-06-03 09:41 -0700
      Re: Do variables need to be initialized? David Kleinecke <dkleinecke@gmail.com> - 2015-06-04 10:17 -0700
        Re: Do variables need to be initialized? Stephen Sprunk <stephen@sprunk.org> - 2015-06-05 11:10 -0500
          Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-05 18:22 +0100
            Re: Do variables need to be initialized? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-06-05 20:54 -0600
              Re: Do variables need to be initialized? Ian Collins <ian-news@hotmail.com> - 2015-06-06 15:49 +1200
                Re: Do variables need to be initialized? Dr Nick <nospam-4@temporary-address.org.uk> - 2015-06-06 08:42 +0100
                Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-06 13:11 -0700
                Re: Do variables need to be initialized? Dr Nick <nospam-4@temporary-address.org.uk> - 2015-06-07 16:58 +0100
                Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-06 11:01 +0100
                Re: Do variables need to be initialized? Ian Collins <ian-news@hotmail.com> - 2015-06-07 09:07 +1200
                Re: Do variables need to be initialized? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-06-06 15:06 -0600
                Re: Do variables need to be initialized? gwowen <gwowen@gmail.com> - 2015-06-08 05:39 -0700
                Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 14:10 +0100
                Re: Do variables need to be initialized? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-06-08 14:21 +0100
                Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 14:34 +0100
                Re: Do variables need to be initialized? David Brown <david.brown@hesbynett.no> - 2015-06-08 16:51 +0200
                Re: Do variables need to be initialized? Richard Heathfield <rjh@cpax.org.uk> - 2015-06-08 16:15 +0100
                Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 17:31 +0100
                Re: Do variables need to be initialized? Ian Collins <ian-news@hotmail.com> - 2015-06-09 08:06 +1200
                Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 22:58 +0100
                Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-08 08:27 -0700
                Re: Do variables need to be initialized? gwowen <gwowen@gmail.com> - 2015-06-08 08:01 -0700
            Re: Do variables need to be initialized? Stephen Sprunk <stephen@sprunk.org> - 2015-06-06 11:11 -0500
              Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-06 19:09 +0100
                Re: Do variables need to be initialized? Robert Wessel <robertwessel2@yahoo.com> - 2015-06-06 13:57 -0500
                Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-06 13:52 -0700
                Re: Do variables need to be initialized? James Kuyper <jameskuyper@verizon.net> - 2015-06-06 20:57 -0400
                Re: Do variables need to be initialized? Stephen Sprunk <stephen@sprunk.org> - 2015-06-10 14:33 -0500
          Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-05 10:56 -0700

csiph-web