Paranoid about style/elegance and function size, please quell

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kanze@gabi-soft.fr

    #31
    Re: Paranoid about style/elegance and function size, please quell

    brangdon@cix.co .uk (Dave Harris) wrote in message
    news:<memo.2003 0702204926.3391 1D@brangdon.mad asafish.com>...[color=blue]
    > E.Robert.Tisdal e@jpl.nasa.gov (E. Robert Tisdale) wrote (abridged):[color=green]
    > > Procedural programs are difficult to analyze and practically
    > > impossible to prove correct. You should avoid procedure calls (and
    > > assignment statements) whenever possible.[/color][/color]
    [color=blue]
    > Right. And you should make sure no-one confuses a procedure with a
    > function. One way to achieve that is to make sure functions never have
    > side effects and procedures never return results.[/color]

    So what do you do with rand()? (I like the rule, but there are
    exceptions.)

    --
    James Kanze GABI Software
    mailto:kanze@ga bi-soft.fr
    Conseils en informatique orientée objet/

    Beratung in objektorientier ter
    Datenverarbeitu ng
    11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45
    16

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.m oderated. First time posters: Do this! ]

    Comment

    • Dave Harris

      #32
      Re: Paranoid about style/elegance and function size, please quell

      kanze@gabi-soft.fr () wrote (abridged):[color=blue][color=green]
      > > Right. And you should make sure no-one confuses a procedure with
      > > a function. One way to achieve that is to make sure functions
      > > never have side effects and procedures never return results.[/color]
      >
      > So what do you do with rand()?[/color]

      Rand ought to be an object anyway:

      class {
      int state; // or whatever
      public:
      void srand( int seed );
      void advance();
      int current() const;
      // int next() const { advance(); return current(); }
      };

      [color=blue]
      > (I like the rule, but there are exceptions.)[/color]

      I agree there are exceptions. Another possible exception is having a
      procedure return information about whether it succeeded.

      These can be eliminated, of course, and some people say they always
      should be, but I don't take that extreme view. The circumlocutions
      sometimes have drawbacks which are worse than the problem they
      solve.

      -- Dave Harris, Nottingham, UK

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.m oderated. First time posters: Do this! ]

      Comment

      • Francis Glassborow

        #33
        Re: Paranoid about style/elegance and function size, please quell

        In message <3F048BDA.30403 06@jpl.nasa.gov >, E. Robert Tisdale
        <E.Robert.Tisda le@jpl.nasa.gov > writes[color=blue]
        >Francis Glassborow wrote:
        >[color=green]
        >> And you were responding to Mr. Tisdale's rewriting of what I had written.[/color]
        >
        >Your function
        >
        > void foo(void){
        > std::cout << std::clock();
        > }
        >
        >modifies a global variable -- cout.[/color]

        So? being a global it is available for anyone to check. Indeed if they
        are really paranoid they can switch on exception behaviour for streams.


        --
        ACCU Spring Conference 2003 April 2-5
        The Conference you should not have missed
        ACCU Spring Conference 2004 Late April
        Francis Glassborow ACCU


        [ See http://www.gotw.ca/resources/clcm.htm for info about ]
        [ comp.lang.c++.m oderated. First time posters: Do this! ]

        Comment

        • Francis Glassborow

          #34
          Re: Paranoid about style/elegance and function size, please quell

          In message <l1llvepkg3.fsf @budvar.future-i.net>, Ed Avis
          <ed@membled.com > writes[color=blue]
          >I am just thinking of a relatively high-level interface such as
          >
          > class Screen {
          > XXX display(Graphic );
          > };
          >
          >XXX could be void or it could be Screen&, returning *this. The latter
          >style would let you chain display() calls, if you are so inclined.[/color]

          And if you read my original guideline it said something that included
          that possibility. Tisdale may have taken us down this path but he was
          disagreeing with a guideline that most seem to approve of. I wasn't the
          OP but my guidelines were a response to his post. Tisdale claimed that
          the guideline was a bad one. I have seen nothing to change my mind.

          --
          ACCU Spring Conference 2003 April 2-5
          The Conference you should not have missed
          ACCU Spring Conference 2004 Late April
          Francis Glassborow ACCU


          [ See http://www.gotw.ca/resources/clcm.htm for info about ]
          [ comp.lang.c++.m oderated. First time posters: Do this! ]

          Comment

          • E. Robert Tisdale

            #35
            Re: Paranoid about style/elegance and function size, please quell

            James Kanze:
            [color=blue]
            > Dave Harris) wrote:
            >[color=green]
            >>And you should make sure no-one confuses a procedure with a function.
            >>One way to achieve that is to make sure that functions
            >> never have side effects and procedures never return results.[/color]
            >
            > So what do you do with rand()?
            > (I like the rule, but there are exceptions.)[/color]

            The rand() function was a *bad* idea for a number of different reasons.
            It should be replace with a Uniform Random Number Generator

            class URNG {
            private:
            // state representation
            public:
            // operators
            int operator()(void );
            // constructors
            URNG(unsigned int seed);
            };

            int main(int argc, char* argv[]) {
            URNG rand(0);
            // use rand()
            return 0;
            }


            [ See http://www.gotw.ca/resources/clcm.htm for info about ]
            [ comp.lang.c++.m oderated. First time posters: Do this! ]

            Comment

            • llewelly

              #36
              Re: Paranoid about style/elegance and function size, please quell

              "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:
              [color=blue]
              > James Kanze:
              >[color=green]
              >> Dave Harris) wrote:
              >>[color=darkred]
              >>>And you should make sure no-one confuses a procedure with a function.
              >>>One way to achieve that is to make sure that functions
              >>> never have side effects and procedures never return results.[/color]
              >>
              >> So what do you do with rand()?
              >> (I like the rule, but there are exceptions.)[/color]
              >
              > The rand() function was a *bad* idea for a number of different[/color]
              reasons.[color=blue]
              > It should be replace with a Uniform Random Number Generator
              >
              > class URNG {[/color]

              Yes, however, let's please not give it a name which looks like a cross
              between a macro and punch to the gut. Anyway, boost has some
              rather nice random number generators.
              [color=blue]
              > private:
              > // state representation
              > public:
              > // operators
              > int operator()(void );
              > // constructors
              > URNG(unsigned int seed);
              > };
              >
              > int main(int argc, char* argv[]) {
              > URNG rand(0);
              > // use rand()
              > return 0;
              > }[/color]
              [snip]

              [ See http://www.gotw.ca/resources/clcm.htm for info about ]
              [ comp.lang.c++.m oderated. First time posters: Do this! ]

              Comment

              Working...