Re-use the argument?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Tobin

    #31
    Re: Re-use the argument?

    In article <g2h74u$5h3$1@r egistered.motza rella.org>,
    Richard <rgrdev@gmail.c omwrote:
    >>>Generally it is considered bad form to modify an argument to a
    >>>function. It makes things a bit more difficult to read.
    >>What a load of nonsense. If its not a const its no more than a local
    >>variable.
    [...]
    >I really am not interested in that. Its effectively a local variable and
    >I have never seen anyone object, in the real world, to it being
    >manipulated.
    There's clearly no general rule against modifying variables, at least
    in traditional imperative languages like C. But that doesn't mean
    you should use any available variable as a temporary. Some variables
    are intended for a single purpose, and are named accordingly. Some
    variables ("i" for example) are traditionally used for a succession
    of values in a loop. The key is to be clear. For example, consider
    a function to determine kinetic energy given mass and speed:

    double ke(double mass, double speed)
    {
    return mass * speed * speed;
    }

    It would clearly be wrong to write:

    double ke(double mass, double speed)
    {
    speed *= speed;
    speed *= mass;
    return speed;
    }

    because that would use the variable "speed" for something which
    is not a speed. On the other hand, this would be quite reasonable:

    int mystrlen(char *s)
    {
    int l = 0;
    while(*s++)
    l++;
    return l;
    }

    "s" is clearly just a string, and it remains a string as you proceed
    through it.

    Perhaps still reasonable is:

    double ftoc(double temp)
    {
    temp -= 32;
    temp /= 1.8;
    return temp;
    }

    because temp represents a temperature throughout, though its units
    change. But it would clearly be wrong if the variable were called
    "tempinfahrenhe it".

    So I don't think there's a special rule for re-using argument
    variables. You should just apply the same reasoning you would for any
    variable: does it make sense to use this variable for this purpose?

    -- Richard
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

    Comment

    • Richard

      #32
      Re: Re-use the argument?

      richard@cogsci. ed.ac.uk (Richard Tobin) writes:
      In article <g2h74u$5h3$1@r egistered.motza rella.org>,
      Richard <rgrdev@gmail.c omwrote:
      >
      >>>>Generally it is considered bad form to modify an argument to a
      >>>>function. It makes things a bit more difficult to read.
      >
      >>>What a load of nonsense. If its not a const its no more than a local
      >>>variable.
      >
      [...]
      >
      >>I really am not interested in that. Its effectively a local variable and
      >>I have never seen anyone object, in the real world, to it being
      >>manipulated .
      >
      There's clearly no general rule against modifying variables, at least
      in traditional imperative languages like C. But that doesn't mean
      you should use any available variable as a temporary. Some variables
      It's not a temporary. Any more than any other is.
      are intended for a single purpose, and are named accordingly. Some
      variables ("i" for example) are traditionally used for a succession
      Yes. Agreed. But I'm not sure what this simple concept has to do with
      the question about the rights and wrongs of using a parameter. Clearly
      you do not use a parameter called "szDate" or whatever to store a
      celsius reading ....
      of values in a loop. The key is to be clear. For example, consider
      a function to determine kinetic energy given mass and speed:
      >
      double ke(double mass, double speed)
      {
      return mass * speed * speed;
      }
      >
      It would clearly be wrong to write:
      >
      double ke(double mass, double speed)
      {
      speed *= speed;
      speed *= mass;
      return speed;
      }
      >
      because that would use the variable "speed" for something which
      is not a speed. On the other hand, this would be quite reasonable:
      Obviously. I'm at a loss to understand why you feel the need to say this
      to be honest.
      >
      int mystrlen(char *s)
      {
      int l = 0;
      while(*s++)
      l++;
      return l;
      }
      >
      "s" is clearly just a string, and it remains a string as you proceed
      through it.
      >
      Perhaps still reasonable is:
      >
      double ftoc(double temp)
      {
      temp -= 32;
      temp /= 1.8;
      return temp;
      }
      >
      because temp represents a temperature throughout, though its units
      change. But it would clearly be wrong if the variable were called
      "tempinfahrenhe it".
      >
      So I don't think there's a special rule for re-using argument
      There most definitely isn't any special rule.
      variables. You should just apply the same reasoning you would for any
      variable: does it make sense to use this variable for this purpose?
      >
      -- Richard
      I'm not sure who you are addressing this too so I will refrain from
      commenting further.

      Comment

      • Richard Tobin

        #33
        Re: Re-use the argument?

        In article <g2hj1r$mse$1@r egistered.motza rella.org>,
        Richard <rgrdev@gmail.c omwrote:
        >>>I really am not interested in that. Its effectively a local variable and
        >>>I have never seen anyone object, in the real world, to it being
        >>>manipulate d.
        [...]
        >I'm not sure who you are addressing this too so I will refrain from
        >commenting further.
        I am simply pointing out that though there may be no objection to
        re-using a parameter variable *because it's a parameter*, there may be
        other reasons to object to re-using it. And because parameter
        variables usually have a name indicating their purpose, that often
        means that those other reasons apply.

        The original poster's example was not a realistic one, so it is
        hard to say anything about it.

        -- Richard
        --
        In the selection of the two characters immediately succeeding the numeral 9,
        consideration shall be given to their replacement by the graphics 10 and 11 to
        facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

        Comment

        • Richard

          #34
          Re: Re-use the argument?

          richard@cogsci. ed.ac.uk (Richard Tobin) writes:
          In article <g2hj1r$mse$1@r egistered.motza rella.org>,
          Richard <rgrdev@gmail.c omwrote:
          >
          >>>>I really am not interested in that. Its effectively a local variable and
          >>>>I have never seen anyone object, in the real world, to it being
          >>>>manipulated .
          [...]
          >>I'm not sure who you are addressing this too so I will refrain from
          >>commenting further.
          >
          I am simply pointing out that though there may be no objection to
          re-using a parameter variable *because it's a parameter*, there may be
          other reasons to object to re-using it. And because parameter
          variables usually have a name indicating their purpose, that often
          means that those other reasons apply.
          I dont think there is any difference between any local variable and a
          parameter in this case. Use the variable after for what it was named. We
          are in agreement.
          >
          The original poster's example was not a realistic one, so it is
          hard to say anything about it.
          >
          -- Richard

          Comment

          • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

            #35
            Re: Re-use the argument?

            On Jun 8, 10:30 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
              double ke(double mass, double speed)
              {
                  speed *= speed;
                  speed *= mass;
                  return speed;
              }

            union {
            double speed;
            double temporary;
            double energy;
            };

            :-D

            Comment

            • Richard Bos

              #36
              Re: Re-use the argument?

              "Malcolm McLean" <regniztar@btin ternet.comwrote :
              "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
              I am not sure you buy you own advice either. I clicked a few of
              the source files form your website and you seem to do it in exactly
              the same places where I would: adjusting parameters and scanning
              arrays:
              It's not my advice. It's the generally accepted advice,
              It may be generally accepted by the author of "C for Dummies", but it
              isn't generally accepted by me.

              Richard

              Comment

              • Richard Heathfield

                #37
                Re: Re-use the argument?

                Richard Tobin said:

                <snip>
                The key is to be clear.
                No argument there - I'm just nit-hunting right now.
                For example, consider
                a function to determine kinetic energy given mass and speed:
                You can't. You need the velocity, not the speed.
                double ke(double mass, double speed)
                {
                return mass * speed * speed;
                }
                ....and you also need another trifling ingredient...

                double ke(double mass, double velocity)
                {
                return mass * velocity * velocity / 2.0;
                }

                For excruciatingly exact nit-picking, you could also accept, and multiply
                by, a parameter that represents the constant of proportionality required
                to make the equation work in a given system of units (whether it be SI,
                cgs, Imperial, or whatever).

                <good stuff snipped, quick summary retained below>
                So I don't think there's a special rule for re-using argument
                variables. You should just apply the same reasoning you would for any
                variable: does it make sense to use this variable for this purpose?
                Right.

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • pete

                  #38
                  [ot]Re: Re-use the argument?

                  Richard Heathfield wrote:
                  Richard Tobin said:
                  >
                  <snip>
                  >
                  >The key is to be clear.
                  >
                  No argument there - I'm just nit-hunting right now.
                  >
                  >For example, consider
                  >a function to determine kinetic energy given mass and speed:
                  >
                  You can't. You need the velocity, not the speed.
                  No; you need the speed.
                  Energy isn't a vector quantity.



                  --
                  pete

                  Comment

                  • Richard Tobin

                    #39
                    Re: Re-use the argument?

                    In article <G9-dnTC9gcCoQNHVnZ 2dnUVZ8t7inZ2d@ bt.com>,
                    Richard Heathfield <rjh@see.sig.in validwrote:
                    >For example, consider
                    >a function to determine kinetic energy given mass and speed:
                    >You can't. You need the velocity, not the speed.
                    The direction is irrelevant to the kinetic energy. Only the magnitude
                    of the velocity - i.e. the speed - is important.
                    >...and you also need another trifling ingredient...
                    Got me there.

                    -- Richard
                    --
                    In the selection of the two characters immediately succeeding the numeral 9,
                    consideration shall be given to their replacement by the graphics 10 and 11 to
                    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

                    Comment

                    • Richard Heathfield

                      #40
                      Re: [ot]Re: Re-use the argument?

                      pete said:
                      Richard Heathfield wrote:
                      >Richard Tobin said:
                      >>
                      ><snip>
                      >>
                      >>The key is to be clear.
                      >>
                      >No argument there - I'm just nit-hunting right now.
                      >>
                      >>For example, consider
                      >>a function to determine kinetic energy given mass and speed:
                      >>
                      >You can't. You need the velocity, not the speed.
                      >
                      No; you need the speed.
                      Energy isn't a vector quantity.
                      Whoops! Okay, so "need" was a bit strong in this specific case. Velocity
                      would, however, be of more use, generally speaking. For example, with
                      velocity and mass you can calculate not just kinetic energy, but also
                      momentum.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • thomas.mertes@gmx.at

                        #41
                        Re: Re-use the argument?

                        On 7 Jun., 08:48, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
                        Which of these functions would you go with?
                        >
                        unsigned Func(unsigned const x)
                        {
                        return 3*x - 2;
                        }
                        I guess that most people would prefer this one.
                        or:
                        >
                        unsigned Func(unsigned x)
                        {
                        x *= 3;
                        x -= 2;
                        >
                        return x;
                        }
                        I guess that this one is preferred by people which have
                        been exposed to OO thinking too much. :-) I know that C
                        does not use OO concepts for this but some people just think:

                        The message *= with the paramenter 3 is send to the object x.
                        The message -= with the parameter 2 is send to the object x.

                        The programming languages you learn infuence the way you
                        are thinking. Therefore 'OO people' have a different view to
                        programming which is sometimes an advantage and sometimes
                        a disadvantage.

                        IMHO you have a clearer view to programming if you do not
                        bend everything to the 'message send to object' concept.
                        Some things like stand alone functions and multiple dispatch
                        cannot be really covered with this way of thinking.

                        You need a broader view to realize that in the real world often
                        a method (function) is handling with two or more objects.
                        In this cases it is not natural to promote one parameter to
                        the this/self parameter and to demote the other parameters.
                        There is no reason for this unbalance, but languages like
                        C++ and Java can only handle such things in a asymmetric way.

                        Ok, I have to stop here before it gets off topic. :-)

                        Greetings Thomas Mertes

                        Seed7 Homepage: http://seed7.sourceforge.net
                        Seed7 - The extensible programming language: User defined statements
                        and operators, abstract data types, templates without special
                        syntax, OO with interfaces and multiple dispatch, statically typed,
                        interpreted or compiled, portable, runs under linux/unix/windows.

                        Comment

                        • Nick Keighley

                          #42
                          Re: Re-use the argument?

                          On 9 Jun, 08:39, Richard Heathfield <r...@see.sig.i nvalidwrote:
                          Richard Tobin said:
                          >
                          <snip>
                          >
                          The key is to be clear.
                          >
                          No argument there - I'm just nit-hunting right now.
                          >
                          For example, consider
                          a function to determine kinetic energy given mass and speed:
                          >
                          You can't. You need the velocity, not the speed.
                          since energy is not a vector you can get by with the magnitude
                          of the velocity, ie. the speed.

                          I'm trying to think of an example where this is wrong.
                          If the energy was integrated over some path?


                          --
                          Nick Keighley

                          Comment

                          • Richard Heathfield

                            #43
                            Re: Re-use the argument?

                            Nick Keighley said:
                            On 9 Jun, 08:39, Richard Heathfield <r...@see.sig.i nvalidwrote:
                            >Richard Tobin said:
                            >>
                            ><snip>
                            >>
                            The key is to be clear.
                            >>
                            >No argument there - I'm just nit-hunting right now.
                            >>
                            For example, consider
                            a function to determine kinetic energy given mass and speed:
                            >>
                            >You can't. You need the velocity, not the speed.
                            >
                            since energy is not a vector you can get by with the magnitude
                            of the velocity, ie. the speed.
                            Yes, I overstated my case.
                            I'm trying to think of an example where this is wrong.
                            If the energy was integrated over some path?
                            Momentum! (Which isn't energy, so it isn't really the droid you're looking
                            for, but at least it allows me to salvage a modicum of general correctness
                            from my specific error...)

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            • Malcolm McLean

                              #44
                              Re: Re-use the argument?

                              "Richard Tobin" <richard@cogsci .ed.ac.ukwrote in message news
                              In article <g2hj1r$mse$1@r egistered.motza rella.org>,
                              Richard <rgrdev@gmail.c omwrote:
                              >
                              >>>>I really am not interested in that. Its effectively a local variable and
                              >>>>I have never seen anyone object, in the real world, to it being
                              >>>>manipulated .
                              [...]
                              >>I'm not sure who you are addressing this too so I will refrain from
                              >>commenting further.
                              >
                              I am simply pointing out that though there may be no objection to
                              re-using a parameter variable *because it's a parameter*, there may be
                              other reasons to object to re-using it. And because parameter
                              variables usually have a name indicating their purpose, that often
                              means that those other reasons apply.
                              >
                              I think that's the truth of the matter.

                              --
                              Free games and programming goodies.



                              Comment

                              • Richard

                                #45
                                Re: Re-use the argument?

                                "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                                "Richard Tobin" <richard@cogsci .ed.ac.ukwrote in message news
                                >In article <g2hj1r$mse$1@r egistered.motza rella.org>,
                                >Richard <rgrdev@gmail.c omwrote:
                                >>
                                >>>>>I really am not interested in that. Its effectively a local variable and
                                >>>>>I have never seen anyone object, in the real world, to it being
                                >>>>>manipulate d.
                                >[...]
                                >>>I'm not sure who you are addressing this too so I will refrain from
                                >>>commenting further.
                                >>
                                >I am simply pointing out that though there may be no objection to
                                >re-using a parameter variable *because it's a parameter*, there may be
                                >other reasons to object to re-using it. And because parameter
                                >variables usually have a name indicating their purpose, that often
                                >means that those other reasons apply.
                                >>
                                I think that's the truth of the matter.
                                I find it amazing that two seasoned programmers as yourself feel the
                                need to explain how to use a named variable. To the uninitiated it would
                                seem like some dark and mystic secret known only to the few. As it is,
                                its totally common sense and nothing more nor less.

                                Comment

                                Working...