Re-use the argument?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Flash Gordon

    #46
    Re: Re-use the argument?

    Malcolm McLean wrote, On 09/06/08 20:57:
    "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.
    Often if you pick the names appropriately the above rules allows
    modifying them.

    int send(unsigned char *txbuf, size_t bytes_to_send)
    {
    int ret=0;
    while (bytes_to_send) {
    ret = low_level_send( txbuf,bytes_to_ send);
    if (ret 0) {
    bytes_to_send -= ret;
    txbuf += ret;
    }
    }
    return ret;
    }

    The above is a simplification of a real and very important function in
    one of my companies major applications. The real function has error
    checking and low_level_send is actually a system-specific function which
    is not guaranteed to send all of the data and return the amount of data
    it sent. There are other ways it could be implemented, but this is
    approximately how I did it.
    --
    Flash Gordon

    Comment

    • Richard Tobin

      #47
      Re: Re-use the argument?

      In article <3sh0i5xanr.ln2 @news.flash-gordon.me.uk>,
      Flash Gordon <spam@flash-gordon.me.ukwro te:
      >Often if you pick the names appropriately the above rules allows
      >modifying them.
      >
      >int send(unsigned char *txbuf, size_t bytes_to_send)
      >{
      int ret=0;
      while (bytes_to_send) {
      ret = low_level_send( txbuf,bytes_to_ send);
      if (ret 0) {
      bytes_to_send -= ret;
      txbuf += ret;
      }
      }
      return ret;
      >}
      It's not coincidence that this works in a case where it could
      naturally (for a suitable value of "naturally" ) be written
      recursively:

      int send(unsigned char *txbuf, size_t bytes_to_send)
      {
      int ret;
      if (bytes_to_send == 0)
      return 0;
      ret = low_level_send( txbuf, bytes_to_send);
      return send(txbuf + ret, bytes_to_send - ret);
      }

      You could view Flash's version as being a work-around for the
      fact that C doesn't make any guarantees about implementing tail-calls
      efficiently.

      -- 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

      • pete

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

        Richard Heathfield wrote:
        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.
        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.
        "You can't" is the part
        that was a bit strong in this specific case.

        You need more practice making mistakes.

        --
        pete

        Comment

        • jaysome

          #49
          Re: Re-use the argument?

          On Sat, 07 Jun 2008 07:26:01 +0000, Richard Heathfield
          <rjh@see.sig.in validwrote:
          >Tomás Ó hÉilidhe said:
          >
          >>
          >Which of these functions would you go with?
          >>
          > unsigned Func(unsigned const x)
          > {
          > return 3*x - 2;
          > }
          >>
          >or:
          >>
          > unsigned Func(unsigned x)
          > {
          > x *= 3;
          > x -= 2;
          >>
          > return x;
          > }
          >
          >Neither. I'd go with this one:
          >
          >unsigned Func(unsigned int x)
          >{
          return 3 * x - 2;
          >}
          And if the function argument was instead a "signed int", you'd go with
          this?

          signed int Func(signed int x)
          {
          return 3 * x - 2;
          }

          Since "signed int" is the same as plain "int", why not this:

          int Func(int x)
          {
          return 3 * x - 2;
          }

          And since "unsigned int" is the same as "unsigned", why not this:

          unsigned Func(unsigned x)
          {
          return 3 * x - 2;
          }

          Questions:

          1. Is there ever a reason to use "unsigned int" instead of plain old
          "unsigned"?

          2. Is there ever a reason to use "signed int" instead of plain old
          "int"?

          3. Carrying it a bit further, is there ever a reason to use "signed
          char" or "unsigned char" instead of plain old "char"?

          --
          jay

          Answers:
          1. No
          2. No
          3. Yes. (C does not specify whether plain old "char" is signed or
          unsigned.) I have come across code, on platforms in which CHAR_BIT is
          8, such as the following, which is a time bomb waiting to explode,
          like an atomic Little Boy bomb:

          /* define signed 8-bit type */
          typedef char S8;

          Of course the correct typedef is as follows (remember, the
          platform-specific value of CHAR_BIT is 8 in this example):

          /* define signed 8-bit type */
          typedef signed char S8;

          --
          jay



          Comment

          • Richard Heathfield

            #50
            Re: Re-use the argument?

            jaysome said:
            On Sat, 07 Jun 2008 07:26:01 +0000, Richard Heathfield
            <rjh@see.sig.in validwrote:
            <snip>
            >
            >>I'd go with this one:
            >>
            >>unsigned Func(unsigned int x)
            >>{
            > return 3 * x - 2;
            >>}
            >
            And if the function argument was instead a "signed int", you'd go with
            this?
            >
            signed int Func(signed int x)
            Oops, good spot. I stopped editing too soon, didn't I? Either

            unsigned Func(unsigned x)

            or

            unsigned int Func(unsigned int x)

            would be a better declarator.
            Questions:
            >
            1. Is there ever a reason to use "unsigned int" instead of plain old
            "unsigned"?
            Taste?
            2. Is there ever a reason to use "signed int" instead of plain old
            "int"?
            Bit-fields, and taste.
            3. Carrying it a bit further, is there ever a reason to use "signed
            char" or "unsigned char" instead of plain old "char"?
            Yes. If you want to represent characters or collections thereof, use char.
            That way, you get the Right Thing on systems where char is signed by
            default (for good reason) *and* on systems where char is unsigned by
            default (again, for good reason).

            If you just want some iddy-biddy integers that can be negative and space is
            tight, use signed char.

            If what you're really doing is representing or interpreting bytes for the
            sake of bytes (e.g. peeking at an object representation) , use unsigned
            char.

            --
            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

            • Richard Heathfield

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

              pete said:

              <snip>
              You need more practice making mistakes.
              Your advice, pete, is wonderfully quoteable, but impractical. If I spent
              any more time practising mistakes than I already do, I wouldn't have any
              time left for playing keyboards (badly, but loudly), drinking coffee
              (expertly), or walking the cat (even in the rain).

              --
              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

              • rio

                #52
                Re: Re-use the argument?


                "Tomás Ó hÉilidhe" <toe@lavabit.co mha scritto nel messaggio
                news:85aef7b0-d635-43c3-8e5d-0d264f09ada6@e5 3g2000hsa.googl egroups.com...
                >
                Which of these functions would you go with?
                >
                unsigned Func(unsigned const x)
                {
                return 3*x - 2;
                }
                >
                or:
                >
                unsigned Func(unsigned x)
                {
                x *= 3;
                x -= 2;
                >
                return x;
                }
                i would never write above because it seems i remember
                i have some not good experience with change
                the argument value of a function
                in that function
                [in assembly at last]





                Comment

                • Ron Ford

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

                  On Tue, 10 Jun 2008 06:23:32 +0000, Richard Heathfield posted:
                  pete said:
                  >
                  <snip>
                  >
                  >You need more practice making mistakes.
                  >
                  Your advice, pete, is wonderfully quoteable, but impractical. If I spent
                  any more time practising mistakes than I already do, I wouldn't have any
                  time left for playing keyboards (badly, but loudly), drinking coffee
                  (expertly), or walking the cat (even in the rain).
                  Pete will be at a disadvantage if he mistakes walking a cat with exploding
                  eight pounds of feline mass-energy.

                  I understand you have a new book out on algorithms. If it were subtitled
                  "The essential Knuth in C," would it be misrepresented?
                  --
                  There are men so philosophical that they can see humor in their own
                  toothaches. But there has never lived a man so philosophical that he could
                  see the toothache in his own humor.
                  H. L. Mencken

                  Comment

                  • Malcolm McLean

                    #54
                    Re: Re-use the argument?

                    "Richard" <rgrdev@gmail.c omwrote in message news:
                    "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
                    >>>>>>manipulat ed.
                    >>[...]
                    >>>>I'm not sure who you are addressing this too so I will refrain from
                    >>>>commentin g 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.
                    >
                    Richard Tobin and I are both aware of the idea that it is considered poor
                    style to modify a parameter, and neither of us dismiss that idea. The
                    question is, why is a reasonable idea?

                    We see beyond "C is call by value, therefore you can modify the parameter as
                    a local, therefore there can be no issues with modifying parameters."


                    --
                    Free games and programming goodies.



                    Comment

                    • Richard Heathfield

                      #55
                      Re: Re-use the argument?

                      Malcolm McLean said:

                      <snip>
                      Richard Tobin and I are both aware of the idea that it is considered poor
                      style to modify a parameter, and neither of us dismiss that idea. The
                      question is, why is a reasonable idea?
                      You have yet to explain why it is a reasonable idea. (That is, you have yet
                      to explain why it is considered poor style to modify a parameter, and who
                      it is that is doing the considering, and why we should take them
                      seriously.)
                      We see beyond "C is call by value, therefore you can modify the parameter
                      as a local, therefore there can be no issues with modifying parameters."
                      Strawman. Nobody has claimed that "there can be no issues with modifying
                      parameters", as far as I know. But the claim that "it is considered poor
                      style to modify a parameter" is one that has not yet been justified.


                      --
                      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

                      • CBFalconer

                        #56
                        Re: Re-use the argument?

                        Malcolm McLean wrote:
                        "Richard" <rgrdev@gmail.c omwrote in message news:
                        >"Malcolm McLean" <regniztar@btin ternet.comwrite s:
                        >>"Richard Tobin" <richard@cogsci .ed.ac.ukwrote in message news
                        >>>
                        .... snip ...
                        >>>
                        >>>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.
                        >
                        Richard Tobin and I are both aware of the idea that it is
                        considered poor style to modify a parameter, and neither of us
                        dismiss that idea. The question is, why is a reasonable idea?
                        >
                        We see beyond "C is call by value, therefore you can modify the
                        parameter as a local, therefore there can be no issues with
                        modifying parameters."
                        I think the point is that C passes by value, and that parameters
                        can be looked on as being local variables that have been
                        pre-initialized. Altering them affects nothing outside the
                        function, and thus is totally harmless to the remainder of the
                        program.

                        --
                        [mail]: Chuck F (cbfalconer at maineline dot net)
                        [page]: <http://cbfalconer.home .att.net>
                        Try the download section.


                        ** Posted from http://www.teranews.com **

                        Comment

                        • Willem

                          #57
                          Re: Re-use the argument?

                          Richard wrote:
                          ) Malcolm McLean said:
                          )Richard Tobin and I are both aware of the idea that it is considered poor
                          )style to modify a parameter, and neither of us dismiss that idea. The
                          )question is, why is a reasonable idea?
                          )
                          ) You have yet to explain why it is a reasonable idea. (That is, you have yet
                          ) to explain why it is considered poor style to modify a parameter, and who
                          ) it is that is doing the considering, and why we should take them
                          ) seriously.)

                          I'll play devil's advocate then and give an example:

                          Suppose you have written a large function, several pages' worth.

                          int process_daily_s tuff(date_t today, user_t user)
                          {
                          ...
                          }

                          Somewhere along the line, you need to access yesterday's data, and
                          you choose to modify the 'today' variable for this, instead of making
                          a new 'yesterday' variable.

                          Now, this code runs happily for a while. Then, your colleague needs to
                          implement some additional functionality in the process_daily_s tuff.
                          He adds his code at the end of the function, and he uses the 'today'
                          variable, fully expecting it to point to the current date.
                          But it doesn't. You changed it. Subtle bugs ensue.


                          So, in short: changing parameters halfway a function could be confusing
                          to other programmers who need to change the function later on.
                          (And yes, you yourself count as an 'other programmer' if enough time
                          has gone between you writing it and you having to change it.)


                          SaSW, Willem
                          --
                          Disclaimer: I am in no way responsible for any of the statements
                          made in the above text. For all I know I might be
                          drugged or something..
                          No I'm not paranoid. You all think I'm paranoid, don't you !
                          #EOT

                          Comment

                          • Richard Heathfield

                            #58
                            Re: Re-use the argument?

                            Willem said:
                            Richard wrote:
                            ) Malcolm McLean said:
                            )Richard Tobin and I are both aware of the idea that it is considered poor
                            )style to modify a parameter, and neither of us dismiss that idea. The
                            )question is, why is a reasonable idea?
                            )
                            ) You have yet to explain why it is a reasonable idea. (That is, you have
                            yet ) to explain why it is considered poor style to modify a parameter, and
                            who ) it is that is doing the considering, and why we should take them
                            ) seriously.)
                            >
                            I'll play devil's advocate then and give an example:
                            >
                            Suppose you have written a large function, several pages' worth.
                            Yes - I don't think anyone disputes that cases can be found where it's a bad
                            idea. There are cases where it's a bad idea to call strlen, but that doesn't
                            make a strlen call "poor style", does it?

                            <snip>

                            --
                            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

                            • Chris Dollin

                              #59
                              Re: Re-use the argument?

                              Willem wrote:
                              Richard wrote:
                              ) Malcolm McLean said:
                              )Richard Tobin and I are both aware of the idea that it is considered poor
                              )style to modify a parameter, and neither of us dismiss that idea. The
                              )question is, why is a reasonable idea?
                              )
                              ) You have yet to explain why it is a reasonable idea. (That is, you have yet
                              ) to explain why it is considered poor style to modify a parameter, and who
                              ) it is that is doing the considering, and why we should take them
                              ) seriously.)
                              >
                              I'll play devil's advocate then and give an example:
                              >
                              Suppose you have written a large function, several pages' worth.
                              Almost all of the time, that's a false supposition.

                              --
                              "The original and modest plans had to be continually extended."/Sector General/

                              Hewlett-Packard Limited registered no:
                              registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

                              Comment

                              • Willem

                                #60
                                Re: Re-use the argument?

                                Chris wrote:
                                ) Willem wrote:
                                )Suppose you have written a large function, several pages' worth.
                                )
                                ) Almost all of the time, that's a false supposition.

                                s/'s/ should be/

                                I've seen plenty of code that fit the bill.


                                SaSW, Willem
                                --
                                Disclaimer: I am in no way responsible for any of the statements
                                made in the above text. For all I know I might be
                                drugged or something..
                                No I'm not paranoid. You all think I'm paranoid, don't you !
                                #EOT

                                Comment

                                Working...