Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.c > #36967

Re: why do some writers declare and define variables separately

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: why do some writers declare and define variables separately
Date 2013-10-03 15:26 -0400
Organization Self
Message-ID <524DC4D4.1050604@verizon.net> (permalink)
References <clcm-20130918-0002@plethora.net> <524dc021$0$39566$afc38c87@news6.united-newsserver.de>

Show all headers | View raw


On 10/03/2013 03:06 PM, Michael Lenz wrote:
> Am Wed, 18 Sep 2013 16:42:10 -0500 schrieb yoodavid:
> 
>> Can anyone tell me why some writers decide to declare and write
>> definitions for variables separately, rather than together?
> 
> Many people do this in order to have all the variables declared on 
> function entry. Littering (initial and/or final) values into there 
> greatly reduces readability.
> 
> Have a look at this example:
> ---
> void foo(){
> 	int a, b, c;
> 	float f, *d,
> 	//some magic
> }
> ---
> void foo(){
> 	int a=4, b=-23, c=__SOME_DEFINED_CONSTANT;
> 	float f=0.0, *d=0,
> 	//some magic
> }
> ---
> 
> From just looking at the function preamble in the first example I 
> (hopefully) know all variables used in there, which is not possible (that 
> easily) on the second one.

For the sake of readability (and ease of maintenance) I usually put each
variable's definition on a different line, whether or not they were
initialized.  I also normally line up the variable names. With those
changes, the distinction you make pretty thoroughly disappears:

    int   a = 4;
    int   b = -23;
    int   c = __RESERVED_IDENTIFIER;
    float f = 0.0F;
    float *d = NULL;

> In case I want to patch some function magic, let's say a check of a 
> called function's return value, somehwere deep inside, I certainly want 
> to know if the symbol name "tmp", "temp", "funcCheck" or the like is 
> already in use. I do not want to mind-numbingly parse the whole preamble.
> I just want to look at it and see.

I normally just do a text search backwards from the point of use. I
don't find that particularly mind-numbing, even in code like your second
version.

> concluding rant: People who declare/define variables somehwere mid-code 
> shall face martial law, i.e. torture, at least. ;)

There are strong differences of opinion on this matter - otherwise,
support for that feature would not have been added to both C++ and C99,
both evolving from C90.

The strongest argument I've seen given for your point of view is that
declaring them elsewhere it makes it harder to find the variable's
declarations. Personally, I find variables more easily when their
definitions are closer to the point of use; but with a backwards text
search, it's seldom very difficult to find a variable (unless you gave
it a bad name - I never define any identifiers shorter than three
characters, and I try to avoid defining identifiers that are subsets of
other identifiers).

It's also far easier to initialize them with a value that will actually
be used. In some cases, that will allow you to declare them const.

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


Thread

why do some writers declare and define variables separately yoodavid <odimegwudavid@yahoo.fr> - 2013-09-18 16:42 -0500
  Re: why do some writers declare and define variables separately Fred K <fred.l.kleinschmidt@gmail.com> - 2013-09-18 14:52 -0700
  Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-18 18:08 -0400
  Re: why do some writers declare and define variables separately T0xicCode <T0xic@co.de> - 2013-09-18 18:10 -0400
    Re: why do some writers declare and define variables separately Ken Brody <kenbrody@spamcop.net> - 2013-09-19 11:16 -0400
      Re: why do some writers declare and define variables separately T0xicCode <T0xic@co.de> - 2013-09-19 11:45 -0400
  Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-18 15:45 -0700
  Re: why do some writers declare and define variables separately Jens Schmidt <Jens.Schmidt-HH@gmx.de> - 2013-09-19 01:19 +0200
  Re: why do some writers declare and define variables separately Francis Glassborow <francis.glassborow@btinternet.com> - 2013-09-19 02:13 +0100
    Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-09-19 03:59 +0100
      Re: why do some writers declare and define variables separately Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-09-27 00:49 +0300
      Re: why do some writers declare and define variables separately Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2013-10-03 15:41 -0700
        Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-04 00:42 +0100
    Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-19 00:01 -0400
      Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-19 10:17 +0200
      Re: why do some writers declare and define variables separately "osmium" <r124c4u102@comcast.net> - 2013-09-19 06:44 -0500
      Re: why do some writers declare and define variables separately Les Cargill <lcargill99@comcast.com> - 2013-09-19 08:00 -0500
        Re: why do some writers declare and define variables separately Noob <root@127.0.0.1> - 2013-09-19 15:12 +0200
        Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-19 08:31 -0700
          Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-19 11:35 -0400
            Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-19 12:16 -0700
              Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-19 15:32 -0400
                Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-22 12:36 -0700
        Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-22 11:28 -0700
          Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-09-23 03:26 -0700
            Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-24 20:33 -0700
              Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-09-25 04:37 -0700
                Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-10-22 21:23 -0700
                Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-10-23 04:34 -0700
                Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-23 16:02 +0100
                Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-10-24 02:31 -0700
                Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-24 12:27 +0100
                Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-10-24 08:08 -0700
              Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-25 08:24 -0700
                Re: why do some writers declare and define variables separately glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-09-25 19:08 +0000
                Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-10-22 00:22 -0700
                Re: why do some writers declare and define variables separately Olle Villesen <noemail@replytogroup.org> - 2013-10-22 07:48 +0000
                Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-10-23 18:13 -0700
                Re: why do some writers declare and define variables separately glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-10-24 03:12 +0000
                Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-10-24 10:58 -0700
                Re: why do some writers declare and define variables separately Martin Shobe <martin.shobe@yahoo.com> - 2013-10-22 08:55 -0500
                Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-10-24 06:15 -0700
          Re: why do some writers declare and define variables separately Les Cargill <lcargill99@comcast.com> - 2013-09-25 17:37 -0500
            Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-10-22 01:15 -0700
    Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-22 11:34 -0700
  Re: why do some writers declare and define variables separately gordonb.t5wsh@burditt.org (Gordon Burditt) - 2013-09-25 05:29 -0500
    Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-25 22:35 +1200
      Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-25 13:02 +0200
      Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 08:39 -0400
        Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-25 08:23 -0700
        Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 08:06 +1200
          Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 16:38 -0400
            Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 09:17 +1200
              Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-09-25 14:32 -0700
                Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 09:50 +1200
              Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 09:55 +1200
              Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 18:50 -0400
                Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 11:17 +1200
                Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 21:26 -0400
                Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 13:50 +1200
      Re: why do some writers declare and define variables separately Noob <root@127.0.0.1> - 2013-09-25 15:55 +0200
        Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 10:03 -0400
          Re: why do some writers declare and define variables separately Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-09-27 01:29 +0300
      Re: why do some writers declare and define variables separately Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-09-27 01:20 +0300
    Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-25 13:05 +0200
      Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-09-25 23:01 +0100
        Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 18:56 -0400
          Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-26 09:10 +0200
            Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-26 07:31 -0400
              Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-26 13:58 +0200
                Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-26 09:12 -0400
  Re: why do some writers declare and define variables separately Michael Lenz <unrest@nullvector.org> - 2013-10-03 19:06 +0000
    Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-10-03 15:26 -0400
  Re: why do some writers declare and define variables separately Dag-Erling Smørgrav <des@des.no> - 2013-10-03 21:15 +0200

csiph-web