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


Groups > comp.lang.c > #12432

Re: the loop control

From Ike Naar <ike@iceland.freeshell.org>
Newsgroups comp.lang.c
Subject Re: the loop control
Date 2011-11-01 09:51 +0000
Organization A noiseless patient Spider
Message-ID <slrn3vfsjavg8r.mq4.ike@iceland.freeshell.org> (permalink)
References (1 earlier) <20111029175230.259@kylheku.com> <j8meed$p0j$1@reader1.panix.com> <878vo0kgzj.fsf@bazspaz.fatphil.org> <slrn3vfsjaue9u.fu6.ike@sverige.freeshell.org> <j8njtb$vhv$1@dont-email.me>

Show all headers | View raw


On 2011-11-01, James Kuyper <jameskuyper@verizon.net> wrote:
> On 10/31/2011 08:11 PM, Ike Naar wrote:
>> On 2011-10-31, Phil Carmody <thefatphil_demunged@yahoo.co.uk> wrote:
>>> Needing to use something more than once, where use modifies that
>>> thing, clearly requires you to maintain an unmodified copy of the
>>> original.
>> 
>> If a local variable is needed to maintain an unmodified copy of the original,
>> one might as well leave the original parameter unmodified,
>> and use a local variable as the entity-to-be-modified.
>> This is (arguably) cleaner, because the meaning of the parameter
>> does not change during the process.
>
> That depends entirely upon how you interpret the meaning of the
> parameter. Like any variable, if it's value varies, it's meaning must be
> something that can be said to remain unchanged despite the fact that the
> value has changed.
>
> The meaning of 'count' and 'point' do not change anywhere within the
> following code:
>
> 	void zero_array(
> 	    int *point,
> 	    int count
> 	){
> 	    while(count-- > 0)
> 	        *point = 0;
> 	}
>
> At the start of the function, and at the end of each pass through the
> loop, 'count' contains the number of elements that still need to be
> zeroed, and if that count is non-zero, then 'point' points at the first
> of the elements that still need to be zeroed.

This is not an example where one needs a local variable to maintain
an unmodified copy of the original. And actually the meanings of
count and point *do* change, initially they describe the entire
set of points, and later on they describe the set of points
yet-to-be-processed.  In this simple example the difference is
futile, because there is no reason to keep the entire set for
later reference. Looking at a slightly more complicated example
that *does* need local variables:

    void froboz_array(int *point, int count)
    {
      int *all_point = point;
      int all_count = count;
      while (count-- > 0)
        fro(*point++);

      point = all_point;
      count = all_count;
      while (count-- > 0)
        boz(*point++);
    }

some might prefer a variant that is almost identical,
but has the advantage that it does not modify the parameters:

    void froboz_array(int *all_point, int all_count)
    {
      int *point = all_point;
      int count = all_count;
      while (count-- > 0)
        fro(*point++);

      point = all_point;
      count = all_count;
      while (count-- > 0)
        boz(*point++);
    }

In the simpler example that you showed (only one sweep
across the point array), one can make a tradeoff between two
desirable objectives:
a) keeping the parameters constant
b) minimizing the number of local variables

I think a) vs. b) is a honest tradeoff, and not a dumb vs. smart question.

>>> Needing to use something once, where use modifies that thing, clearly
>>> does not require you to maintain an unmodified copy of the original.
>>>
>>> If you are unable to see the difference between these cases, then you
>>> will clearly be hampered when it comes to chosing appropriate coding
>>> techniques. That, however, is your problem, and not the language's
>>> problem. 
>> 
>> If somebody has a different view, it's not necessarily because they
>> are "unable" to see things your way.
>
> He's not addressing your failure to agree with him, but rather your
> claim that the alternative approach leads to confusion. He's saying, in
> essence, that any person who would be confused for that reason still has
> a lot to learn about programming.

Phil was talking to Joe; I am not Joe.

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


Thread

the loop control jgk@panix.com (Joe keane) - 2011-10-29 21:38 +0000
  Re: the loop control Ian Collins <ian-news@hotmail.com> - 2011-10-30 10:48 +1300
  Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-29 22:10 +0000
    Re: the loop control jgk@panix.com (Joe keane) - 2011-10-31 15:21 +0000
      Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 11:35 -0400
        Re: the loop control jgharston <jgh@arcade.demon.co.uk> - 2011-10-31 10:27 -0700
          Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-10-31 18:36 +0000
          Re: the loop control Robert Wessel <robertwessel2@yahoo.com> - 2011-10-31 13:49 -0500
          Re: the loop control Sjouke Burry <s@b> - 2011-10-31 18:49 +0000
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 15:07 -0400
          Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-10-31 13:56 -0700
      Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-10-31 17:18 +0000
        Re: the loop control Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-01 04:35 -0700
          Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
            Re: the loop control Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-02 01:16 -0700
      Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-31 19:23 +0000
        Re: the loop control jgk@panix.com (Joe keane) - 2011-11-02 04:47 +0000
      Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:07 +0200
        Re: the loop control Ike Naar <ike@sverige.freeshell.org> - 2011-11-01 00:11 +0000
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 22:01 -0400
            Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-11-01 02:51 +0000
              Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:23 -0400
            Re: the loop control Ike Naar <ike@iceland.freeshell.org> - 2011-11-01 09:51 +0000
              Re: the loop control pete <pfiland@mindspring.com> - 2011-11-01 05:34 -0500
              Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:30 -0400
              Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-01 07:35 -0400
            Re: the loop control Hans Vlems <hvlems@freenet.de> - 2011-11-11 00:38 -0800
        Re: the loop control jgk@panix.com (Joe keane) - 2011-11-02 03:36 +0000
          Re: the loop control Ian Collins <ian-news@hotmail.com> - 2011-11-02 17:09 +1300
            Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-11-02 03:56 -0700
      Re: the loop control Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-31 21:38 +0000
  Re: the loop control Acid Washed China Blue Jeans <chine.bleu@yahoo.com> - 2011-10-29 19:31 -0700
  Re: the loop control Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-10-29 23:11 -0600
  Re: the loop control "Philip Lantz" <prl@canterey.us> - 2011-10-29 22:47 -0700
    Re: the loop control jgk@panix.com (Joe keane) - 2011-10-31 16:36 +0000
      Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-10-31 19:30 +0000
        Re: the loop control blp@cs.stanford.edu (Ben Pfaff) - 2011-10-31 12:38 -0700
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-10-31 15:51 -0400
            Re: the loop control blp@cs.stanford.edu (Ben Pfaff) - 2011-10-31 13:06 -0700
            Re: the loop control Keith Thompson <kst-u@mib.org> - 2011-10-31 14:17 -0700
              Re: the loop control Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
            Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:30 +0200
        Re: the loop control jgk@panix.com (Joe keane) - 2011-11-03 21:29 +0000
          Re: the loop control James Kuyper <jameskuyper@verizon.net> - 2011-11-03 18:00 -0400
          Re: the loop control Kaz Kylheku <kaz@kylheku.com> - 2011-11-04 00:43 +0000
            Re: the loop control jgk@panix.com (Joe keane) - 2011-11-11 02:24 +0000
      Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-31 23:20 +0200
      Re: the loop control pete <pfiland@mindspring.com> - 2011-10-31 18:01 -0500
        Re: the loop control Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-02 01:20 +0200
  Re: the loop control 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-02 01:25 -0700

csiph-web