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


Groups > comp.lang.c > #63097

Re: Do variables need to be initialized?

From Bartc <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Do variables need to be initialized?
Date 2015-06-08 22:58 +0100
Organization A noiseless patient Spider
Message-ID <ml534i$abg$1@dont-email.me> (permalink)
References (11 earlier) <87mw0a9vz6.fsf@bsb.me.uk> <ml45i8$gmn$1@dont-email.me> <ml4a2j$2vf$1@dont-email.me> <ml4fvb$s30$1@dont-email.me> <ctmav1F238tU1@mid.individual.net>

Show all headers | View raw


On 08/06/2015 21:06, Ian Collins wrote:
> Bartc wrote:
>> On 08/06/2015 15:51, David Brown wrote:
>>> On 08/06/15 15:34, Bartc wrote:
>>
>>>> You can't see that both introduce clutter that is going to affect
>>>> readability?
>>
>>> 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.
>>
>> Sure. You do that by using variables within the same function! If the
>> declarations at top seem too remote from their use later in the
>> function, /then/ maybe the function might be too big.
>>
>> All I'm saying is that interspersing declarations alters the 'lines' of
>> the code adversely.
>
> Care to address the examples I mentioned a couple of days ago?

OK (I don't view the group in threaded mode so tend to lose track).

I think I drafted a reply then cancelled because one of your examples 
(splitting a complex expression into smaller units each stored in a 
local temporary variable) was a good one and I couldn't really shoot it 
down.

But even then, declaring them like this:

   { int t1, t2;

     t1 = ...;
     t2 = ...;
     if (....)

is not that bad, if they are at least declared at the top of a block. 
What I don't like is a sequence of ordinary statements, assignments, 
function calls, and then suddenly a declaration:

    a = b+c;
    fn(0,10);
    int t1;
    ...

because it looks out of place. Although I expect you will use temps like 
this:

   ...
   int t1 = <subexpr>;
   int t2 = <subexpr>;
   ...

I would still just declare them at the top of the function (then you can 
re-use t1 and t2 instead of having to declare a t1 and a t2 inside a 
number of blocks).

(I remember I have my own language and compiler too. I've just checked 
it and that also allows declarations anywhere! I'll have to take care of 
it; I think it was easier to just allow that than to prohibit it.

In that case, the positioning actually doesn't matter, as the scope will 
always be the entire function body (there are no block scopes**). In C 
the scope ranges from point of declaration, to the end of the block or 
function. Still it looks odd, in C being the equivalent of:

   a = b+c;
   int a,b,c;

with the a,b,c in the expression being declared on the next line.

(**This language is a little different in allowing certain names inside 
a function (not parameters or auto variables) to be accessed from 
outside. So if a name 'a' is declared in function 'fn', I can access it 
from outside as fn.a. Hence no block scopes as blocks don't have names 
and there is no access path.))

-- 
Bartc

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